Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,193 @@
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
-
import faiss
|
3 |
-
import streamlit as st
|
4 |
-
import numpy as np
|
5 |
-
from PIL import Image
|
6 |
from groq import Groq
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
61 |
response = groq_client.chat.completions.create(
|
|
|
62 |
model="mixtral-8x7b-32768",
|
63 |
-
|
64 |
-
|
65 |
-
{"role": "user", "content": f"Math Notes: {relevant_text}\n\nQuestion: {question}"}
|
66 |
-
],
|
67 |
)
|
68 |
return response.choices[0].message.content
|
69 |
|
70 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
|
85 |
-
question = st.text_input("π’ Ask a math question:")
|
86 |
-
if question:
|
87 |
-
with st.spinner("Thinking..."):
|
88 |
-
answer = generate_response(question)
|
89 |
-
st.write("### β¨ Answer:")
|
90 |
-
st.write(answer) # Display plain text answer
|
|
|
1 |
+
import gradio as gr
|
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 Keys
|
9 |
+
groq_api_key = os.getenv("groq_api_key")
|
10 |
+
groq_client = Groq(api_key=groq_api_key)
|
11 |
+
|
12 |
+
# β
Load Zero-Shot Disease Classification Model
|
13 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
14 |
+
|
15 |
+
# β
Medical Conditions List
|
16 |
+
conditions = [
|
17 |
+
"Asthma", "COPD", "Pneumonia", "Tuberculosis", "COVID-19", "Bronchitis",
|
18 |
+
"Heart Failure", "Hypertension", "Diabetes Type 1", "Diabetes Type 2",
|
19 |
+
"Migraine", "Gastroenteritis", "Anemia", "Depression", "Anxiety Disorder",
|
20 |
+
"Chronic Kidney Disease", "UTI", "Osteoporosis", "Psoriasis", "Epilepsy"
|
21 |
+
]
|
22 |
+
|
23 |
+
# β
Specialist Mapping
|
24 |
+
specialist_mapping = {
|
25 |
+
"Asthma": ("Pulmonologist", "Respiratory System"),
|
26 |
+
"COPD": ("Pulmonologist", "Respiratory System"),
|
27 |
+
"Pneumonia": ("Pulmonologist", "Respiratory System"),
|
28 |
+
"Tuberculosis": ("Infectious Disease Specialist", "Respiratory System"),
|
29 |
+
"COVID-19": ("Infectious Disease Specialist", "Immune System"),
|
30 |
+
"Heart Failure": ("Cardiologist", "Cardiovascular System"),
|
31 |
+
"Hypertension": ("Cardiologist", "Cardiovascular System"),
|
32 |
+
"Diabetes Type 1": ("Endocrinologist", "Endocrine System"),
|
33 |
+
"Diabetes Type 2": ("Endocrinologist", "Endocrine System"),
|
34 |
+
"Migraine": ("Neurologist", "Nervous System"),
|
35 |
+
"Gastroenteritis": ("Gastroenterologist", "Digestive System"),
|
36 |
+
"Anemia": ("Hematologist", "Blood Disorders"),
|
37 |
+
"Depression": ("Psychiatrist", "Mental Health"),
|
38 |
+
"Anxiety Disorder": ("Psychiatrist", "Mental Health"),
|
39 |
+
"Chronic Kidney Disease": ("Nephrologist", "Urinary System"),
|
40 |
+
"UTI": ("Urologist", "Urinary System"),
|
41 |
+
"Osteoporosis": ("Orthopedic Specialist", "Musculoskeletal System"),
|
42 |
+
"Psoriasis": ("Dermatologist", "Skin Disorders"),
|
43 |
+
"Epilepsy": ("Neurologist", "Nervous System")
|
44 |
+
}
|
45 |
+
|
46 |
+
# β
Translate Function
|
47 |
+
def translate_text(text, target_lang="en"):
|
48 |
+
try:
|
49 |
+
return GoogleTranslator(source="auto", target=target_lang).translate(text)
|
50 |
+
except Exception as e:
|
51 |
+
return f"Translation Error: {str(e)}"
|
52 |
+
|
53 |
+
# β
Expert AI Analysis
|
54 |
+
def generate_expert_analysis(condition, symptoms):
|
55 |
+
specialist_title = specialist_mapping.get(condition, ("General Physician", "General Medicine"))[0]
|
56 |
+
prompt = f"""As a {specialist_title.lower()}, explain {condition} to a patient experiencing these symptoms: "{symptoms}".
|
57 |
+
Structure the response into:
|
58 |
+
1. **Biological Process**
|
59 |
+
2. **Immediate Treatment**
|
60 |
+
3. **Long-term Care**
|
61 |
+
4. **Emergency Signs**
|
62 |
+
5. **Diet Plan**
|
63 |
+
Use professional yet simple language.
|
64 |
+
"""
|
65 |
response = groq_client.chat.completions.create(
|
66 |
+
messages=[{"role": "user", "content": prompt}],
|
67 |
model="mixtral-8x7b-32768",
|
68 |
+
temperature=0.5,
|
69 |
+
max_tokens=1024
|
|
|
|
|
70 |
)
|
71 |
return response.choices[0].message.content
|
72 |
|
73 |
+
# β
Medical Report Generator
|
74 |
+
def create_medical_report(symptoms):
|
75 |
+
try:
|
76 |
+
translated_symptoms = translate_text(symptoms, "en")
|
77 |
+
result = classifier(translated_symptoms, conditions, multi_label=False)
|
78 |
+
diagnosis = result['labels'][0]
|
79 |
+
specialist, system = specialist_mapping.get(diagnosis, ("General Physician", "General Medicine"))
|
80 |
+
expert_analysis = generate_expert_analysis(diagnosis, translated_symptoms)
|
81 |
+
|
82 |
+
full_report = (
|
83 |
+
f"**Medical Report**\n\n"
|
84 |
+
f"**Patient Symptoms:** {translated_symptoms}\n"
|
85 |
+
f"**Primary Diagnosis:** {diagnosis}\n"
|
86 |
+
f"**Affected System:** {system}\n"
|
87 |
+
f"**Consult:** {specialist}\n\n"
|
88 |
+
f"**Expert Analysis:**\n{expert_analysis}\n"
|
89 |
+
)
|
90 |
+
|
91 |
+
pdf = FPDF()
|
92 |
+
pdf.add_page()
|
93 |
+
pdf.set_font("Arial", size=12)
|
94 |
+
pdf.multi_cell(0, 10, full_report)
|
95 |
+
pdf.output("report.pdf")
|
96 |
+
|
97 |
+
return full_report, "report.pdf"
|
98 |
+
|
99 |
+
except Exception as e:
|
100 |
+
return f"Error generating report: {str(e)}", None
|
101 |
+
|
102 |
+
# β
Medical Chatbot Function
|
103 |
+
def medical_chatbot(user_input, chat_history):
|
104 |
+
system_prompt = """You are a compassionate psychiatric assistant named MedMind. Your role is to:
|
105 |
+
- Provide emotional support and stress relief techniques
|
106 |
+
- Offer evidence-based mental health advice
|
107 |
+
- Help users understand medical terminology
|
108 |
+
- Never diagnose but suggest professional help when needed
|
109 |
+
- Maintain therapeutic conversation flow
|
110 |
+
- Remember previous interactions for context"""
|
111 |
+
|
112 |
+
messages = [
|
113 |
+
{"role": "system", "content": system_prompt},
|
114 |
+
*[{"role": "user" if i%2==0 else "assistant", "content": msg}
|
115 |
+
for i, msg in enumerate(chat_history)]
|
116 |
+
]
|
117 |
+
|
118 |
+
if user_input.strip():
|
119 |
+
messages.append({"role": "user", "content": user_input})
|
120 |
+
|
121 |
+
response = groq_client.chat.completions.create(
|
122 |
+
messages=messages,
|
123 |
+
model="mixtral-8x7b-32768",
|
124 |
+
temperature=0.7,
|
125 |
+
max_tokens=500
|
126 |
+
)
|
127 |
+
|
128 |
+
bot_response = response.choices[0].message.content
|
129 |
+
chat_history.append((user_input, bot_response))
|
130 |
+
|
131 |
+
return "", chat_history
|
132 |
+
|
133 |
+
# β
Gradio UI with Sidebar
|
134 |
+
css = """
|
135 |
+
body { background-color: #f0f4f8; }
|
136 |
+
.sidebar { background-color: #2c3e50; padding: 20px; height: 100vh; color: white; }
|
137 |
+
.chat-container { height: 70vh; overflow-y: auto; }
|
138 |
+
.message { padding: 10px; margin: 5px; border-radius: 5px; }
|
139 |
+
.user-message { background-color: #e3f2fd; margin-left: 20%; }
|
140 |
+
.bot-message { background-color: #ffffff; margin-right: 20%; }
|
141 |
+
"""
|
142 |
+
|
143 |
+
with gr.Blocks(css=css) as interface:
|
144 |
+
chat_history = gr.State([])
|
145 |
+
|
146 |
+
with gr.Row():
|
147 |
+
with gr.Column(scale=1, elem_classes="sidebar"):
|
148 |
+
gr.Markdown("## MedExpert AI")
|
149 |
+
with gr.Group():
|
150 |
+
gr.Button("π Home", variant="secondary")
|
151 |
+
gr.Button("π©Ί Diagnostic", variant="secondary")
|
152 |
+
gr.Button("π¬ Mental Health Chat", variant="secondary")
|
153 |
+
|
154 |
+
with gr.Column(scale=4):
|
155 |
+
# Home Section
|
156 |
+
with gr.Group(visible=True) as home_section:
|
157 |
+
gr.Markdown("# Welcome to MedExpert AI")
|
158 |
+
gr.Markdown("Your intelligent medical assistant providing:")
|
159 |
+
gr.Markdown("- Symptom Analysis\n- Medical Reports\n- Mental Health Support")
|
160 |
+
|
161 |
+
# Diagnostic Section
|
162 |
+
with gr.Group(visible=False) as diagnostic_section:
|
163 |
+
symptoms_input = gr.Textbox(label="Describe Your Symptoms")
|
164 |
+
analyze_btn = gr.Button("Analyze Symptoms π₯", variant="primary")
|
165 |
+
report_output = gr.Textbox(label="Medical Report", interactive=False)
|
166 |
+
pdf_output = gr.File(label="Download PDF Report")
|
167 |
+
|
168 |
+
# Chatbot Section
|
169 |
+
with gr.Group(visible=False) as chatbot_section:
|
170 |
+
chatbot = gr.Chatbot(elem_classes="chat-container")
|
171 |
+
user_input = gr.Textbox(placeholder="Type your message here...", show_label=False)
|
172 |
+
send_btn = gr.Button("Send", variant="primary")
|
173 |
+
|
174 |
+
# Diagnostic Logic
|
175 |
+
analyze_btn.click(
|
176 |
+
create_medical_report,
|
177 |
+
inputs=[symptoms_input],
|
178 |
+
outputs=[report_output, pdf_output]
|
179 |
+
)
|
180 |
+
|
181 |
+
# Chatbot Logic
|
182 |
+
send_btn.click(
|
183 |
+
medical_chatbot,
|
184 |
+
inputs=[user_input, chat_history],
|
185 |
+
outputs=[user_input, chatbot]
|
186 |
+
)
|
187 |
+
user_input.submit(
|
188 |
+
medical_chatbot,
|
189 |
+
inputs=[user_input, chat_history],
|
190 |
+
outputs=[user_input, chatbot]
|
191 |
+
)
|
192 |
|
193 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|