Spaces:
Running
Running
Portfolio User
commited on
Commit
·
1f1b45b
1
Parent(s):
6cdf141
Robust deployment: lazy data loading and package structure
Browse files- agent/__init__.py +0 -0
- app.py +25 -31
- data/__init__.py +0 -0
agent/__init__.py
ADDED
|
File without changes
|
app.py
CHANGED
|
@@ -2,51 +2,45 @@ import gradio as gr
|
|
| 2 |
from agent.core import MedicalAgent
|
| 3 |
from data.loader import fetch_med_dialog_sample, format_dialogue_context
|
| 4 |
import os
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def medical_chat_interface(message, history):
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
response =
|
| 27 |
return response
|
| 28 |
except Exception as e:
|
| 29 |
return f"An error occurred: {str(e)}"
|
| 30 |
|
| 31 |
-
# Custom Theme
|
| 32 |
-
theme = gr.themes.Soft(
|
| 33 |
-
primary_hue="blue",
|
| 34 |
-
secondary_hue="slate",
|
| 35 |
-
neutral_hue="slate",
|
| 36 |
-
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 37 |
-
).set(
|
| 38 |
-
body_background_fill="*neutral_50",
|
| 39 |
-
block_background_fill="white",
|
| 40 |
-
block_border_width="1px",
|
| 41 |
-
)
|
| 42 |
|
| 43 |
with gr.Blocks(theme=theme, title="Medical AI Agent") as demo:
|
| 44 |
gr.Markdown(
|
| 45 |
"""
|
| 46 |
# 🏥 Medical AI Assistant
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
This agent is grounded in the **OpenMed** dataset collection and powered by **Gemini 3 Flash**.
|
| 50 |
|
| 51 |
*⚠️ Disclaimer: This is an AI tool for educational purposes. Consult a doctor for medical advice.*
|
| 52 |
"""
|
|
@@ -54,7 +48,7 @@ with gr.Blocks(theme=theme, title="Medical AI Agent") as demo:
|
|
| 54 |
|
| 55 |
chatbot = gr.ChatInterface(
|
| 56 |
fn=medical_chat_interface,
|
| 57 |
-
examples=["What are common symptoms of iron deficiency?", "
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
|
|
|
| 2 |
from agent.core import MedicalAgent
|
| 3 |
from data.loader import fetch_med_dialog_sample, format_dialogue_context
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
+
# Globals
|
| 7 |
+
agent = None
|
| 8 |
+
medical_context = ""
|
| 9 |
|
| 10 |
+
def get_agent():
|
| 11 |
+
global agent, medical_context
|
| 12 |
+
if agent is None:
|
| 13 |
+
try:
|
| 14 |
+
agent = MedicalAgent()
|
| 15 |
+
# Try to get a sample, but don't crash if it fails
|
| 16 |
+
try:
|
| 17 |
+
initial_data = fetch_med_dialog_sample(2)
|
| 18 |
+
medical_context = format_dialogue_context(initial_data)
|
| 19 |
+
except:
|
| 20 |
+
medical_context = "No additional context available."
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error initializing agent: {e}")
|
| 23 |
+
return agent
|
| 24 |
|
| 25 |
def medical_chat_interface(message, history):
|
| 26 |
+
current_agent = get_agent()
|
| 27 |
+
if not current_agent:
|
| 28 |
+
return "Error: Agent not initialized. Please check your GEMINI_API_KEY in Secrets."
|
| 29 |
|
| 30 |
try:
|
| 31 |
+
response = current_agent.get_response(message, context=medical_context)
|
| 32 |
return response
|
| 33 |
except Exception as e:
|
| 34 |
return f"An error occurred: {str(e)}"
|
| 35 |
|
| 36 |
+
# Custom Theme
|
| 37 |
+
theme = gr.themes.Soft(primary_hue="blue")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
with gr.Blocks(theme=theme, title="Medical AI Agent") as demo:
|
| 40 |
gr.Markdown(
|
| 41 |
"""
|
| 42 |
# 🏥 Medical AI Assistant
|
| 43 |
+
Powered by **Gemini 3 Flash** and **OpenMed** datasets.
|
|
|
|
|
|
|
| 44 |
|
| 45 |
*⚠️ Disclaimer: This is an AI tool for educational purposes. Consult a doctor for medical advice.*
|
| 46 |
"""
|
|
|
|
| 48 |
|
| 49 |
chatbot = gr.ChatInterface(
|
| 50 |
fn=medical_chat_interface,
|
| 51 |
+
examples=["What are common symptoms of iron deficiency?", "Explain hypertension in simple terms."]
|
| 52 |
)
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
data/__init__.py
ADDED
|
File without changes
|