Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +1,173 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
from
|
7 |
-
|
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 |
try:
|
40 |
-
|
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 |
-
|
45 |
-
|
46 |
-
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
#
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
try:
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
else:
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}")
|