Spaces:
Sleeping
Sleeping
File size: 1,883 Bytes
dc2f8c5 e92705a dc2f8c5 6422bb0 e92705a dc2f8c5 004ce70 6422bb0 016a685 dc2f8c5 3b94a44 004ce70 3b94a44 004ce70 e92705a dc2f8c5 004ce70 dc2f8c5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 |
import streamlit as st
from transformers import T5ForConditionalGeneration, T5Tokenizer
import torch
# Load pre-trained model and tokenizer from the "KhantKyaw/T5-small_new_chatbot"
model_name = "KhantKyaw/T5-small_new_chatbot" # Use the fine-tuned model
model = T5ForConditionalGeneration.from_pretrained(model_name)
tokenizer = T5Tokenizer.from_pretrained(model_name)
# Set device to GPU if available for faster inference, otherwise fallback to CPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
# Streamlit Interface
st.title("Mental Health Chatbot with T5")
def generate_response(input_text):
# Add conversational context to input
input_text = f"You are a helpful assistant. {input_text}"
# Tokenize input text
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
# Generate a response from the model with advanced generation settings
outputs = model.generate(input_ids,
max_length=100, # max length of the output sequence
num_beams=5, # Beam search for better results
top_p=0.95, # Top-p sampling for more variety
temperature=0.7, # Temperature controls randomness
no_repeat_ngram_size=2, # Prevent repetition of n-grams
pad_token_id=tokenizer.eos_token_id) # Padding token to avoid padding tokens being part of the output
# Decode the model's output to a readable string
bot_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
return bot_output
# Create input box for user to type a message
user_input = st.text_input("You: ", "")
if user_input:
# Generate and display the bot's response
response = generate_response(user_input)
st.write(f"Bot: {response}")
|