jsilbersky commited on
Commit
4548130
·
verified ·
1 Parent(s): f907b3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -20
app.py CHANGED
@@ -1,30 +1,43 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- # Používáme větší model GPT-2 pro generování textu
5
- qa_pipeline = pipeline("text-generation", model="gpt2")
 
 
6
 
7
- def chat(user_input, history):
8
- # Generování odpovědi s maximální délkou 30 pro rychlé odpovědi
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(placeholder="Type your message here...")
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(user_input, history):
24
- return chat(user_input, history)
 
 
25
 
26
- submit.click(respond, [msg, state], [chatbot, msg, state], show_progress=True)
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()