Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# app.py
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
# Initialize the BioGPT model using the Hugging Face pipeline
|
8 |
+
generator = pipeline("text-generation", model="microsoft/BioGPT")
|
9 |
+
|
10 |
+
# Streamlit app title and description
|
11 |
+
st.title("24/7Dr. Health Chatbot")
|
12 |
+
st.markdown("""
|
13 |
+
This is a health chatbot that can provide responses based on the symptoms you describe.
|
14 |
+
It uses a medical GPT model to generate responses and help guide your understanding.
|
15 |
+
""")
|
16 |
+
|
17 |
+
# Initialize session state for conversation history if it does not exist
|
18 |
+
if 'history' not in st.session_state:
|
19 |
+
st.session_state.history = []
|
20 |
+
|
21 |
+
# Function to generate chatbot responses using BioGPT
|
22 |
+
def generate_medical_response(user_input):
|
23 |
+
"""
|
24 |
+
Generates a response using BioGPT model based on user input (symptoms).
|
25 |
+
|
26 |
+
Args:
|
27 |
+
user_input (str): The symptoms or health-related query from the user.
|
28 |
+
|
29 |
+
Returns:
|
30 |
+
str: The generated response from the BioGPT model.
|
31 |
+
"""
|
32 |
+
response = generator(user_input,
|
33 |
+
max_length=150,
|
34 |
+
num_return_sequences=1,
|
35 |
+
pad_token_id=50256,
|
36 |
+
truncation=True,
|
37 |
+
temperature=0.7,
|
38 |
+
top_k=50,
|
39 |
+
top_p=0.95)
|
40 |
+
return response[0]['generated_text']
|
41 |
+
|
42 |
+
def display_conversation_history():
|
43 |
+
"""Display the conversation history in the app."""
|
44 |
+
if st.session_state.history:
|
45 |
+
st.subheader("Conversation History")
|
46 |
+
for message in st.session_state.history:
|
47 |
+
st.write(message)
|
48 |
+
|
49 |
+
def main():
|
50 |
+
"""Main function to run the Streamlit app."""
|
51 |
+
|
52 |
+
# Input box for user to describe symptoms
|
53 |
+
user_input = st.text_input("Describe your symptoms:")
|
54 |
+
|
55 |
+
# When the 'Ask' button is pressed
|
56 |
+
if st.button("Ask"):
|
57 |
+
if user_input: # Check if user input is not empty
|
58 |
+
# Store the user's input in the conversation history
|
59 |
+
st.session_state.history.append(f"You: {user_input}")
|
60 |
+
|
61 |
+
# Generate the chatbot's response using BioGPT
|
62 |
+
bot_response = generate_medical_response(user_input)
|
63 |
+
|
64 |
+
# Store the chatbot's response in the conversation history
|
65 |
+
st.session_state.history.append(f"Bot: {bot_response}")
|
66 |
+
|
67 |
+
# Clear the input box after submission (optional for improved UX)
|
68 |
+
st.text_input("Describe your symptoms:", "", key="clear_input")
|
69 |
+
|
70 |
+
# Display the conversation history on the Streamlit app
|
71 |
+
display_conversation_history()
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
main()
|