Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from fpdf import FPDF
|
4 |
+
import os
|
5 |
+
from groq import Groq
|
6 |
+
from deep_translator import GoogleTranslator
|
7 |
+
|
8 |
+
# Load API Key
|
9 |
+
groq_api_key = os.getenv("groq_api_key")
|
10 |
+
groq_client = Groq(api_key=groq_api_key)
|
11 |
+
|
12 |
+
# Translation Function
|
13 |
+
def translate_text(text, target_lang="en"):
|
14 |
+
try:
|
15 |
+
return GoogleTranslator(source="auto", target=target_lang).translate(text)
|
16 |
+
except Exception as e:
|
17 |
+
return f"Translation Error: {str(e)}"
|
18 |
+
|
19 |
+
# Load Model
|
20 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
21 |
+
|
22 |
+
# Medical Conditions
|
23 |
+
conditions = [
|
24 |
+
"Asthma", "COPD", "Pneumonia", "Tuberculosis", "COVID-19", "Bronchitis",
|
25 |
+
"Heart Failure", "Hypertension", "Diabetes Type 1", "Diabetes Type 2",
|
26 |
+
"Migraine", "Gastroenteritis", "Anemia", "Depression", "Anxiety Disorder",
|
27 |
+
"Chronic Kidney Disease", "UTI", "Osteoporosis", "Psoriasis", "Epilepsy"
|
28 |
+
]
|
29 |
+
|
30 |
+
# Specialist Mapping
|
31 |
+
specialist_mapping = {
|
32 |
+
"Asthma": ("Pulmonologist", "Respiratory System"),
|
33 |
+
"COPD": ("Pulmonologist", "Respiratory System"),
|
34 |
+
"Pneumonia": ("Pulmonologist", "Respiratory System"),
|
35 |
+
"Tuberculosis": ("Infectious Disease Specialist", "Respiratory System"),
|
36 |
+
"COVID-19": ("Infectious Disease Specialist", "Immune System"),
|
37 |
+
"Heart Failure": ("Cardiologist", "Cardiovascular System"),
|
38 |
+
"Hypertension": ("Cardiologist", "Cardiovascular System"),
|
39 |
+
"Diabetes Type 1": ("Endocrinologist", "Endocrine System"),
|
40 |
+
"Diabetes Type 2": ("Endocrinologist", "Endocrine System"),
|
41 |
+
"Migraine": ("Neurologist", "Nervous System"),
|
42 |
+
"Gastroenteritis": ("Gastroenterologist", "Digestive System"),
|
43 |
+
"Anemia": ("Hematologist", "Blood Disorders"),
|
44 |
+
"Depression": ("Psychiatrist", "Mental Health"),
|
45 |
+
"Anxiety Disorder": ("Psychiatrist", "Mental Health"),
|
46 |
+
"Chronic Kidney Disease": ("Nephrologist", "Urinary System"),
|
47 |
+
"UTI": ("Urologist", "Urinary System"),
|
48 |
+
"Osteoporosis": ("Orthopedic Specialist", "Musculoskeletal System"),
|
49 |
+
"Psoriasis": ("Dermatologist", "Skin Disorders"),
|
50 |
+
"Epilepsy": ("Neurologist", "Nervous System")
|
51 |
+
}
|
52 |
+
|
53 |
+
# Function to generate expert analysis
|
54 |
+
def generate_expert_analysis(condition, symptoms):
|
55 |
+
specialist_title = specialist_mapping.get(condition, ("General Physician", "General Medicine"))[0]
|
56 |
+
|
57 |
+
prompt = f"""As a {specialist_title.lower()}, explain {condition} to a patient experiencing these symptoms: "{symptoms}".
|
58 |
+
|
59 |
+
Structure the response into:
|
60 |
+
1. **Biological Process**
|
61 |
+
2. **Immediate Treatment**
|
62 |
+
3. **Long-term Care**
|
63 |
+
4. **Emergency Signs**
|
64 |
+
5. **Diet Plan**
|
65 |
+
|
66 |
+
Use professional yet simple language. **No AI disclaimers or generic advice**.
|
67 |
+
"""
|
68 |
+
|
69 |
+
response = groq_client.chat.completions.create(
|
70 |
+
messages=[{"role": "user", "content": prompt}],
|
71 |
+
model="mixtral-8x7b-32768",
|
72 |
+
temperature=0.5,
|
73 |
+
max_tokens=1024
|
74 |
+
)
|
75 |
+
|
76 |
+
return response.choices[0].message.content
|
77 |
+
|
78 |
+
# Function to generate medical report
|
79 |
+
def create_medical_report(symptoms):
|
80 |
+
try:
|
81 |
+
translated_symptoms = translate_text(symptoms, "en")
|
82 |
+
result = classifier(translated_symptoms, conditions, multi_label=False)
|
83 |
+
diagnosis = result['labels'][0]
|
84 |
+
specialist, system = specialist_mapping.get(diagnosis, ("General Physician", "General Medicine"))
|
85 |
+
|
86 |
+
expert_analysis = generate_expert_analysis(diagnosis, translated_symptoms)
|
87 |
+
|
88 |
+
full_report = (
|
89 |
+
f"**Medical Report**\n\n"
|
90 |
+
f"**Patient Symptoms:** {translated_symptoms}\n"
|
91 |
+
f"**Primary Diagnosis:** {diagnosis}\n"
|
92 |
+
f"**Affected System:** {system}\n"
|
93 |
+
f"**Consult:** {specialist}\n\n"
|
94 |
+
f"**Expert Analysis:**\n{expert_analysis}\n\n"
|
95 |
+
"**Key Questions for Your Doctor:**\n"
|
96 |
+
"1. Is this condition acute or chronic?\n"
|
97 |
+
"2. What medication options are suitable?\n"
|
98 |
+
"3. What lifestyle changes help manage this condition?\n"
|
99 |
+
"4. What warning signs require immediate attention?\n"
|
100 |
+
)
|
101 |
+
|
102 |
+
pdf = FPDF()
|
103 |
+
pdf.add_page()
|
104 |
+
pdf.set_font("Arial", size=12)
|
105 |
+
pdf.multi_cell(0, 10, full_report)
|
106 |
+
pdf.output("medical_report.pdf")
|
107 |
+
|
108 |
+
return full_report, "medical_report.pdf"
|
109 |
+
|
110 |
+
except Exception as e:
|
111 |
+
return f"Error generating report: {str(e)}", None
|
112 |
+
|
113 |
+
# Streamlit UI
|
114 |
+
st.set_page_config(page_title="MedExpert AI", layout="wide")
|
115 |
+
|
116 |
+
# Sidebar
|
117 |
+
st.sidebar.title("π Navigation")
|
118 |
+
page = st.sidebar.radio("Go to", ["π Home", "π Diagnosis", "π’ About"])
|
119 |
+
|
120 |
+
# Main Page
|
121 |
+
st.title("π©Ί MedExpert: AI-Powered Clinical Analysis System")
|
122 |
+
st.markdown("π‘ *Get AI-powered medical analysis & professional reports instantly!*")
|
123 |
+
|
124 |
+
# Home Page
|
125 |
+
if page == "π Home":
|
126 |
+
st.image("https://source.unsplash.com/800x400/?healthcare,hospital", use_column_width=True)
|
127 |
+
st.write("### Welcome to MedExpert!")
|
128 |
+
st.write("πΉ AI-based clinical diagnosis system for analyzing symptoms.")
|
129 |
+
st.write("πΉ Generates professional **medical reports** instantly.")
|
130 |
+
st.write("πΉ Provides **expert doctor recommendations**.")
|
131 |
+
st.write("πΉ Supports **multiple languages** (Urdu, English, Arabic, etc.).")
|
132 |
+
|
133 |
+
# Diagnosis Page
|
134 |
+
elif page == "π Diagnosis":
|
135 |
+
st.subheader("π Describe Your Symptoms")
|
136 |
+
symptoms = st.text_area("Enter symptoms in any language", placeholder="e.g., Mujhay sar main dard hai")
|
137 |
+
|
138 |
+
if st.button("π§ββοΈ Get Diagnosis"):
|
139 |
+
if symptoms.strip():
|
140 |
+
with st.spinner("π Analyzing Symptoms..."):
|
141 |
+
report_text, pdf_file = create_medical_report(symptoms)
|
142 |
+
|
143 |
+
if report_text:
|
144 |
+
st.subheader("π Medical Report")
|
145 |
+
st.write(report_text)
|
146 |
+
st.success("β
Report Generated Successfully!")
|
147 |
+
with open(pdf_file, "rb") as file:
|
148 |
+
st.download_button(label="π₯ Download Report", data=file, file_name="medical_report.pdf", mime="application/pdf")
|
149 |
+
else:
|
150 |
+
st.error("β Please enter symptoms before proceeding.")
|
151 |
+
|
152 |
+
# About Page
|
153 |
+
elif page == "π’ About":
|
154 |
+
st.write("### βΉοΈ About MedExpert")
|
155 |
+
st.write("This AI-powered medical system helps users get professional diagnosis and expert medical recommendations.")
|
156 |
+
|
157 |
+
# Footer
|
158 |
+
st.markdown("---")
|
159 |
+
st.write("π¨ββοΈ **Built with β€οΈ by AI & Healthcare Experts**")
|
160 |
+
|