Hasnain-Ali commited on
Commit
6f4c3f1
Β·
verified Β·
1 Parent(s): 6bd2f9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +185 -82
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 sentence_transformers import SentenceTransformer
8
- import easyocr # βœ… Use EasyOCR instead of Tesseract
9
-
10
- # Initialize OCR reader
11
- reader = easyocr.Reader(["en"]) # Supports multiple languages like ['en', 'ar', 'fr']
12
-
13
- # Initialize Sentence Transformer Model
14
- embed_model = SentenceTransformer("all-MiniLM-L6-v2")
15
-
16
- # Initialize FAISS Vector Database
17
- d = 384 # Embedding dimension
18
- index = faiss.IndexFlatL2(d)
19
-
20
- # Store Extracted Texts
21
- text_data = []
22
-
23
- # Load Groq API Key
24
- GROQ_API_KEY = os.getenv("mathGpt_groq")
25
- if GROQ_API_KEY == "your_api_key_here":
26
- st.warning("⚠️ Please set your GROQ_API_KEY in Hugging Face secrets.")
27
-
28
- groq_client = Groq(api_key=GROQ_API_KEY)
29
-
30
- # Function to Process Image and Extract Text using EasyOCR
31
- def process_image(image):
32
- image = np.array(image) # Convert PIL image to NumPy array
33
- results = reader.readtext(image, detail=0) # Extract text only
34
- return " ".join(results).strip()
35
-
36
- # Function to Add Text to Vector Database
37
- def add_text_to_db(text):
38
- if text:
39
- text_data.append(text)
40
- embedding = embed_model.encode([text])
41
- index.add(np.array(embedding, dtype=np.float32))
42
-
43
- # Function to Retrieve Top-K Relevant Texts
44
- def retrieve_relevant_texts(query, k=3):
45
- if not text_data:
46
- return "No notes uploaded yet."
47
-
48
- query_embedding = embed_model.encode([query])
49
- distances, indices = index.search(np.array(query_embedding, dtype=np.float32), k)
50
-
51
- relevant_texts = []
52
- for i in indices[0]:
53
- if i < len(text_data):
54
- relevant_texts.append(text_data[i])
55
-
56
- return "\n\n".join(relevant_texts) if relevant_texts else "No relevant information found."
57
-
58
- # Function to Generate Response with Groq API
59
- def generate_response(question):
60
- relevant_text = retrieve_relevant_texts(question)
 
 
 
 
 
61
  response = groq_client.chat.completions.create(
 
62
  model="mixtral-8x7b-32768",
63
- messages=[
64
- {"role": "system", "content": "You are a math tutor. Answer based on the provided notes."},
65
- {"role": "user", "content": f"Math Notes: {relevant_text}\n\nQuestion: {question}"}
66
- ],
67
  )
68
  return response.choices[0].message.content
69
 
70
- # Streamlit Frontend
71
- st.title("πŸ“– Math Notes Chatbot πŸ€–")
72
-
73
- # Multi-File Uploader
74
- uploaded_files = st.file_uploader("Upload Handwritten Math Notes (Images)", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
75
-
76
- if uploaded_files:
77
- with st.spinner("Processing images..."):
78
- for uploaded_file in uploaded_files:
79
- image = Image.open(uploaded_file)
80
- extracted_text = process_image(image)
81
- add_text_to_db(extracted_text)
82
- st.success(f"βœ… {len(uploaded_files)} images processed and stored!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- # Question Input
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()