Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load API key from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 9 |
+
|
| 10 |
+
# Initialize the Groq client
|
| 11 |
+
client = Groq(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
# Define the health issues for the chatbot
|
| 14 |
+
health_issues = [
|
| 15 |
+
"fever", "malaria", "skin infection", "flu", "cough", "diabetes", "hypertension",
|
| 16 |
+
"cold", "asthma", "arthritis", "chickenpox", "measles", "pneumonia", "migraine",
|
| 17 |
+
"anxiety", "depression", "heart disease", "cancer", "high cholesterol", "stroke",
|
| 18 |
+
"obesity", "insomnia", "constipation", "acid reflux", "eczema", "psoriasis",
|
| 19 |
+
"allergy", "cold sores", "chronic fatigue", "UTI", "kidney disease", "hepatitis",
|
| 20 |
+
"tuberculosis", "stomach ulcers", "gout", "HIV/AIDS", "malnutrition", "blood pressure",
|
| 21 |
+
"menstrual irregularities", "pregnancy", "anemia", "vitamin deficiency", "chronic pain"
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Function to fetch chatbot completion from Groq API
|
| 26 |
+
def get_response(query):
|
| 27 |
+
completion = client.chat.completions.create(
|
| 28 |
+
model="llama-3.3-70b-versatile",
|
| 29 |
+
messages=[{"role": "user", "content": query}],
|
| 30 |
+
temperature=0.7,
|
| 31 |
+
max_completion_tokens=1024,
|
| 32 |
+
top_p=1,
|
| 33 |
+
)
|
| 34 |
+
response = completion.choices[0].message.content
|
| 35 |
+
return response
|
| 36 |
+
|
| 37 |
+
def main():
|
| 38 |
+
st.title("Health Assistant Chatbot")
|
| 39 |
+
|
| 40 |
+
# Let the user choose a health topic or type a custom query
|
| 41 |
+
topic = st.selectbox("Choose a health issue", health_issues)
|
| 42 |
+
user_input = st.text_area("Or ask a health-related question:", "")
|
| 43 |
+
|
| 44 |
+
# If the user provides a query, we use that
|
| 45 |
+
query = user_input if user_input else f"Tell me about {topic}"
|
| 46 |
+
|
| 47 |
+
# Create a submit button
|
| 48 |
+
submit_button = st.button("Submit")
|
| 49 |
+
|
| 50 |
+
# Call the Groq API to get the response if the button is clicked
|
| 51 |
+
if submit_button and query:
|
| 52 |
+
response = get_response(query)
|
| 53 |
+
|
| 54 |
+
# Display the response
|
| 55 |
+
st.write("### Response:")
|
| 56 |
+
st.write(response)
|
| 57 |
+
|
| 58 |
+
# Handle unrelated queries
|
| 59 |
+
if user_input and not any(health_issue in user_input.lower() for health_issue in health_issues):
|
| 60 |
+
st.write("Sorry, I can only answer health-related questions.")
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
main()
|