Haseeb-001 commited on
Commit
25920f8
ยท
verified ยท
1 Parent(s): 6411f6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +168 -88
app.py CHANGED
@@ -1,93 +1,173 @@
1
  import os
2
- import json
3
- import time
4
- import speech_recognition as sr
5
- import pyttsx3
6
- from transformers import pipeline, AutoTokenizer, AutoModel
7
-
8
- # Initialize Text-to-Speech (TTS)
9
- tts_engine = pyttsx3.init()
10
- tts_engine.setProperty("rate", 150) # Adjust speaking speed
11
-
12
- def speak(text):
13
- """Convert text to speech."""
14
- tts_engine.say(text)
15
- tts_engine.runAndWait()
16
-
17
- # Initialize Speech Recognition (STT)
18
- recognizer = sr.Recognizer()
19
-
20
- def listen():
21
- """Capture user's voice input and convert to text."""
22
- with sr.Microphone() as source:
23
- print("๐ŸŽค Listening...")
24
- recognizer.adjust_for_ambient_noise(source)
25
- audio = recognizer.listen(source)
26
-
27
- try:
28
- user_input = recognizer.recognize_google(audio)
29
- print(f"User: {user_input}")
30
- return user_input
31
- except sr.UnknownValueError:
32
- speak("Sorry, I didn't catch that. Can you repeat?")
33
- return listen()
34
- except sr.RequestError:
35
- speak("There was an error with the speech recognition service.")
36
- return ""
37
-
38
- # Load Medical Model
 
 
39
  try:
40
- tokenizer = AutoTokenizer.from_pretrained("microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext")
41
- model = AutoModel.from_pretrained("microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext")
42
- medical_pipeline = pipeline('feature-extraction', model=model, tokenizer=tokenizer, device=-1)
43
  except Exception as e:
44
- speak("Error loading medical AI model. Using fallback response.")
45
- medical_pipeline = None
46
-
47
- # Store conversation in JSON
48
- conversation_data = {"user_info": {}, "medical_history": [], "chat": []}
49
-
50
- def save_conversation():
51
- with open("conversation.json", "w") as f:
52
- json.dump(conversation_data, f, indent=4)
53
-
54
- # Collect Basic User Info
55
- speak("Hello! I am your medical assistant. May I know your name?")
56
- user_name = listen()
57
- conversation_data["user_info"]["name"] = user_name
58
-
59
- speak(f"Nice to meet you, {user_name}. How old are you?")
60
- age = listen()
61
- conversation_data["user_info"]["age"] = age
62
-
63
- speak("What is your primary health concern today?")
64
- health_issue = listen()
65
- conversation_data["medical_history"].append(health_issue)
66
-
67
- # Real-Time Conversation Loop
68
- while True:
69
- speak("Tell me more about your symptoms or concerns.")
70
- user_query = listen()
71
-
72
- if "stop" in user_query.lower() or "bye" in user_query.lower():
73
- speak("Thank you for sharing your details. Take care!")
74
- break
75
-
76
- conversation_data["chat"].append({"user": user_query})
77
-
78
- # Process Medical Query
79
- if medical_pipeline:
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  try:
81
- embeddings = medical_pipeline(user_query)
82
- response = "Based on medical analysis, I recommend consulting a doctor for further evaluation."
83
- except Exception:
84
- response = "I couldn't analyze your query, but I suggest seeking medical advice."
 
 
 
 
 
 
 
 
 
 
 
 
85
  else:
