Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,58 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
10 |
-
model = AutoModelForCausalLM.from_pretrained(model_id).to(device)
|
11 |
-
model.eval()
|
12 |
-
|
13 |
-
user_history = []
|
14 |
-
turn_counter = 0
|
15 |
-
MAX_TURNS_FOR_PREDICTION = 8
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
turn_counter += 1
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
max_new_tokens=50,
|
29 |
-
do_sample=False,
|
30 |
-
pad_token_id=tokenizer.eos_token_id,
|
31 |
-
eos_token_id=tokenizer.eos_token_id,
|
32 |
-
)
|
33 |
-
response_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
34 |
-
response = response_text.split("AI:")[-1].strip()
|
35 |
-
user_history.append(f"AI: {response}")
|
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 |
-
outputs=[
|
68 |
-
gr.Textbox(label="Bot Response"),
|
69 |
-
gr.Textbox(label="Depression Probability (after 8 turns)")
|
70 |
-
],
|
71 |
-
title="Depression Detection Chatbot",
|
72 |
-
description="This chatbot detects depression by conversing for multiple turns. After 8 turns, it estimates the probability of depression."
|
73 |
-
)
|
74 |
-
|
75 |
-
chat_interface.launch()
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
from peft import PeftModel, PeftConfig
|
4 |
+
import gradio as gr
|
5 |
|
6 |
+
# Load adapter config and base model
|
7 |
+
adapter_id = "KomalNaseem/depression-chatbot-model"
|
8 |
+
peft_config = PeftConfig.from_pretrained(adapter_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Load tokenizer
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(peft_config.base_model_name_or_path)
|
|
|
12 |
|
13 |
+
# Load base model
|
14 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
15 |
+
peft_config.base_model_name_or_path,
|
16 |
+
torch_dtype=torch.float16,
|
17 |
+
device_map="auto"
|
18 |
+
)
|
19 |
|
20 |
+
# Load the LoRA adapter
|
21 |
+
model = PeftModel.from_pretrained(base_model, adapter_id)
|
22 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# Chat function
|
25 |
+
def chat_fn(user_input, history):
|
26 |
+
# Prepare input prompt by appending to history
|
27 |
+
history = history or []
|
28 |
+
prompt = ""
|
29 |
+
for turn in history:
|
30 |
+
prompt += f"User: {turn[0]}\nAI: {turn[1]}\n"
|
31 |
+
prompt += f"User: {user_input}\nAI:"
|
32 |
+
|
33 |
+
# Tokenize and generate response
|
34 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
35 |
+
with torch.no_grad():
|
36 |
+
outputs = model.generate(
|
37 |
+
**inputs,
|
38 |
+
max_new_tokens=200,
|
39 |
+
temperature=0.7,
|
40 |
+
top_p=0.9,
|
41 |
+
do_sample=True,
|
42 |
+
pad_token_id=tokenizer.eos_token_id
|
43 |
)
|
44 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
45 |
+
|
46 |
+
# Extract AI response (cutting off prompt)
|
47 |
+
ai_response = decoded.split("User:")[-1].split("AI:")[-1].strip()
|
48 |
+
history.append((user_input, ai_response))
|
49 |
+
return history, history
|
50 |
+
|
51 |
+
# Launch Gradio chat interface
|
52 |
+
gr.ChatInterface(
|
53 |
+
fn=chat_fn,
|
54 |
+
title="🧠 Depression Detection Chatbot",
|
55 |
+
description="This chatbot engages in a conversation and detects signs of depression using LLaMA 3.2 with a fine-tuned LoRA adapter.",
|
56 |
+
theme="soft",
|
57 |
+
examples=["I'm feeling really low today.", "I can't sleep at night and feel hopeless."],
|
58 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|