-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (40 loc) 路 1.44 KB
/
Copy pathapp.py
File metadata and controls
51 lines (40 loc) 路 1.44 KB
1
2
3
4
5
6
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
40
41
42
43
44
45
46
47
48
49
50
51
# app.py
# HealthBot - Question Answering Health Assistant by Rishi Yadav
import streamlit as st
from transformers import pipeline
# Load health Q&A context
with open("context.txt", "r", encoding="utf-8") as f:
context = f.read()
# Load QA model
qa = pipeline("question-answering")
# Streamlit UI setup
st.set_page_config(page_title="馃┖ Health Chatbot", layout="wide")
st.title("馃┖ Health Chatbot")
st.markdown("Ask any general health question. The bot will answer using trusted health info.")
# Chat history
if "chat" not in st.session_state:
st.session_state.chat = []
# Display past messages
for msg in st.session_state.chat:
with st.chat_message(msg["role"]):
st.write(msg["content"])
# Response function
def get_answer(question):
if len(question.strip()) < 5:
return "Please ask a more complete question."
try:
result = qa(question=question, context=context)
return result["answer"]
except Exception as e:
return "Sorry, I couldn't find an answer."
# Take user input
user_q = st.chat_input("Enter your health question...")
if user_q:
st.session_state.chat.append({"role": "user", "content": user_q})
with st.chat_message("user"):
st.write(user_q)
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
reply = get_answer(user_q)
st.write(reply)
st.session_state.chat.append({"role": "assistant", "content": reply})