File size: 2,619 Bytes
e1275e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
"""Hello doctor skin.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1OI0xTyanplOVAVOE0OmaZBoPmrFSS9tb
"""

!pip install --upgrade openai transformers gradio huggingface_hub

import os
from getpass import getpass
from huggingface_hub import login

# Securely input your keys
openai_api_key = getpass("Enter your OpenAI API key: ")
hf_token = getpass("Enter your Hugging Face token: ")

# Set environment variables (no keys shown in code)
os.environ["OPENAI_API_KEY"] = openai_api_key

# Login to Hugging Face
login(token=hf_token)

import openai

def analyze_symptoms(symptoms):
    try:
        client = openai.OpenAI(api_key=os.environ["OPENAI_API_KEY"])
        response = client.chat.completions.create(
            model="gpt-4-turbo",
            messages=[
                {"role": "system", "content": "You are a helpful medical assistant."},
                {"role": "user", "content": f"I have these symptoms: {symptoms}. What might be the cause?"}
            ]
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Error analyzing symptoms: {e}"

from transformers import AutoProcessor, AutoModelForImageClassification
from PIL import Image
import torch

# Load public image model
image_model_id = "microsoft/beit-base-patch16-224-pt22k-ft22k"
processor = AutoProcessor.from_pretrained(image_model_id, token=HF_TOKEN)
model = AutoModelForImageClassification.from_pretrained(image_model_id, token=HF_TOKEN)

def classify_image(image):
    try:
        img = image.convert("RGB")
        inputs = processor(images=img, return_tensors="pt")
        with torch.no_grad():
            outputs = model(**inputs)
            predicted_class = outputs.logits.argmax(-1).item()
            label = model.config.id2label[predicted_class]
        return f"Predicted skin condition: {label}"
    except Exception as e:
        return f"Error classifying image: {e}"

import gradio as gr

iface = gr.Interface(
    fn=lambda symptoms, img: (
        analyze_symptoms(symptoms),
        classify_image(img) if img else "No image uploaded"
    ),
    inputs=[
        gr.Textbox(label="Describe your symptoms"),
        gr.Image(type="pil", label="Upload skin image")
    ],
    outputs=[
        gr.Textbox(label="Symptom Analysis"),
        gr.Textbox(label="Image Diagnosis")
    ],
    title="AI Doctor",
    description="Enter your symptoms and/or upload a skin image to get medical insights. Powered by GPT-4 and Hugging Face vision transformers."
)

iface.launch(share=True)