AndrewLam489 commited on
Commit
f4c82e6
·
verified ·
1 Parent(s): cbb8c2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -18
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
3
 
4
- # Load the model and tokenizer
5
- chatbot = pipeline("text2text-generation", model="google/flan-t5-large")
 
6
 
7
  # Set up the Streamlit page configuration
8
  st.set_page_config(page_title="AI Companion Chatbot", layout="centered")
@@ -19,25 +21,34 @@ providing a safe and empathetic space for you to express your feelings.
19
  # Create a text input box for user input
20
  user_input = st.text_area("How are you feeling today?", "")
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Display chatbot's response
23
  if st.button("Send"):
24
  if user_input: # Check if the user has provided input
25
- # Construct the message to instruct the model to behave like a therapist
26
- prompt = f"""
27
- You are a therapist with a strong focus on providing practical, actionable advice.
28
- Rules:
29
- 1. Respond in a supportive, empathetic, and non-judgmental manner to the following statement.
30
- 2. Offer at least 3 **specific** strategies or coping techniques that the user can try immediately to manage or alleviate their anxiety.
31
- These could include emotional regulation techniques (like grounding exercises, breathing techniques),
32
- self-care practices (like self-compassion or taking breaks), or mindset shifts (like reframing negative thoughts or focusing on what can be controlled).
33
- 3. Be very descriptive. Use bullet points to clearly state actionable steps.
34
- 4. Do not use "I" or reference the first person perspective.
35
-
36
- Base your response on how the user is feeling: {user_input}
37
- """
38
-
39
  # Get the response from the model
40
- response = chatbot(prompt, max_length=350, num_return_sequences=1)[0]['generated_text']
41
 
42
  # Show the response
43
  st.text_area("AI Companion Response:", response, height=200)
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ import torch
4
 
5
+ # Load the tokenizer and model
6
+ tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it")
7
+ model = AutoModelForCausalLM.from_pretrained("google/gemma-7b-it", torch_dtype=torch.bfloat16)
8
 
9
  # Set up the Streamlit page configuration
10
  st.set_page_config(page_title="AI Companion Chatbot", layout="centered")
 
21
  # Create a text input box for user input
22
  user_input = st.text_area("How are you feeling today?", "")
23
 
24
+ # Define the function to generate the response
25
+ def generate_response(user_input):
26
+ prompt = f"""
27
+ You are a therapist with a strong focus on providing practical, actionable advice.
28
+ Rules:
29
+ 1. Respond in a supportive, empathetic, and non-judgmental manner to the following statement.
30
+ 2. Offer at least 3 **specific** strategies or coping techniques that the user can try immediately to manage or alleviate their anxiety.
31
+ These could include emotional regulation techniques (like grounding exercises, breathing techniques),
32
+ self-care practices (like self-compassion or taking breaks), or mindset shifts (like reframing negative thoughts or focusing on what can be controlled).
33
+ 3. Be very descriptive. Use bullet points to clearly state actionable steps.
34
+ 4. Do not use "I" or reference the first person perspective.
35
+
36
+ Base your response on how the user is feeling: {user_input}
37
+ """
38
+
39
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
40
+
41
+ # Generate the output
42
+ outputs = model.generate(**inputs, max_length=350, num_return_sequences=1)
43
+
44
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
45
+ return response
46
+
47
  # Display chatbot's response
48
  if st.button("Send"):
49
  if user_input: # Check if the user has provided input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  # Get the response from the model
51
+ response = generate_response(user_input)
52
 
53
  # Show the response
54
  st.text_area("AI Companion Response:", response, height=200)