Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ import spaces
|
|
8 |
# --- Initialize the Model Pipeline (No changes) ---
|
9 |
print("Loading MedGemma model...")
|
10 |
try:
|
11 |
-
# Using "image-to-text" is more robust for modern multimodal chat models.
|
12 |
pipe = pipeline(
|
13 |
"image-to-text",
|
14 |
model="google/medgemma-4b-it",
|
@@ -26,8 +25,8 @@ except Exception as e:
|
|
26 |
@spaces.GPU()
|
27 |
def analyze_symptoms(symptom_image: Image.Image, symptoms_text: str):
|
28 |
"""
|
29 |
-
Analyzes user's symptoms using the
|
30 |
-
for the MedGemma multimodal model.
|
31 |
"""
|
32 |
if not model_loaded:
|
33 |
return "Error: The AI model could not be loaded. Please check the Space logs."
|
@@ -37,9 +36,7 @@ def analyze_symptoms(symptom_image: Image.Image, symptoms_text: str):
|
|
37 |
return "Please describe your symptoms or upload an image for analysis."
|
38 |
|
39 |
try:
|
40 |
-
# ---
|
41 |
-
|
42 |
-
# 1. System Prompt: This sets the AI's persona and overall goal.
|
43 |
system_instruction = (
|
44 |
"You are an expert, empathetic AI medical assistant. "
|
45 |
"Analyze the potential medical condition based on the user's input. "
|
@@ -48,50 +45,41 @@ def analyze_symptoms(symptom_image: Image.Image, symptoms_text: str):
|
|
48 |
"the user provided."
|
49 |
)
|
50 |
|
51 |
-
# 2. User Content: This must be a list of dictionaries for multimodal input.
|
52 |
user_content = []
|
53 |
-
|
54 |
-
# The model requires some form of text. If the user provides none,
|
55 |
-
# we add a generic prompt to accompany the image.
|
56 |
text_to_send = symptoms_text if symptoms_text else "Please analyze this medical image."
|
57 |
user_content.append({"type": "text", "text": text_to_send})
|
58 |
|
59 |
-
# Add the image part if it exists.
|
60 |
if symptom_image:
|
61 |
user_content.append({"type": "image", "image": symptom_image})
|
62 |
|
63 |
-
# 3. Construct the full message list for the pipeline
|
64 |
messages = [
|
65 |
{"role": "system", "content": system_instruction},
|
66 |
{"role": "user", "content": user_content},
|
67 |
]
|
68 |
-
|
69 |
-
print("Generating pipeline output with chat format...")
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
#
|
74 |
-
|
75 |
-
|
76 |
-
max_new_tokens
|
77 |
-
do_sample
|
78 |
-
temperature
|
79 |
-
|
|
|
|
|
|
|
|
|
80 |
|
81 |
print("Pipeline Output:", output)
|
82 |
|
83 |
-
# ---
|
84 |
-
# The output from a chat-templated pipeline call is a list containing the full
|
85 |
-
# conversation history, including the newly generated assistant message.
|
86 |
if output and isinstance(output, list) and output[0].get('generated_text'):
|
87 |
-
# The generated_text contains the full conversation history
|
88 |
full_conversation = output[0]['generated_text']
|
89 |
-
# The last message in the list is the AI's response.
|
90 |
assistant_message = full_conversation[-1]
|
91 |
if assistant_message['role'] == 'assistant':
|
92 |
result = assistant_message['content']
|
93 |
else:
|
94 |
-
# Fallback in case the last message isn't from the assistant
|
95 |
result = str(assistant_message)
|
96 |
else:
|
97 |
result = "The model did not return a valid response. Please try again."
|
@@ -104,8 +92,7 @@ def analyze_symptoms(symptom_image: Image.Image, symptoms_text: str):
|
|
104 |
print(f"An exception occurred during analysis: {type(e).__name__}: {e}")
|
105 |
return f"An error occurred during analysis. Please check the logs for details: {str(e)}"
|
106 |
|
107 |
-
|
108 |
-
# --- Create the Gradio Interface (No changes needed) ---
|
109 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI Symptom Analyzer") as demo:
|
110 |
gr.HTML("""
|
111 |
<div style="text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 2rem; border-radius: 10px; margin-bottom: 2rem;">
|
@@ -144,4 +131,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Symptom Analyzer") as demo:
|
|
144 |
|
145 |
if __name__ == "__main__":
|
146 |
print("Starting Gradio interface...")
|
147 |
-
demo.launch(debug=True)
|
|
|
8 |
# --- Initialize the Model Pipeline (No changes) ---
|
9 |
print("Loading MedGemma model...")
|
10 |
try:
|
|
|
11 |
pipe = pipeline(
|
12 |
"image-to-text",
|
13 |
model="google/medgemma-4b-it",
|
|
|
25 |
@spaces.GPU()
|
26 |
def analyze_symptoms(symptom_image: Image.Image, symptoms_text: str):
|
27 |
"""
|
28 |
+
Analyzes user's symptoms using the recommended chat format and correct
|
29 |
+
parameter passing for the MedGemma multimodal model.
|
30 |
"""
|
31 |
if not model_loaded:
|
32 |
return "Error: The AI model could not be loaded. Please check the Space logs."
|
|
|
36 |
return "Please describe your symptoms or upload an image for analysis."
|
37 |
|
38 |
try:
|
39 |
+
# --- CHAT-BASED PROMPT LOGIC (Unchanged) ---
|
|
|
|
|
40 |
system_instruction = (
|
41 |
"You are an expert, empathetic AI medical assistant. "
|
42 |
"Analyze the potential medical condition based on the user's input. "
|
|
|
45 |
"the user provided."
|
46 |
)
|
47 |
|
|
|
48 |
user_content = []
|
|
|
|
|
|
|
49 |
text_to_send = symptoms_text if symptoms_text else "Please analyze this medical image."
|
50 |
user_content.append({"type": "text", "text": text_to_send})
|
51 |
|
|
|
52 |
if symptom_image:
|
53 |
user_content.append({"type": "image", "image": symptom_image})
|
54 |
|
|
|
55 |
messages = [
|
56 |
{"role": "system", "content": system_instruction},
|
57 |
{"role": "user", "content": user_content},
|
58 |
]
|
|
|
|
|
59 |
|
60 |
+
print("Generating pipeline output with chat format...")
|
61 |
+
|
62 |
+
# --- DEFINITIVE PIPELINE CALL ---
|
63 |
+
# All text-generation parameters must be nested within a 'generate_kwargs' dictionary.
|
64 |
+
generate_kwargs = {
|
65 |
+
"max_new_tokens": 512,
|
66 |
+
"do_sample": True,
|
67 |
+
"temperature": 0.7,
|
68 |
+
}
|
69 |
+
|
70 |
+
# The `messages` list is the primary argument.
|
71 |
+
# `generate_kwargs` is a dedicated keyword argument for generation options.
|
72 |
+
output = pipe(messages, generate_kwargs=generate_kwargs)
|
73 |
|
74 |
print("Pipeline Output:", output)
|
75 |
|
76 |
+
# --- OUTPUT PROCESSING (Unchanged) ---
|
|
|
|
|
77 |
if output and isinstance(output, list) and output[0].get('generated_text'):
|
|
|
78 |
full_conversation = output[0]['generated_text']
|
|
|
79 |
assistant_message = full_conversation[-1]
|
80 |
if assistant_message['role'] == 'assistant':
|
81 |
result = assistant_message['content']
|
82 |
else:
|
|
|
83 |
result = str(assistant_message)
|
84 |
else:
|
85 |
result = "The model did not return a valid response. Please try again."
|
|
|
92 |
print(f"An exception occurred during analysis: {type(e).__name__}: {e}")
|
93 |
return f"An error occurred during analysis. Please check the logs for details: {str(e)}"
|
94 |
|
95 |
+
# --- Gradio Interface (No changes needed) ---
|
|
|
96 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI Symptom Analyzer") as demo:
|
97 |
gr.HTML("""
|
98 |
<div style="text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 2rem; border-radius: 10px; margin-bottom: 2rem;">
|
|
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
print("Starting Gradio interface...")
|
134 |
+
demo.launch(debug=True)
|