artificial-developer/derma_ai
2
1import streamlit as st2from openai import OpenAI3import base644from fpdf import FPDF5from io import BytesIO6from PIL import Image7import os8import pandas as pd9from streamlit_extras.colored_header import colored_header10from dotenv import load_dotenv11 12load_dotenv()13 14OPENAI_API_KEY = os.environ['OPENAI_API_KEY']15client = OpenAI(api_key=OPENAI_API_KEY)16 17 18def encode_image(uploaded_file):19 return base64.b64encode(uploaded_file.getvalue()).decode('utf-8')20 21def analyze_image(image_data_list, question, is_url=False):22 messages = [{"role": "user", "content": [{"type": "text", "text": question}]}]23 24 for image_data in image_data_list:25 if is_url:26 messages[0]["content"].append({"type": "image_url", "image_url": {"url": image_data}})27 else:28 messages[0]["content"].append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}})29 30 response = client.chat.completions.create(model="gpt-4-vision-preview", messages=messages, max_tokens=4096)31 return response.choices[0].message.content32 33def example():34 colored_header(35 label="DermAI Medical Assistant",36 description="Revolutionizing Skin Disease Diagnosis with AI, Upload your image get diagnosed and download report in pdf.",37 color_name="violet-70",38 )39 40 41example()42 43image_input_method = st.radio("Select Image Input Method",44 ('Upload Image', 'Enter Image URL'))45user_question = """You are a dermatologist and an expert in analzying 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 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, its okay to state 'Unable to determine based on the provided image. if the give image not have any diseas the give response 'Unable to determine based on the provided image'. Now analyze the image and answer the above questions in the same structured manner defined above"""46 47 48 49 50image_data_list = []51 52if image_input_method == 'Upload Image':53 uploaded_files = st.file_uploader("Choose images...", type=["jpg", "jpeg", "png"], accept_multiple_files=True)54 if uploaded_files:55 for uploaded_file in uploaded_files:56 image_data_list.append(encode_image(uploaded_file))57 img_bytes = uploaded_file.read() 58 st.image(Image.open(BytesIO(img_bytes)), caption='Uploaded Image')59 60 if st.button('Analyze image(s)'):61 62 output = analyze_image(image_data_list, user_question)63 def generate_pdf(output, img_bytes):64 pdf = FPDF()65 pdf.add_page()66 67 pdf.set_font("Arial", size=12)68 pdf.set_fill_color(200, 220, 255)69 pdf.set_text_color(0, 0, 0)70 71 pdf.set_font("Arial", 'B', 16)72 pdf.cell(0, 10, "Initial Diagnose Report by DermAI", ln=True, align='C', fill=True)73 74 pdf.set_font("Arial", size=12)75 pdf.ln(10) 76 pdf.multi_cell(0, 10, output)77 78 pdf.ln(10) 79 80 img_path = "temp_image.jpg"81 with open(img_path, "wb") as img_file:82 img_file.write(img_bytes)83 pdf.image(img_path, x=20, y=None, w=170)84 85 86 pdf_output = pdf.output(dest="S").encode("latin1")87 return pdf_output88 89 90 91 pdf_output = generate_pdf(output, img_bytes)92 93 st.markdown("### Initial Diagnose Report by DermAI")94 95 st.write(output)96 97 st.download_button(98 label="Download as PDF",99 data=pdf_output,100 file_name='Report.pdf',101 mime='application/pdf'102 )103 104 105elif image_input_method == 'Enter Image URL':106 image_urls = st.text_area("Enter the URLs of the images, one per line")107 if image_urls and st.button('Analyze image URL(s)'):108 url_list = image_urls.split('\n')109 insights = analyze_image(url_list, user_question, is_url=True)110 st.write(insights)