tahirsher commited on
Commit
4051d52
1 Parent(s): 0c2980c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from peft import PeftModel, PeftConfig
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4
+
5
+ # Load the chatbot model with PEFT
6
+ @st.cache_resource
7
+ def load_chatbot_model():
8
+ # Load the Peft configuration and base model
9
+ config = PeftConfig.from_pretrained("langtest/falcon-llama3-finetuned-mental-health-hf-plus-dsm5-new-mistral")
10
+ base_model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
11
+ peft_model = PeftModel.from_pretrained(base_model, "langtest/falcon-llama3-finetuned-mental-health-hf-plus-dsm5-new-mistral")
12
+
13
+ # Load the tokenizer for generating the text
14
+ tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
15
+
16
+ # Create a text generation pipeline using the model and tokenizer
17
+ return pipeline("text-generation", model=peft_model, tokenizer=tokenizer)
18
+
19
+ # Initialize the chatbot
20
+ chatbot = load_chatbot_model()
21
+
22
+ # Function to generate a response from the chatbot
23
+ def generate_response(user_input):
24
+ # Generate the response using the chatbot model
25
+ response = chatbot(user_input, max_length=100, num_return_sequences=1)
26
+ return response[0]['generated_text']
27
+
28
+ # Streamlit UI setup
29
+ st.title("Mental Health Chatbot")
30
+ st.write("""
31
+ This chatbot is designed to provide empathetic responses to mental health issues.
32
+ It is not a replacement for professional help, but it aims to offer support.
33
+ """)
34
+
35
+ # Input from the user
36
+ user_input = st.text_input("You: ", placeholder="How are you feeling today?")
37
+
38
+ # Display chat history and chatbot responses
39
+ if user_input:
40
+ with st.spinner("The chatbot is thinking..."):
41
+ response = generate_response(user_input)
42
+ st.text_area("Chatbot:", value=response, height=200)
43
+
44
+ # Provide some mental health support resources
45
+ st.markdown("""
46
+ ### Mental Health Resources:
47
+ - [National Alliance on Mental Illness (NAMI)](https://www.nami.org/Home)
48
+ - [Mental Health America](https://www.mhanational.org/)
49
+ - [Crisis Text Line](https://www.crisistextline.org/)
50
+ """)