Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
)
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import BlenderbotForConditionalGeneration, BlenderbotTokenizer
|
3 |
+
|
4 |
+
# Load BlenderBot model and tokenizer
|
5 |
+
model_name = "facebook/blenderbot-400M-distill"
|
6 |
+
model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
|
7 |
+
tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
|
8 |
+
|
9 |
+
def respond(message, history):
|
10 |
+
# Format history for BlenderBot (requires previous turns)
|
11 |
+
chat_history = "\n".join([f"User: {h[0]}\nBot: {h[1]}" for h in history])
|
12 |
+
full_text = f"{chat_history}\nUser: {message}\nBot:"
|
13 |
+
|
14 |
+
# Tokenize and generate
|
15 |
+
inputs = tokenizer([full_text], return_tensors="pt", truncation=True, max_length=512)
|
16 |
+
reply_ids = model.generate(**inputs, max_length=200, temperature=0.9)
|
17 |
+
response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
|
18 |
+
|
19 |
+
# Extract only the latest bot response
|
20 |
+
return response.split("Bot:")[-1].strip()
|
21 |
+
|
22 |
+
# Launch with Gradio
|
23 |
+
gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
respond,
|
25 |
+
title="🤖 Friendly BlenderBot",
|
26 |
+
examples=["How are you?", "What's your favorite movie?"],
|
27 |
+
theme="soft"
|
28 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|