Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
response = qa_pipeline(user_input, max_length=50, num_return_sequences=1)[0]["generated_text"]
|
| 10 |
-
history.append((user_input, response))
|
| 11 |
-
return history, ""
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
with gr.Blocks() as demo:
|
| 14 |
chatbot = gr.Chatbot()
|
| 15 |
-
msg = gr.Textbox(
|
| 16 |
-
with gr.Row():
|
| 17 |
-
submit = gr.Button("Send")
|
| 18 |
-
clear = gr.Button("New Chat")
|
| 19 |
-
|
| 20 |
-
# Stav pro ukládání historie
|
| 21 |
-
state = gr.State([])
|
| 22 |
|
| 23 |
-
def respond(
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
msg.submit(respond, [msg, state], [chatbot, msg, state], show_progress=True)
|
| 28 |
-
clear.click(lambda: ([], "", []), None, [chatbot, msg, state])
|
| 29 |
|
| 30 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 3 |
|
| 4 |
+
# Načteme model a tokenizer
|
| 5 |
+
model_name = "facebook/blenderbot-400M-distill"
|
| 6 |
+
tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
|
| 9 |
+
# Uchováme historii konverzace
|
| 10 |
+
history = []
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def chat(user_input):
|
| 13 |
+
global history
|
| 14 |
+
|
| 15 |
+
# Přidáme novou zprávu uživatele do historie
|
| 16 |
+
history.append(user_input)
|
| 17 |
+
|
| 18 |
+
# Připravíme vstup (předchozí zprávy spojíme)
|
| 19 |
+
conversation = " ".join(history[-5:]) # Posledních 5 zpráv
|
| 20 |
+
inputs = tokenizer(conversation, return_tensors="pt")
|
| 21 |
+
|
| 22 |
+
# Model vygeneruje odpověď
|
| 23 |
+
reply_ids = model.generate(**inputs, max_length=100)
|
| 24 |
+
reply = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
| 25 |
+
|
| 26 |
+
# Přidáme odpověď do historie
|
| 27 |
+
history.append(reply)
|
| 28 |
+
|
| 29 |
+
return reply
|
| 30 |
+
|
| 31 |
+
# Gradio rozhraní
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
chatbot = gr.Chatbot()
|
| 34 |
+
msg = gr.Textbox(label="Napiš zprávu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
def respond(message, chat_history):
|
| 37 |
+
bot_message = chat(message)
|
| 38 |
+
chat_history.append((message, bot_message))
|
| 39 |
+
return "", chat_history
|
| 40 |
|
| 41 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
|
|
|
|
|
| 42 |
|
| 43 |
demo.launch()
|