Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
app.py
|
3 |
+
|
4 |
+
This Gradio app loads your fine-tuned model and serves as a therapeutic chatbot named "Serenity".
|
5 |
+
It uses a system prompt to steer the conversation in a supportive, open-ended manner.
|
6 |
+
"""
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
import torch
|
10 |
+
from transformers import TextStreamer
|
11 |
+
from unsloth import FastLanguageModel
|
12 |
+
|
13 |
+
# ---------------------------
|
14 |
+
# 1. Load your fine-tuned model
|
15 |
+
# ---------------------------
|
16 |
+
max_seq_length = 2048 # adjust as needed
|
17 |
+
load_in_4bit = True # set to True if you used 4-bit quantization
|
18 |
+
dtype = None # auto-detect dtype
|
19 |
+
|
20 |
+
# Replace with your actual model repository on Hugging Face Hub
|
21 |
+
model_name = "YOUR_USERNAME/YOUR_MODEL_REPO"
|
22 |
+
|
23 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
24 |
+
model_name=model_name,
|
25 |
+
max_seq_length=max_seq_length,
|
26 |
+
load_in_4bit=load_in_4bit,
|
27 |
+
dtype=dtype,
|
28 |
+
)
|
29 |
+
FastLanguageModel.for_inference(model)
|
30 |
+
|
31 |
+
# ---------------------------
|
32 |
+
# 2. Define the therapeutic system prompt
|
33 |
+
# ---------------------------
|
34 |
+
therapy_system_prompt = """
|
35 |
+
You are "Serenity", a compassionate, supportive, and curious Therapist. Your role is to:
|
36 |
+
1. **Validate First**: Start by validating emotions.
|
37 |
+
2. **Explore Gently**: Always ask open-ended questions using "What" or "How".
|
38 |
+
3. **Encourage Elaboration**: Make sure to ask for more details.
|
39 |
+
4. **Avoid Closure**: Never end with statements - always end with a question.
|
40 |
+
5. **Support Safety**: If serious issues emerge, support them as best as possible and validate their feelings.
|
41 |
+
"""
|
42 |
+
|
43 |
+
# ---------------------------
|
44 |
+
# 3. Define the response generation function
|
45 |
+
# ---------------------------
|
46 |
+
def respond(message, chat_history):
|
47 |
+
"""
|
48 |
+
Generates a therapeutic response given a new user message and the conversation history.
|
49 |
+
|
50 |
+
Parameters:
|
51 |
+
message (str): The latest message from the user.
|
52 |
+
chat_history (list): List of (user_message, assistant_response) tuples.
|
53 |
+
|
54 |
+
Returns:
|
55 |
+
A tuple with an empty string (clearing the input) and the updated chat history.
|
56 |
+
"""
|
57 |
+
# Always include the system prompt at the beginning
|
58 |
+
messages = [{"role": "system", "content": therapy_system_prompt}]
|
59 |
+
|
60 |
+
# Append conversation history
|
61 |
+
for user_msg, bot_resp in chat_history:
|
62 |
+
messages.extend([
|
63 |
+
{"role": "user", "content": user_msg},
|
64 |
+
{"role": "assistant", "content": bot_resp}
|
65 |
+
])
|
66 |
+
|
67 |
+
# Append the new user message
|
68 |
+
messages.append({"role": "user", "content": message})
|
69 |
+
|
70 |
+
# Tokenize with therapeutic context
|
71 |
+
inputs = tokenizer.apply_chat_template(
|
72 |
+
messages,
|
73 |
+
tokenize=True,
|
74 |
+
add_generation_prompt=True,
|
75 |
+
return_tensors="pt",
|
76 |
+
).to("cuda")
|
77 |
+
|
78 |
+
# Generate the response
|
79 |
+
outputs = model.generate(
|
80 |
+
input_ids=inputs,
|
81 |
+
max_new_tokens=256,
|
82 |
+
temperature=0.85,
|
83 |
+
repetition_penalty=1.2,
|
84 |
+
top_p=0.90,
|
85 |
+
do_sample=True,
|
86 |
+
eos_token_id=tokenizer.eos_token_id,
|
87 |
+
pad_token_id=tokenizer.eos_token_id,
|
88 |
+
)
|
89 |
+
|
90 |
+
# Process response:
|
91 |
+
# Decode the output and extract the assistant's reply.
|
92 |
+
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
93 |
+
# The split strategy here might need adjustment depending on your template;
|
94 |
+
# we assume the assistant reply is after the last occurrence of "assistant"
|
95 |
+
therapy_response = full_response.split("assistant")[-1].strip()
|
96 |
+
|
97 |
+
# Update chat history
|
98 |
+
chat_history.append((message, therapy_response))
|
99 |
+
|
100 |
+
return "", chat_history
|
101 |
+
|
102 |
+
# ---------------------------
|
103 |
+
# 4. Build the Gradio Interface
|
104 |
+
# ---------------------------
|
105 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="teal")) as demo:
|
106 |
+
gr.Markdown("""
|
107 |
+
# 🌿 Serenity - AI Therapist
|
108 |
+
*A safe space for emotional support and reflection*
|
109 |
+
""")
|
110 |
+
|
111 |
+
# The chatbot component displays the conversation
|
112 |
+
chatbot = gr.Chatbot(height=450, avatar_images=("user.png", "therapist.png"))
|
113 |
+
msg = gr.Textbox(label="Share your feelings", placeholder="Type your message...")
|
114 |
+
|
115 |
+
with gr.Row():
|
116 |
+
submit_btn = gr.Button("Send", variant="primary")
|
117 |
+
clear_btn = gr.Button("Clear History")
|
118 |
+
|
119 |
+
# State to hold chat history as list of (user, assistant) tuples
|
120 |
+
chat_state = gr.State([])
|
121 |
+
|
122 |
+
# Interaction handlers:
|
123 |
+
# When the user submits a message, generate a response and update the history.
|
124 |
+
submit_btn.click(
|
125 |
+
respond,
|
126 |
+
[msg, chat_state],
|
127 |
+
[msg, chatbot],
|
128 |
+
queue=False
|
129 |
+
)
|
130 |
+
|
131 |
+
msg.submit(
|
132 |
+
respond,
|
133 |
+
[msg, chat_state],
|
134 |
+
[msg, chatbot],
|
135 |
+
queue=False
|
136 |
+
)
|
137 |
+
|
138 |
+
# Clear chat history handler
|
139 |
+
clear_btn.click(
|
140 |
+
lambda: [], None, chat_state, queue=False
|
141 |
+
).then(
|
142 |
+
lambda: None, None, chatbot, queue=False
|
143 |
+
)
|
144 |
+
|
145 |
+
# ---------------------------
|
146 |
+
# 5. Launch the app
|
147 |
+
# ---------------------------
|
148 |
+
demo.launch(debug=False, share=True)
|