Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from groq import Groq
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
+
|
| 10 |
+
# Initialize Groq client
|
| 11 |
+
client = Groq(api_key=API_KEY)
|
| 12 |
+
|
| 13 |
+
# Define health topics
|
| 14 |
+
HEALTH_TOPICS = [
|
| 15 |
+
"Coronary artery disease", "Heart attack", "Hypertension", "Stroke",
|
| 16 |
+
"Diabetes", "Obesity", "Thyroid disorders", "Cancer", "Depression",
|
| 17 |
+
"Anxiety", "Asthma", "Pneumonia", "IBS", "Kidney disease", "Arthritis",
|
| 18 |
+
"HIV/AIDS", "Flu", "COVID-19", "Skin conditions", "Hair loss"
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
# Function to check if a question is health-related
|
| 22 |
+
def is_health_related(question):
|
| 23 |
+
return any(topic.lower() in question.lower() for topic in HEALTH_TOPICS)
|
| 24 |
+
|
| 25 |
+
# Function to get AI response
|
| 26 |
+
def get_response(question):
|
| 27 |
+
if not is_health_related(question):
|
| 28 |
+
return "❌ Sorry, I only answer health-related questions."
|
| 29 |
+
|
| 30 |
+
response = client.chat.completions.create(
|
| 31 |
+
model="llama-3.3-70b-versatile",
|
| 32 |
+
messages=[{"role": "user", "content": question}],
|
| 33 |
+
temperature=1,
|
| 34 |
+
max_tokens=1024,
|
| 35 |
+
top_p=1,
|
| 36 |
+
stream=False,
|
| 37 |
+
)
|
| 38 |
+
return response.choices[0].message.content
|
| 39 |
+
|
| 40 |
+
# Streamlit UI
|
| 41 |
+
st.set_page_config(page_title="Health Assistant Chatbot", layout="wide")
|
| 42 |
+
st.title("🩺 AI Health Assistant Chatbot")
|
| 43 |
+
|
| 44 |
+
# User input
|
| 45 |
+
user_input = st.text_area("Ask a health-related question:")
|
| 46 |
+
|
| 47 |
+
# Process user input
|
| 48 |
+
if st.button("Get Answer"):
|
| 49 |
+
if user_input:
|
| 50 |
+
with st.spinner("Thinking..."):
|
| 51 |
+
answer = get_response(user_input)
|
| 52 |
+
st.success(answer)
|
| 53 |
+
else:
|
| 54 |
+
st.warning("Please enter a health-related question.")
|
| 55 |
+
|
| 56 |
+
# Upload and transcribe audio
|
| 57 |
+
st.header("🎙️ Ask via Voice")
|
| 58 |
+
audio_file = st.file_uploader("Upload your question (M4A, MP3, WAV)", type=["m4a", "mp3", "wav"])
|
| 59 |
+
|
| 60 |
+
if audio_file:
|
| 61 |
+
with open("temp_audio.m4a", "wb") as f:
|
| 62 |
+
f.write(audio_file.read())
|
| 63 |
+
|
| 64 |
+
with st.spinner("Transcribing..."):
|
| 65 |
+
with open("temp_audio.m4a", "rb") as file:
|
| 66 |
+
transcription = client.audio.transcriptions.create(
|
| 67 |
+
file=("temp_audio.m4a", file.read()),
|
| 68 |
+
model="whisper-large-v3-turbo",
|
| 69 |
+
response_format="verbose_json",
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
transcript_text = transcription.text
|
| 73 |
+
st.success(f"Transcription: {transcript_text}")
|
| 74 |
+
|
| 75 |
+
# Auto-answer transcribed question
|
| 76 |
+
if transcript_text:
|
| 77 |
+
answer = get_response(transcript_text)
|
| 78 |
+
st.success(f"AI Response: {answer}")
|