artificial-developer/flask
0
1from flask import Flask, request, jsonify2from openai import OpenAI3import base644from dotenv import load_dotenv5import os6 7load_dotenv()8 9app = Flask(__name__)10 11OPENAI_API_KEY = os.environ['OPENAI_API_KEY']12client = OpenAI(api_key=OPENAI_API_KEY)13 14user_question = """You are a dermatologist and an expert in analyzing images related to skin diseases working for a very reputed hospital. You will be provided with images with skin diseases and you need to identify the any skin disease or health issues. You need to generate the result in a detailed manner. Write all the findings, next steps, recommendation, etc. You only need to respond if the image is related to a human body and health issues. You must have to answer but also write a disclaimer saying that "Consult with a Doctor before making any decisions. Remember, if certain aspects are not clear from the image, it's okay to state 'Unable to determine based on the provided image. If the given image does not have any disease, then give the response 'Unable to determine based on the provided image'."""15 16def analyze_image(image_data, question):17 messages = [{"role": "user", "content": [{"type": "text", "text": user_question}]}]18 messages[0]["content"].append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}})19 response = client.chat.completions.create(model="gpt-4-vision-preview", messages=messages, max_tokens=4096)20 return response.choices[0].message.content21 22@app.route('/analyze', methods=['POST'])23def analyze():24 if 'image' not in request.files:25 return jsonify({'error': 'No image file provided'}), 40026 27 image_file = request.files['image']28 if image_file.filename == '':29 return jsonify({'error': 'No selected file'}), 40030 31 image_data = base64.b64encode(image_file.read()).decode('utf-8')32 question = user_question33 response = analyze_image(image_data, question)34 return jsonify({'response': response})35 36if __name__ == '__main__':37 app.run(debug=True)38 