Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import nltk
|
| 4 |
+
from nltk.corpus import stopwords
|
| 5 |
+
from nltk.tokenize import word_tokenize
|
| 6 |
+
|
| 7 |
+
# Download necessary NLTK data
|
| 8 |
+
nltk.download('punkt')
|
| 9 |
+
nltk.download('stopwords')
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Load a pre-trained Hugging Face model
|
| 13 |
+
chatbot = pipeline("text-generation", model="distilgpt2")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# Define healthcare-specific response logic (or use a model to generate responses)
|
| 17 |
+
def healthcare_chatbot(user_input):
|
| 18 |
+
# Simple rule-based keywords to respond
|
| 19 |
+
if "symptom" in user_input:
|
| 20 |
+
return "It seems like you're experiencing symptoms. Please consult a doctor for accurate advice."
|
| 21 |
+
elif "appointment" in user_input:
|
| 22 |
+
return "Would you like me to schedule an appointment with a doctor?"
|
| 23 |
+
elif "medication" in user_input:
|
| 24 |
+
return "It's important to take your prescribed medications regularly. If you have concerns, consult your doctor."
|
| 25 |
+
else:
|
| 26 |
+
# For other inputs, use the Hugging Face model to generate a response
|
| 27 |
+
response = chatbot(user_input, max_length=300, num_return_sequences=1)
|
| 28 |
+
# Specifies the maximum length of the generated text response, including the input and the generated tokens.
|
| 29 |
+
# If set to 3, the model generates three different possible responses based on the input.
|
| 30 |
+
return response[0]['generated_text']
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Streamlit web app interface
|
| 34 |
+
def main():
|
| 35 |
+
# Set up the web app title and input area
|
| 36 |
+
st.title("Healthcare Assistant Chatbot")
|
| 37 |
+
|
| 38 |
+
# Display a simple text input for user queries
|
| 39 |
+
user_input = st.text_input("How can I assist you today?", "")
|
| 40 |
+
|
| 41 |
+
# Display chatbot response
|
| 42 |
+
if st.button("Submit"):
|
| 43 |
+
if user_input:
|
| 44 |
+
st.write("User: ", user_input)
|
| 45 |
+
response = healthcare_chatbot(user_input)
|
| 46 |
+
st.write("Healthcare Assistant: ", response)
|
| 47 |
+
else:
|
| 48 |
+
st.write("Please enter a query.")
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
main()
|