86
- response = "I can assist with basic health guidance. Please consult a medical professional for proper diagnosis."
87
-
88
- conversation_data["chat"].append({"bot": response})
89
- speak(response)
90
- save_conversation()
91
-
92
- # Save final conversation
93
- save_conversation()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import streamlit as st
3
+ import numpy as np
4
+ import faiss
5
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModel
6
+ from groq import Groq
7
+ import sounddevice as sd
8
+ import soundfile as sf
9
+ import tempfile
10
+ import whisper
11
+
12
+ # Load API Key from Environment
13
+ groq_api_key = os.environ.get("GROQ_API_KEY")
14
+ if groq_api_key is None:
15
+ st.error("GROQ_API_KEY environment variable not set.")
16
+ st.stop()
17
+
18
+ # Initialize Groq Client
19
+ try:
20
+ client = Groq(api_key=groq_api_key)
21
+ except Exception as e:
22
+ st.error(f"Error initializing Groq client: {e}")
23
+ st.stop()
24
+
25
+ # Load PubMedBERT Model (Try Groq API first, then Hugging Face)
26
+ try:
27
+ pubmedbert_tokenizer = AutoTokenizer.from_pretrained("NeuML/pubmedbert-base-embeddings")
28
+ pubmedbert_model = AutoModel.from_pretrained("NeuML/pubmedbert-base-embeddings")
29
+ pubmedbert_pipeline = pipeline('feature-extraction', model=pubmedbert_model, tokenizer=pubmedbert_tokenizer, device=-1)
30
+ except Exception:
31
+ st.warning("Error loading PubMedBERT from Groq API. Using Hugging Face model.")
32
+ pubmedbert_tokenizer = AutoTokenizer.from_pretrained("microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext")
33
+ pubmedbert_model = AutoModelForSequenceClassification.from_pretrained("microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext")
34
+ pubmedbert_pipeline = pipeline('feature-extraction', model=pubmedbert_model, tokenizer=pubmedbert_tokenizer, device=-1)
35
+
36
+ # Initialize FAISS Index
37
+ embedding_dim = 768
38
+ index = faiss.IndexFlatL2(embedding_dim)
39
+
40
+ # Load Whisper model
41
  try:
42
+ whisper_model = whisper.load_model("base") # or "small", "medium", "large"
 
 
43
  except Exception as e:
44
+ st.error(f"Error loading Whisper model: {e}")
45
+ st.stop()
46
+
47
+ # Function to Check if Query is Related to Epilepsy
48
+ def preprocess_query(query):
49
+ tokens = query.lower().split()
50
+ epilepsy_keywords = ["seizure", "epilepsy", "convulsion", "neurology", "brain activity"]
51
+ is_epilepsy_related = any(k in tokens for k in epilepsy_keywords)
52
+ return tokens, is_epilepsy_related
53
+
54
+ # Function to Generate Response
55
+ def generate_response(user_query): # Removed chat_history argument
56
+ # Grammatical Correction using LLaMA (Hidden from User)
57
+ try:
58
+ correction_prompt = f"""
59
+ Correct the following user query for grammar and spelling errors, but keep the original intent intact.
60
+ Do not add or remove any information, just fix the grammar.
61
+ User Query: {user_query}
62
+ Corrected Query:
63
+ """
64
+ grammar_completion = client.chat.completions.create(
65
+ messages=[{"role": "user", "content": correction_prompt}],
66
+ model="llama-3.3-70b-versatile",
67
+ stream=False,
68
+ )
69
+ corrected_query = grammar_completion.choices[0].message.content.strip()
70
+ if not corrected_query:
71
+ corrected_query = user_query
72
+ except Exception as e:
73
+ corrected_query = user_query
74
+ print(f"โš ๏ธ Grammar correction error: {e}")
75
+
76
+ tokens, is_epilepsy_related = preprocess_query(corrected_query)
77
+
78
+ # Greeting Responses
79
+ greetings = ["hello", "hi", "hey"]
80
+ if any(word in tokens for word in greetings):
81
+ return "๐Ÿ‘‹ Hello! How can I assist you today?"
82
+
83
+ # Epilepsy Related Response
84
+ if is_epilepsy_related:
85
+ try:
86
+ pubmedbert_embeddings = pubmedbert_pipeline(corrected_query)
87
+ embedding_mean = np.mean(pubmedbert_embeddings[0], axis=0)
88
+ index.add(np.array([embedding_mean]))
89
+ pubmedbert_insights = "**PubMedBERT Analysis:** โœ… Query is relevant to epilepsy research."
90
+ except Exception as e:
91
+ pubmedbert_insights = f"โš ๏ธ Error during PubMedBERT analysis: {e}"
92
+
93
  try:
