Spaces:
Sleeping
Sleeping
File size: 2,505 Bytes
f3e9507 7ae6b7d ddffc2c a47adc3 7ae6b7d 34d37fe 7ae6b7d 9a1219a 7fa8691 7ae6b7d 9a1219a 7ae6b7d ddffc2c 7ae6b7d 7fa8691 a47adc3 7ae6b7d 7fa8691 bd66ae1 8d10a89 9a1219a a47adc3 8d10a89 9a1219a 7fa8691 9a1219a bd66ae1 7fa8691 8d10a89 9a1219a 7fa8691 34d37fe a47adc3 bd66ae1 a47adc3 bd66ae1 a47adc3 ddffc2c a47adc3 b2d83e4 9a1219a | 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# ----------------------
# Model setup
# ----------------------
MODEL_NAME = "Dhansh2001/my-fitlien-chatbot-pruned-quantized"
def load_model():
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
# Ensure pad token is set
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model.config.pad_token_id = tokenizer.eos_token_id
return tokenizer, model
tokenizer, model = load_model()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
# ----------------------
# Chat function
# ----------------------
def chat_with_bot(message, history):
try:
# Convert history to conversation string
conversation = ""
for user_msg, bot_msg in history:
conversation += f"User: {user_msg}\nBot: {bot_msg}\n"
conversation += f"User: {message}\nBot:"
# Encode input
inputs = tokenizer.encode(conversation, return_tensors="pt").to(device)
# Generate response
with torch.no_grad():
outputs = model.generate(
inputs,
max_length=500,
num_beams=5,
no_repeat_ngram_size=3,
do_sample=True,
temperature=0.7,
pad_token_id=tokenizer.eos_token_id,
early_stopping=True
)
# Decode only the new part
reply = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
return reply.strip() if reply.strip() else "I'm not sure how to answer that."
except Exception as e:
return f"⚠️ Error: {str(e)}"
# ----------------------
# Use ChatInterface instead of Blocks
# ----------------------
demo = gr.ChatInterface(
fn=chat_with_bot,
title="My Fitlien Chatbot",
description="A chatbot fine-tuned from DialoGPT-medium for fitness conversations.",
examples=[
"Hello! How are you?",
"What's a good workout routine?",
"Tell me about nutrition",
"How can I stay motivated?"
],
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear",
submit_btn="Submit"
)
# Launch
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True
) |