File size: 2,600 Bytes
14e2f15
 
 
 
 
fb56228
14e2f15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

# app.py

import streamlit as st
from transformers import pipeline
import torch

# Initialize the BioGPT model using the Hugging Face pipeline
generator = pipeline("text-generation", model="microsoft/BioGPT")

# Streamlit app title and description
st.title("24/7Dr. Health Chatbot")
st.markdown("""
    This is a health chatbot that can provide responses based on the symptoms you describe.
    It uses a medical GPT model to generate responses and help guide your understanding.
    """)

# Initialize session state for conversation history if it does not exist
if 'history' not in st.session_state:
    st.session_state.history = []

# Function to generate chatbot responses using BioGPT
def generate_medical_response(user_input):
    """
    Generates a response using BioGPT model based on user input (symptoms).
    
    Args:
        user_input (str): The symptoms or health-related query from the user.
        
    Returns:
        str: The generated response from the BioGPT model.
    """
    response = generator(user_input,
                         max_length=150,
                         num_return_sequences=1,
                         pad_token_id=50256,
                         truncation=True,
                         temperature=0.7,
                         top_k=50,
                         top_p=0.95)
    return response[0]['generated_text']

def display_conversation_history():
    """Display the conversation history in the app."""
    if st.session_state.history:
        st.subheader("Conversation History")
        for message in st.session_state.history:
            st.write(message)

def main():
    """Main function to run the Streamlit app."""
    
    # Input box for user to describe symptoms
    user_input = st.text_input("Describe your symptoms:")

    # When the 'Ask' button is pressed
    if st.button("Ask"):
        if user_input:  # Check if user input is not empty
            # Store the user's input in the conversation history
            st.session_state.history.append(f"You: {user_input}")

            # Generate the chatbot's response using BioGPT
            bot_response = generate_medical_response(user_input)

            # Store the chatbot's response in the conversation history
            st.session_state.history.append(f"Bot: {bot_response}")

            # Clear the input box after submission (optional for improved UX)
            st.text_input("Describe your symptoms:", "", key="clear_input")

    # Display the conversation history on the Streamlit app
    display_conversation_history()

if __name__ == "__main__":
    main()