94
+ epilepsy_prompt = f"""
95
+ **User Query:** {corrected_query}
96
+ **Instructions:** Provide a concise, structured, and human-friendly response specifically about epilepsy or seizures.
97
+ """
98
+ chat_completion = client.chat.completions.create(
99
+ messages=[{"role": "user", "content": epilepsy_prompt}],
100
+ model="llama-3.3-70b-versatile",
101
+ stream=False,
102
+ )
103
+ model_response = chat_completion.choices[0].message.content.strip()
104
+ except Exception as e:
105
+ model_response = f"โš ๏ธ Error generating response with LLaMA: {e}"
106
+
107
+ return f"**NeuroGuard:** โœ… **Analysis:**\n{pubmedbert_insights}\n\n**Response:**\n{model_response}"
108
+
109
+ # General Health Response
110
  else:
111
+ try:
112
+ pubmedbert_embeddings = pubmedbert_pipeline(corrected_query)
113
+ embedding_mean = np.mean(pubmedbert_embeddings[0], axis=0)
114
+ index.add(np.array([embedding_mean]))
115
+ pubmedbert_insights = "**PubMedBERT Analysis:** PubMed analysis performed for health-related context."
116
+ except Exception as e:
117
+ pubmedbert_insights = f"โš ๏ธ Error during PubMedBERT analysis: {e}"
118
+
119
+ try:
120
+ general_health_prompt = f"""
121
+ **User Query:** {corrected_query}
122
+ **Instructions:** Provide a concise, structured, and human-friendly response to the general health query. If the query is clearly not health-related, respond generally.
123
+ """
124
+ chat_completion = client.chat.completions.create(
125
+ messages=[{"role": "user", "content": general_health_prompt}],
126
+ model="llama-3.3-70b-versatile",
127
+ stream=False,
128
+ )
129
+ model_response = chat_completion.choices[0].message.content.strip()
130
+ except Exception as e:
131
+ model_response = f"โš ๏ธ Error generating response with LLaMA: {e}"
132
+
133
+ return f"**NeuroGuard:** โœ… **Analysis:**\n{pubmedbert_insights}\n\n**Response:**\n{model_response}"
134
+
135
+ # Streamlit UI Setup
136
+ st.set_page_config(page_title="NeuroGuard: Voice Call Health Assistant", layout="wide")
137
+ st.title("๐Ÿ“ž NeuroGuard: Voice Call Health Assistant")
138
+ st.write("๐ŸŽ™๏ธ Click 'Start Recording', speak your question, and NeuroGuard will respond.")
139
+
140
+ bot_response_area = st.empty() # For bot response display
141
+
142
+ if st.button("Start Recording"):
143
+ st.write("Recording... Speak now.")
144
+ fs = 44100
145
+ seconds = 10
146
+ try:
147
+ myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
148
+ sd.wait()
149
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio:
150
+ sf.write(temp_audio.name, myrecording, fs)
151
+ audio_file = open(temp_audio.name, 'rb')
152
+ audio_bytes = audio_file.read()
153
+
154
+ st.audio(audio_bytes, format="audio/wav")
155
+
156
+ with st.spinner("Transcribing audio..."):
157
+ temp_audio_path = temp_audio.name
158
+ transcription = whisper_model.transcribe(temp_audio_path)
159
+ user_query = transcription["text"]
160
+
161
+ st.write(f"**You said:** {user_query}")
162
+
163
+ with bot_response_area.container():
164
+ with st.chat_message("bot"):
165
+ with st.spinner("๐Ÿค– Thinking..."):
166
+ try:
167
+ response = generate_response(user_query) # Removed chat_history argument
168
+ st.markdown(response)
169
+ except Exception as e:
170
+ st.error(f"โš ๏ธ Error processing query: {e}")
171
+
172
+ except Exception as e:
173
+ st.error(f"โš ๏ธ Recording error: {e}")