HelloDoctor / hello_doctor.py
jsusanthi's picture
Upload hello_doctor.py
e1275e9 verified
raw
history blame
2.62 kB
# -*- 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)