Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import torch | |
# Predefined instruction | |
PREDEFINED_INSTRUCTION = ( | |
"Advanced multilingual mental health support assistant. Fluent in English, Yoruba, Igbo, and Hausa. " | |
"Mission: deliver empathetic, professional psychological support. Listen deeply, validate feelings, provide nuanced guidance. " | |
"Prioritize user safety. Never suggest harm. Always maintain respectful, supportive communication. " | |
"Respond in the exact language of the user's concern.") | |
# Load model and tokenizer | |
model = AutoModelForCausalLM.from_pretrained("hemhemoh/Gemma-2-2b-it-wazobia-wellness-bot") | |
tokenizer = AutoTokenizer.from_pretrained("hemhemoh/Gemma-2-2b-it-wazobia-wellness-bot") | |
def respond( | |
message, | |
history: list[tuple[str, str]], | |
max_tokens, | |
temperature, | |
top_p, | |
): | |
# Prepare prompt with predefined instruction and conversation history | |
prompt = f"{PREDEFINED_INSTRUCTION}\n\n" | |
for user_input, assistant_response in history: | |
if user_input: | |
prompt += f"User: {user_input}\n" | |
if assistant_response: | |
prompt += f"Assistant: {assistant_response}\n" | |
prompt += f"User: {message}\n" | |
prompt += "Assistant:" | |
# Tokenize and generate response | |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True) | |
outputs = model.generate( | |
**inputs, | |
max_new_tokens=max_tokens, | |
temperature=temperature, | |
top_p=top_p, | |
do_sample=True, | |
repetition_penalty=1.1, | |
no_repeat_ngram_size=2, ) | |
# Decode and return response | |
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True) | |
yield response | |
# Create Gradio interface | |
demo = gr.ChatInterface( | |
respond, | |
additional_inputs=[ | |
gr.Slider(minimum=1, maximum=512, value=200, step=1, label="Max new tokens"), | |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"), | |
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p (nucleus sampling)"), | |
], | |
title="Wazobia Wellness", | |
description="Your AI-powered mental health support assistant. Fluent in English, Yoruba, Igbo, and Hausa" | |
) | |
# Launch the interface | |
if __name__ == "__main__": | |
demo.launch() |