Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,53 +2,61 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
MODEL_NAME = "Dhansh2001/my-fitlien-chatbot-pruned-quantized"
|
| 7 |
|
|
|
|
| 8 |
def load_model():
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 11 |
-
|
|
|
|
| 12 |
if tokenizer.pad_token is None:
|
| 13 |
tokenizer.pad_token = tokenizer.eos_token
|
| 14 |
model.config.pad_token_id = tokenizer.eos_token_id
|
| 15 |
-
|
| 16 |
return tokenizer, model
|
| 17 |
|
| 18 |
-
# Load
|
| 19 |
tokenizer, model = load_model()
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def chat_with_bot(message, history):
|
| 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 |
iface = gr.ChatInterface(
|
| 49 |
fn=chat_with_bot,
|
| 50 |
title="My Fitlien Chatbot",
|
| 51 |
-
description="A chatbot fine-tuned from DialoGPT-medium for fitness conversations",
|
| 52 |
examples=[
|
| 53 |
"Hello! How are you?",
|
| 54 |
"What's a good workout routine?",
|
|
@@ -61,4 +69,4 @@ iface = gr.ChatInterface(
|
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
iface.launch()
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Model name
|
| 6 |
MODEL_NAME = "Dhansh2001/my-fitlien-chatbot-pruned-quantized"
|
| 7 |
|
| 8 |
+
# Load model & tokenizer
|
| 9 |
def load_model():
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 12 |
+
|
| 13 |
+
# Ensure pad token is set
|
| 14 |
if tokenizer.pad_token is None:
|
| 15 |
tokenizer.pad_token = tokenizer.eos_token
|
| 16 |
model.config.pad_token_id = tokenizer.eos_token_id
|
| 17 |
+
|
| 18 |
return tokenizer, model
|
| 19 |
|
| 20 |
+
# Load once at startup
|
| 21 |
tokenizer, model = load_model()
|
| 22 |
|
| 23 |
+
# Use CPU or GPU automatically
|
| 24 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 25 |
+
model.to(device)
|
| 26 |
+
|
| 27 |
def chat_with_bot(message, history):
|
| 28 |
+
try:
|
| 29 |
+
# Encode user input
|
| 30 |
+
inputs = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt").to(device)
|
| 31 |
+
|
| 32 |
+
# Generate response
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
outputs = model.generate(
|
| 35 |
+
inputs,
|
| 36 |
+
max_length=500,
|
| 37 |
+
num_beams=5,
|
| 38 |
+
no_repeat_ngram_size=3,
|
| 39 |
+
do_sample=True,
|
| 40 |
+
temperature=0.7,
|
| 41 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 42 |
+
early_stopping=True
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Decode and return
|
| 46 |
+
reply = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
| 47 |
+
|
| 48 |
+
# Ensure a safe string return
|
| 49 |
+
return reply if reply.strip() != "" else "I'm not sure how to answer that."
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
# Catch errors so API never 500s silently
|
| 53 |
+
return f"⚠️ Error: {str(e)}"
|
| 54 |
|
| 55 |
+
# Gradio ChatInterface
|
| 56 |
iface = gr.ChatInterface(
|
| 57 |
fn=chat_with_bot,
|
| 58 |
title="My Fitlien Chatbot",
|
| 59 |
+
description="A chatbot fine-tuned from DialoGPT-medium for fitness conversations.",
|
| 60 |
examples=[
|
| 61 |
"Hello! How are you?",
|
| 62 |
"What's a good workout routine?",
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
iface.launch()
|