Spaces:
Sleeping
Sleeping
Upload hello_doctor.py
Browse files- hello_doctor.py +82 -0
hello_doctor.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Hello doctor skin.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1OI0xTyanplOVAVOE0OmaZBoPmrFSS9tb
|
8 |
+
"""
|
9 |
+
|
10 |
+
!pip install --upgrade openai transformers gradio huggingface_hub
|
11 |
+
|
12 |
+
import os
|
13 |
+
from getpass import getpass
|
14 |
+
from huggingface_hub import login
|
15 |
+
|
16 |
+
# Securely input your keys
|
17 |
+
openai_api_key = getpass("Enter your OpenAI API key: ")
|
18 |
+
hf_token = getpass("Enter your Hugging Face token: ")
|
19 |
+
|
20 |
+
# Set environment variables (no keys shown in code)
|
21 |
+
os.environ["OPENAI_API_KEY"] = openai_api_key
|
22 |
+
|
23 |
+
# Login to Hugging Face
|
24 |
+
login(token=hf_token)
|
25 |
+
|
26 |
+
import openai
|
27 |
+
|
28 |
+
def analyze_symptoms(symptoms):
|
29 |
+
try:
|
30 |
+
client = openai.OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
31 |
+
response = client.chat.completions.create(
|
32 |
+
model="gpt-4-turbo",
|
33 |
+
messages=[
|
34 |
+
{"role": "system", "content": "You are a helpful medical assistant."},
|
35 |
+
{"role": "user", "content": f"I have these symptoms: {symptoms}. What might be the cause?"}
|
36 |
+
]
|
37 |
+
)
|
38 |
+
return response.choices[0].message.content
|
39 |
+
except Exception as e:
|
40 |
+
return f"Error analyzing symptoms: {e}"
|
41 |
+
|
42 |
+
from transformers import AutoProcessor, AutoModelForImageClassification
|
43 |
+
from PIL import Image
|
44 |
+
import torch
|
45 |
+
|
46 |
+
# Load public image model
|
47 |
+
image_model_id = "microsoft/beit-base-patch16-224-pt22k-ft22k"
|
48 |
+
processor = AutoProcessor.from_pretrained(image_model_id, token=HF_TOKEN)
|
49 |
+
model = AutoModelForImageClassification.from_pretrained(image_model_id, token=HF_TOKEN)
|
50 |
+
|
51 |
+
def classify_image(image):
|
52 |
+
try:
|
53 |
+
img = image.convert("RGB")
|
54 |
+
inputs = processor(images=img, return_tensors="pt")
|
55 |
+
with torch.no_grad():
|
56 |
+
outputs = model(**inputs)
|
57 |
+
predicted_class = outputs.logits.argmax(-1).item()
|
58 |
+
label = model.config.id2label[predicted_class]
|
59 |
+
return f"Predicted skin condition: {label}"
|
60 |
+
except Exception as e:
|
61 |
+
return f"Error classifying image: {e}"
|
62 |
+
|
63 |
+
import gradio as gr
|
64 |
+
|
65 |
+
iface = gr.Interface(
|
66 |
+
fn=lambda symptoms, img: (
|
67 |
+
analyze_symptoms(symptoms),
|
68 |
+
classify_image(img) if img else "No image uploaded"
|
69 |
+
),
|
70 |
+
inputs=[
|
71 |
+
gr.Textbox(label="Describe your symptoms"),
|
72 |
+
gr.Image(type="pil", label="Upload skin image")
|
73 |
+
],
|
74 |
+
outputs=[
|
75 |
+
gr.Textbox(label="Symptom Analysis"),
|
76 |
+
gr.Textbox(label="Image Diagnosis")
|
77 |
+
],
|
78 |
+
title="AI Doctor",
|
79 |
+
description="Enter your symptoms and/or upload a skin image to get medical insights. Powered by GPT-4 and Hugging Face vision transformers."
|
80 |
+
)
|
81 |
+
|
82 |
+
iface.launch(share=True)
|