Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,38 @@
|
|
| 1 |
-
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
generator = pipeline("text-generation", model="gpt2")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
MODEL_ID = "distilgpt2" # fast + small + reliable on CPU
|
|
|
|
| 6 |
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
|
| 9 |
+
|
| 10 |
+
def respond(message, history):
|
| 11 |
+
# Build a simple prompt from chat history
|
| 12 |
+
prompt = ""
|
| 13 |
+
for user, bot in history:
|
| 14 |
+
prompt += f"User: {user}\nBot: {bot}\n"
|
| 15 |
+
prompt += f"User: {message}\nBot:"
|
| 16 |
+
|
| 17 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
output_ids = model.generate(
|
| 20 |
+
**inputs,
|
| 21 |
+
max_new_tokens=120,
|
| 22 |
+
do_sample=True,
|
| 23 |
+
temperature=0.8,
|
| 24 |
+
top_p=0.95,
|
| 25 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
decoded = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 29 |
+
reply = decoded.split("Bot:")[-1].strip()
|
| 30 |
+
return reply
|
| 31 |
+
|
| 32 |
+
demo = gr.ChatInterface(
|
| 33 |
+
fn=respond,
|
| 34 |
+
title="🤖 Fast Chat",
|
| 35 |
+
description="Lightweight CPU-friendly chatbot running on Hugging Face Spaces."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
demo.launch()
|