Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from PIL import Image
|
| 7 |
+
##############commands######################
|
| 8 |
+
#python3 -m venv venv
|
| 9 |
+
#source venv/bin/activate
|
| 10 |
+
#pip install google-generativeai gradio python-dotenv pillow
|
| 11 |
+
###########################################
|
| 12 |
+
# 1. SETUP API KEY
|
| 13 |
+
# ----------------
|
| 14 |
+
# Load environment variables from the .env file (for local development)
|
| 15 |
+
load_dotenv()
|
| 16 |
+
|
| 17 |
+
# Fetch the key. If running on Hugging Face, it will look in their 'Secrets'.
|
| 18 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 19 |
+
|
| 20 |
+
if not api_key:
|
| 21 |
+
raise ValueError("API Key not found. Please set GEMINI_API_KEY in .env or Secrets.")
|
| 22 |
+
|
| 23 |
+
# Configure the Google AI library
|
| 24 |
+
genai.configure(api_key=api_key)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def analyze_symptoms(text_input, image_input , temperature, top_p ):
|
| 28 |
+
"""
|
| 29 |
+
This function takes text and an image, sends them to Gemini,
|
| 30 |
+
and returns the medical analysis.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
# Validation: Ensure at least one input is provided
|
| 34 |
+
if not text_input and not image_input:
|
| 35 |
+
return "Please provide a description or an image."
|
| 36 |
+
|
| 37 |
+
# Initialize the model
|
| 38 |
+
# 'gemini-1.5-flash' is excellent for multimodal tasks (fast & accurate).
|
| 39 |
+
model = genai.GenerativeModel('gemini-2.5-flash')
|
| 40 |
+
generation_config = genai.types.GenerationConfig(
|
| 41 |
+
temperature=temperature,
|
| 42 |
+
top_p=top_p
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Create the System Prompt
|
| 46 |
+
# We must instruct the AI to act like a doctor but be safe.
|
| 47 |
+
prompt_text = (
|
| 48 |
+
"You are an AI medical assistant. "
|
| 49 |
+
"Analyze the following symptoms and the provided image (if any). "
|
| 50 |
+
"Provide potential causes and home remedies. "
|
| 51 |
+
"IMPORTANT: You must start your response with a clear disclaimer "
|
| 52 |
+
"that you are an AI and this is not professional medical advice."
|
| 53 |
+
f"\n\nPatient Description: {text_input}"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Prepare the content for Gemini
|
| 57 |
+
# Gemini accepts a list containing text and/or image data.
|
| 58 |
+
content = [prompt_text]
|
| 59 |
+
if image_input:
|
| 60 |
+
content.append(image_input)
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
# Generate content
|
| 64 |
+
response = model.generate_content(content , generation_config=generation_config)
|
| 65 |
+
return response.text
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error: {str(e)}"
|
| 68 |
+
|
| 69 |
+
# Blocks allows us to build complex layouts (Rows, Columns, etc.)
|
| 70 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 71 |
+
|
| 72 |
+
# Header
|
| 73 |
+
gr.Markdown("# 🏥 AI Health Symptom Checker")
|
| 74 |
+
gr.Markdown("Describe your symptoms and upload an image (e.g., a rash, sore throat) for preliminary analysis.")
|
| 75 |
+
|
| 76 |
+
# Layout: Use a Row to put inputs side-by-side (on large screens)
|
| 77 |
+
with gr.Row():
|
| 78 |
+
# Left Column: Inputs
|
| 79 |
+
with gr.Column():
|
| 80 |
+
symptoms = gr.Textbox(
|
| 81 |
+
label="Describe your symptoms",
|
| 82 |
+
placeholder="E.g., I've had a headache and sore throat for 2 days...",
|
| 83 |
+
lines=4
|
| 84 |
+
)
|
| 85 |
+
# The type='pil' ensures the image is ready for the Python Pillow library
|
| 86 |
+
img_upload = gr.Image(label="Upload Image (Optional)", type="pil")
|
| 87 |
+
|
| 88 |
+
# Custom Button
|
| 89 |
+
submit_btn = gr.Button("Analyze Symptoms", variant="primary")
|
| 90 |
+
|
| 91 |
+
# Configure the Google AI library
|
| 92 |
+
temperature_slider = gr.Slider(
|
| 93 |
+
minimum=0.0,
|
| 94 |
+
maximum=1.0,
|
| 95 |
+
value=0.7,
|
| 96 |
+
step=0.05,
|
| 97 |
+
label="🔥 Temperature (Creativity Level)"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
top_p_slider = gr.Slider(
|
| 101 |
+
minimum=0.0,
|
| 102 |
+
maximum=1.0,
|
| 103 |
+
value=0.9,
|
| 104 |
+
step=0.05,
|
| 105 |
+
label="🎯 Top-p (Response Diversity)"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Right Column: Output
|
| 109 |
+
with gr.Column():
|
| 110 |
+
output_box = gr.Markdown(label="AI Analysis")
|
| 111 |
+
|
| 112 |
+
# 4. CONNECTING LOGIC (Event Listeners)
|
| 113 |
+
# -------------------------------------
|
| 114 |
+
# When the button is clicked, run 'analyze_symptoms'
|
| 115 |
+
# Inputs: [symptoms, img_upload] -> correspond to function arguments
|
| 116 |
+
# Outputs: [output_box] -> where the result goes
|
| 117 |
+
submit_btn.click(
|
| 118 |
+
fn=analyze_symptoms,
|
| 119 |
+
inputs=[symptoms, img_upload, temperature_slider, top_p_slider],
|
| 120 |
+
outputs=output_box
|
| 121 |
+
)
|
| 122 |
+
print("App is Launching...")
|
| 123 |
+
# 5. LAUNCH THE APP
|
| 124 |
+
# -----------------
|
| 125 |
+
demo.launch()
|