Spaces:
Sleeping
Sleeping
Update components/palm_summarizer.py
Browse files
components/palm_summarizer.py
CHANGED
@@ -1,24 +1,48 @@
|
|
1 |
import os
|
2 |
import google.generativeai as genai
|
3 |
|
4 |
-
# Load API key
|
5 |
genai.configure(api_key=os.getenv("PALM_API_KEY"))
|
6 |
|
7 |
-
#
|
8 |
-
model = genai.GenerativeModel("gemini-pro"
|
9 |
|
10 |
def summarize_with_palm(text):
|
11 |
try:
|
|
|
12 |
prompt = f"""
|
13 |
-
You are a medical assistant. Please
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
|
18 |
Report Text:
|
19 |
{text}
|
|
|
|
|
20 |
"""
|
|
|
|
|
21 |
response = model.generate_content(prompt)
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
except Exception as e:
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import google.generativeai as genai
|
3 |
|
4 |
+
# Load API key - Use consistent environment variable name
|
5 |
genai.configure(api_key=os.getenv("PALM_API_KEY"))
|
6 |
|
7 |
+
# Updated model name and configuration
|
8 |
+
model = genai.GenerativeModel("gemini-1.5-flash") # or "gemini-1.5-pro"
|
9 |
|
10 |
def summarize_with_palm(text):
|
11 |
try:
|
12 |
+
# Enhanced prompt for medical reports
|
13 |
prompt = f"""
|
14 |
+
You are a medical assistant specializing in health report analysis. Please analyze the following medical/lab report and provide:
|
15 |
+
|
16 |
+
1. **Summary**: A concise 2-3 line summary of the overall health status
|
17 |
+
2. **Key Findings**: List the most important test results and their significance
|
18 |
+
3. **Abnormal Values**: Highlight any values outside normal ranges with simple explanations
|
19 |
+
4. **Health Recommendations**: Basic health advice based on the results (always recommend consulting a doctor for medical decisions)
|
20 |
+
|
21 |
+
Important: Use simple, patient-friendly language. Avoid complex medical jargon.
|
22 |
|
23 |
Report Text:
|
24 |
{text}
|
25 |
+
|
26 |
+
Please format your response clearly with the above sections.
|
27 |
"""
|
28 |
+
|
29 |
+
# Generate content with the updated API
|
30 |
response = model.generate_content(prompt)
|
31 |
+
|
32 |
+
# Check if response is valid
|
33 |
+
if response and response.text:
|
34 |
+
return response.text.strip()
|
35 |
+
else:
|
36 |
+
return "❌ No summary could be generated from the report."
|
37 |
+
|
38 |
except Exception as e:
|
39 |
+
# Better error handling with specific error types
|
40 |
+
error_msg = str(e)
|
41 |
+
if "API_KEY" in error_msg.upper():
|
42 |
+
return "❌ API Key error: Please check your PALM_API_KEY environment variable."
|
43 |
+
elif "QUOTA" in error_msg.upper():
|
44 |
+
return "❌ API Quota exceeded: Please check your Google AI Studio quota."
|
45 |
+
elif "404" in error_msg:
|
46 |
+
return "❌ Model not found: Please verify the model name and API access."
|
47 |
+
else:
|
48 |
+
return f"❌ Summarization error: {error_msg}"
|