Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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 |
-
with gr.Sidebar():
|
| 64 |
-
gr.LoginButton()
|
| 65 |
-
chatbot.render()
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
if __name__ == "__main__":
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
print("Loading MindBridge model...")
|
| 6 |
+
model_name = "prats010/mindbridge-mental-health-model"
|
| 7 |
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
model_name,
|
| 13 |
+
torch_dtype=torch.float32
|
| 14 |
+
)
|
| 15 |
+
model.eval()
|
| 16 |
+
print("✅ Model loaded!")
|
| 17 |
+
|
| 18 |
+
def chat(user_input, history):
|
| 19 |
+
input_text = f"User: {user_input}\nCounselor:"
|
| 20 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt")
|
| 21 |
+
attention_mask = inputs.ne(tokenizer.eos_token_id).long()
|
| 22 |
+
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model.generate(
|
| 25 |
+
inputs,
|
| 26 |
+
attention_mask=attention_mask,
|
| 27 |
+
max_new_tokens=150,
|
| 28 |
+
temperature=0.9,
|
| 29 |
+
top_p=0.85,
|
| 30 |
+
top_k=50,
|
| 31 |
+
do_sample=True,
|
| 32 |
+
repetition_penalty=1.5,
|
| 33 |
+
no_repeat_ngram_size=3,
|
| 34 |
+
pad_token_id=tokenizer.eos_token_id
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
+
response = response.split("Counselor:")[-1].strip()
|
| 39 |
+
return response
|
| 40 |
+
|
| 41 |
+
demo = gr.ChatInterface(
|
| 42 |
+
fn=chat,
|
| 43 |
+
title="MindBridge AI",
|
| 44 |
+
description="Your mental health companion",
|
| 45 |
+
examples=[
|
| 46 |
+
"I have been feeling really anxious lately",
|
| 47 |
+
"I feel like nobody understands me",
|
| 48 |
+
"I can't sleep and feel overwhelmed"
|
| 49 |
+
]
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|