Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,96 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import streamlit as st
|
3 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
4 |
-
import numpy as np
|
5 |
-
from groq import Groq
|
6 |
-
|
7 |
-
groq_api_key = os.environ.get("GROQ_API_KEY")
|
8 |
-
if groq_api_key is None:
|
9 |
-
raise ValueError("GROQ_API_KEY environment variable not set.")
|
10 |
-
|
11 |
-
client = Groq(api_key=groq_api_key)
|
12 |
-
|
13 |
-
# Load PubMedBERT for medical queries and embeddings
|
14 |
-
pubmedbert_tokenizer = AutoTokenizer.from_pretrained("microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext")
|
15 |
-
pubmedbert_model = AutoModelForSequenceClassification.from_pretrained("microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext")
|
16 |
-
pubmedbert_pipeline = pipeline('feature-extraction', model=pubmedbert_model, tokenizer=pubmedbert_tokenizer, device=-1) # Use device=-1 for CPU
|
17 |
-
|
18 |
-
# Function to preprocess and understand user query
|
19 |
-
def preprocess_query(query):
|
20 |
-
tokens = query.lower().split()
|
21 |
-
keywords = [keyword for keyword in tokens if keyword in ["seizure", "symptoms", "jerks", "confusion", "epilepsy"]] # Example keywords
|
22 |
-
is_medical_related = any(keyword in keywords for keyword in ["seizure", "symptoms", "jerks", "confusion", "epilepsy", "medical"]) # More comprehensive medical keyword check
|
23 |
-
return tokens, keywords, is_medical_related
|
24 |
-
|
25 |
-
# Function to generate response (modified for structured output)
|
26 |
-
def generate_response(user_query):
|
27 |
-
"""
|
28 |
-
Generates a structured response based on the user query, using PubMedBERT for medical analysis when relevant,
|
29 |
-
and Groq for general conversation. Datasets and FAISS are removed in this version.
|
30 |
-
"""
|
31 |
-
tokens, keywords, is_medical_related = preprocess_query(user_query)
|
32 |
-
|
33 |
-
# Enhanced Query (for demonstration - can be improved further)
|
34 |
-
enhanced_query = " ".join(tokens) # Basic enhancement - can add synonym expansion etc.
|
35 |
-
|
36 |
-
symptom_insights = "" # FAISS retrieval removed
|
37 |
-
|
38 |
-
if is_medical_related:
|
39 |
-
# Use PubMedBERT for medical-related queries
|
40 |
-
pubmedbert_embeddings = pubmedbert_pipeline(user_query)
|
41 |
-
embedding_mean = np.mean(pubmedbert_embeddings[0], axis=0) # Calculate mean embedding
|
42 |
-
|
43 |
-
pubmedbert_insights = "PubMedBERT analysis: (Embedding vector calculated for medical insight. Note: This version uses feature extraction for analysis and keyword-based recommendations, not direct seizure prediction. For specific predictions or detailed remedies, further fine-tuning and specialized models would be required.)"
|
44 |
-
|
45 |
-
model_name = "PubMedBERT"
|
46 |
-
model_response = pubmedbert_insights
|
47 |
-
|
48 |
-
if "seizure" in keywords or "symptoms" in keywords:
|
49 |
-
remedy_recommendations = "\n\n**General Recommendations & Remedies (Keyword-Based):**\n"
|
50 |
-
remedy_recommendations += "- **Important:** Consult a neurologist for accurate diagnosis and personalized treatment.\n"
|
51 |
-
remedy_recommendations += "- Describe your symptoms in detail to your doctor, including frequency, triggers, and duration.\n"
|
52 |
-
remedy_recommendations += "- Maintain a seizure diary to track events and potential triggers.\n"
|
53 |
-
remedy_recommendations += "- Ensure adequate sleep and stress management as potential seizure triggers.\n"
|
54 |
-
remedy_recommendations += "- Follow your neurologist's advice regarding medication and lifestyle adjustments.\n"
|
55 |
-
else:
|
56 |
-
remedy_recommendations = ""
|
57 |
-
else:
|
58 |
-
# Use LLaMA 2 or Mistral 7B for general conversation (via Groq API)
|
59 |
-
model_name = "LLaMA 2 / Mistral 7B (via Groq)"
|
60 |
-
try:
|
61 |
-
chat_completion = client.chat.completions.create(
|
62 |
-
messages=[{"role": "user", "content": user_query}],
|
63 |
-
model="llama-3.3-70b-versatile", # Or try "mistral-8x7b-32768"
|
64 |
-
stream=False,
|
65 |
-
)
|
66 |
-
model_response = chat_completion.choices[0].message.content.strip()
|
67 |
-
except Exception as e:
|
68 |
-
model_response = f"Error from Groq model: {e}"
|
69 |
-
remedy_recommendations = ""
|
70 |
-
|
71 |
-
|
72 |
-
# Merging insights and generating final structured response
|
73 |
-
final_response = f"**Enhanced Query:** {enhanced_query}\n\n" # Section 1: Enhanced Query
|
74 |
-
final_response += f"**Chatbot Analysis:**\n"
|
75 |
-
final_response += f"- Query Type: {'Medical-related' if is_medical_related else 'General Conversation'}\n"
|
76 |
-
final_response += f"- Model Used: {model_name}\n"
|
77 |
-
final_response += f"- Keywords detected: {', '.join(keywords) if keywords else 'None'}\n" # Keywords section
|
78 |
-
|
79 |
-
final_response += symptom_insights # Symptom insights removed from response
|
80 |
-
final_response += f"\n**Model Response/Insights:**\n{model_response}\n" # Model-specific insights
|
81 |
-
final_response += remedy_recommendations # Section 3: Remedy/Recommendations
|
82 |
-
|
83 |
-
return final_response
|
84 |
-
|
85 |
-
# Streamlit Interface
|
86 |
-
st.title("Epilepsy & Seizure Chatbot (PubMedBERT & Groq)")
|
87 |
-
st.write("Ask questions related to epilepsy and seizures. This chatbot uses PubMedBERT for medical analysis and Groq (LLaMA 2/Mistral) for general conversation. Datasets and FAISS similarity search have been removed.")
|
88 |
-
|
89 |
-
user_query = st.text_area("Enter your query here:", placeholder="I'm experiencing sudden muscle jerks and confusion. What could this mean?")
|
90 |
-
|
91 |
-
if st.button("Get Response"):
|
92 |
-
if user_query:
|
93 |
-
response = generate_response(user_query)
|
94 |
-
st.markdown(response) # Use st.markdown to render formatted response
|
95 |
-
else:
|
96 |
-
st.warning("Please enter a query.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|