Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,14 @@ import json
|
|
| 4 |
from docx import Document
|
| 5 |
from io import BytesIO
|
| 6 |
import base64
|
| 7 |
-
|
| 8 |
|
| 9 |
# Pollinations AI Configuration
|
| 10 |
-
POLLINATIONS_API_URL = "https://text.pollinations.ai/openai
|
| 11 |
-
MODEL = "deepseek-r1"
|
| 12 |
-
|
| 13 |
|
| 14 |
def call_pollinations_ai(system_prompt, user_message):
|
| 15 |
-
"""Call Pollinations AI API"""
|
| 16 |
headers = {
|
| 17 |
"Content-Type": "application/json"
|
| 18 |
}
|
|
@@ -24,7 +23,7 @@ def call_pollinations_ai(system_prompt, user_message):
|
|
| 24 |
{"role": "user", "content": user_message}
|
| 25 |
],
|
| 26 |
"temperature": 0.1,
|
| 27 |
-
"max_tokens": 8000
|
| 28 |
}
|
| 29 |
|
| 30 |
try:
|
|
@@ -35,7 +34,6 @@ def call_pollinations_ai(system_prompt, user_message):
|
|
| 35 |
except Exception as e:
|
| 36 |
return f"Error calling API: {str(e)}"
|
| 37 |
|
| 38 |
-
|
| 39 |
def generate_diagnosis(symptoms, medical_history, age, gender, allergies, medications, family_history, lifestyle):
|
| 40 |
"""Generate medical diagnosis"""
|
| 41 |
|
|
@@ -43,29 +41,21 @@ def generate_diagnosis(symptoms, medical_history, age, gender, allergies, medica
|
|
| 43 |
Provide evidence-based preliminary diagnoses using patient information. Be thorough and analytical."""
|
| 44 |
|
| 45 |
user_message = f"""Analyze the patient's case and provide a preliminary diagnosis:
|
| 46 |
-
|
| 47 |
PATIENT DEMOGRAPHICS:
|
| 48 |
- Age: {age}
|
| 49 |
- Gender: {gender}
|
| 50 |
-
|
| 51 |
CURRENT SYMPTOMS:
|
| 52 |
{symptoms}
|
| 53 |
-
|
| 54 |
MEDICAL HISTORY:
|
| 55 |
{medical_history}
|
| 56 |
-
|
| 57 |
ALLERGIES:
|
| 58 |
{allergies if allergies else 'None reported'}
|
| 59 |
-
|
| 60 |
CURRENT MEDICATIONS:
|
| 61 |
{medications if medications else 'None reported'}
|
| 62 |
-
|
| 63 |
FAMILY HISTORY:
|
| 64 |
{family_history if family_history else 'None reported'}
|
| 65 |
-
|
| 66 |
LIFESTYLE FACTORS:
|
| 67 |
{lifestyle if lifestyle else 'Not provided'}
|
| 68 |
-
|
| 69 |
Please provide:
|
| 70 |
1. Preliminary diagnosis with possible conditions
|
| 71 |
2. List most likely conditions in order of probability
|
|
@@ -74,7 +64,6 @@ Please provide:
|
|
| 74 |
|
| 75 |
return call_pollinations_ai(system_prompt, user_message)
|
| 76 |
|
| 77 |
-
|
| 78 |
def generate_treatment_plan(symptoms, medical_history, age, gender, allergies, medications, family_history, diagnosis):
|
| 79 |
"""Generate treatment recommendations"""
|
| 80 |
|
|
@@ -82,26 +71,19 @@ def generate_treatment_plan(symptoms, medical_history, age, gender, allergies, m
|
|
| 82 |
Consider patient history, comorbidities, and current medical best practices. Focus on safe and effective recommendations."""
|
| 83 |
|
| 84 |
user_message = f"""Based on the following diagnosis and patient profile, create a comprehensive treatment plan:
|
| 85 |
-
|
| 86 |
PATIENT DEMOGRAPHICS:
|
| 87 |
- Age: {age}
|
| 88 |
- Gender: {gender}
|
| 89 |
-
|
| 90 |
PRELIMINARY DIAGNOSIS:
|
| 91 |
{diagnosis}
|
| 92 |
-
|
| 93 |
MEDICAL HISTORY:
|
| 94 |
{medical_history}
|
| 95 |
-
|
| 96 |
ALLERGIES:
|
| 97 |
{allergies if allergies else 'None reported'}
|
| 98 |
-
|
| 99 |
CURRENT MEDICATIONS:
|
| 100 |
{medications if medications else 'None reported'}
|
| 101 |
-
|
| 102 |
FAMILY HISTORY:
|
| 103 |
{family_history if family_history else 'None reported'}
|
| 104 |
-
|
| 105 |
Please provide:
|
| 106 |
1. Pharmacological treatment (specific medications and dosages)
|
| 107 |
2. Lifestyle modifications
|
|
@@ -113,7 +95,6 @@ Please provide:
|
|
| 113 |
|
| 114 |
return call_pollinations_ai(system_prompt, user_message)
|
| 115 |
|
| 116 |
-
|
| 117 |
def generate_docx_report(diagnosis, treatment_plan, patient_data):
|
| 118 |
"""Generate DOCX report"""
|
| 119 |
doc = Document()
|
|
@@ -137,7 +118,6 @@ def generate_docx_report(diagnosis, treatment_plan, patient_data):
|
|
| 137 |
bio.seek(0)
|
| 138 |
return bio
|
| 139 |
|
| 140 |
-
|
| 141 |
def process_medical_analysis(symptoms, medical_history, age, gender, allergies, medications, family_history, lifestyle):
|
| 142 |
"""Main processing function"""
|
| 143 |
|
|
@@ -163,7 +143,6 @@ def process_medical_analysis(symptoms, medical_history, age, gender, allergies,
|
|
| 163 |
|
| 164 |
return diagnosis, treatment_plan, download_link
|
| 165 |
|
| 166 |
-
|
| 167 |
# Gradio Interface
|
| 168 |
with gr.Blocks(title="Medical AI Assistant", theme=gr.themes.Soft()) as demo:
|
| 169 |
gr.Markdown("# 🏥 Medical AI Assistant")
|
|
@@ -227,6 +206,5 @@ with gr.Blocks(title="Medical AI Assistant", theme=gr.themes.Soft()) as demo:
|
|
| 227 |
outputs=[diagnosis_output, treatment_output, download_output]
|
| 228 |
)
|
| 229 |
|
| 230 |
-
|
| 231 |
if __name__ == "__main__":
|
| 232 |
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 4 |
from docx import Document
|
| 5 |
from io import BytesIO
|
| 6 |
import base64
|
| 7 |
+
from urllib.parse import quote
|
| 8 |
|
| 9 |
# Pollinations AI Configuration
|
| 10 |
+
POLLINATIONS_API_URL = "https://text.pollinations.ai/openai"
|
| 11 |
+
MODEL = "openai" # Changed from "deepseek-r1" to a supported model
|
|
|
|
| 12 |
|
| 13 |
def call_pollinations_ai(system_prompt, user_message):
|
| 14 |
+
"""Call Pollinations AI API using OpenAI-compatible POST endpoint"""
|
| 15 |
headers = {
|
| 16 |
"Content-Type": "application/json"
|
| 17 |
}
|
|
|
|
| 23 |
{"role": "user", "content": user_message}
|
| 24 |
],
|
| 25 |
"temperature": 0.1,
|
| 26 |
+
# "max_tokens": 8000 # Remove if not supported by Pollinations
|
| 27 |
}
|
| 28 |
|
| 29 |
try:
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error calling API: {str(e)}"
|
| 36 |
|
|
|
|
| 37 |
def generate_diagnosis(symptoms, medical_history, age, gender, allergies, medications, family_history, lifestyle):
|
| 38 |
"""Generate medical diagnosis"""
|
| 39 |
|
|
|
|
| 41 |
Provide evidence-based preliminary diagnoses using patient information. Be thorough and analytical."""
|
| 42 |
|
| 43 |
user_message = f"""Analyze the patient's case and provide a preliminary diagnosis:
|
|
|
|
| 44 |
PATIENT DEMOGRAPHICS:
|
| 45 |
- Age: {age}
|
| 46 |
- Gender: {gender}
|
|
|
|
| 47 |
CURRENT SYMPTOMS:
|
| 48 |
{symptoms}
|
|
|
|
| 49 |
MEDICAL HISTORY:
|
| 50 |
{medical_history}
|
|
|
|
| 51 |
ALLERGIES:
|
| 52 |
{allergies if allergies else 'None reported'}
|
|
|
|
| 53 |
CURRENT MEDICATIONS:
|
| 54 |
{medications if medications else 'None reported'}
|
|
|
|
| 55 |
FAMILY HISTORY:
|
| 56 |
{family_history if family_history else 'None reported'}
|
|
|
|
| 57 |
LIFESTYLE FACTORS:
|
| 58 |
{lifestyle if lifestyle else 'Not provided'}
|
|
|
|
| 59 |
Please provide:
|
| 60 |
1. Preliminary diagnosis with possible conditions
|
| 61 |
2. List most likely conditions in order of probability
|
|
|
|
| 64 |
|
| 65 |
return call_pollinations_ai(system_prompt, user_message)
|
| 66 |
|
|
|
|
| 67 |
def generate_treatment_plan(symptoms, medical_history, age, gender, allergies, medications, family_history, diagnosis):
|
| 68 |
"""Generate treatment recommendations"""
|
| 69 |
|
|
|
|
| 71 |
Consider patient history, comorbidities, and current medical best practices. Focus on safe and effective recommendations."""
|
| 72 |
|
| 73 |
user_message = f"""Based on the following diagnosis and patient profile, create a comprehensive treatment plan:
|
|
|
|
| 74 |
PATIENT DEMOGRAPHICS:
|
| 75 |
- Age: {age}
|
| 76 |
- Gender: {gender}
|
|
|
|
| 77 |
PRELIMINARY DIAGNOSIS:
|
| 78 |
{diagnosis}
|
|
|
|
| 79 |
MEDICAL HISTORY:
|
| 80 |
{medical_history}
|
|
|
|
| 81 |
ALLERGIES:
|
| 82 |
{allergies if allergies else 'None reported'}
|
|
|
|
| 83 |
CURRENT MEDICATIONS:
|
| 84 |
{medications if medications else 'None reported'}
|
|
|
|
| 85 |
FAMILY HISTORY:
|
| 86 |
{family_history if family_history else 'None reported'}
|
|
|
|
| 87 |
Please provide:
|
| 88 |
1. Pharmacological treatment (specific medications and dosages)
|
| 89 |
2. Lifestyle modifications
|
|
|
|
| 95 |
|
| 96 |
return call_pollinations_ai(system_prompt, user_message)
|
| 97 |
|
|
|
|
| 98 |
def generate_docx_report(diagnosis, treatment_plan, patient_data):
|
| 99 |
"""Generate DOCX report"""
|
| 100 |
doc = Document()
|
|
|
|
| 118 |
bio.seek(0)
|
| 119 |
return bio
|
| 120 |
|
|
|
|
| 121 |
def process_medical_analysis(symptoms, medical_history, age, gender, allergies, medications, family_history, lifestyle):
|
| 122 |
"""Main processing function"""
|
| 123 |
|
|
|
|
| 143 |
|
| 144 |
return diagnosis, treatment_plan, download_link
|
| 145 |
|
|
|
|
| 146 |
# Gradio Interface
|
| 147 |
with gr.Blocks(title="Medical AI Assistant", theme=gr.themes.Soft()) as demo:
|
| 148 |
gr.Markdown("# 🏥 Medical AI Assistant")
|
|
|
|
| 206 |
outputs=[diagnosis_output, treatment_output, download_output]
|
| 207 |
)
|
| 208 |
|
|
|
|
| 209 |
if __name__ == "__main__":
|
| 210 |
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|