igortech commited on
Commit
c5b594f
·
verified ·
1 Parent(s): 6cbe796

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -3
app.py CHANGED
@@ -3,7 +3,25 @@ from transformers import pipeline
3
 
4
  generator = pipeline("text-generation", model="microsoft/DialoGPT-small")
5
 
6
- def simple_gen(text):
7
- return generator(text, max_length=50)[0]['generated_text']
 
 
 
 
 
 
 
8
 
9
- gr.Interface(simple_gen, gr.Textbox(), gr.Textbox()).launch()
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  generator = pipeline("text-generation", model="microsoft/DialoGPT-small")
5
 
6
+ def chat_fn(message, history=None):
7
+ print(f"User message: {message}")
8
+ if history is None:
9
+ history = []
10
+ conversation = ""
11
+ for user_msg, bot_msg in history:
12
+ conversation += f"User: {user_msg}\nBot: {bot_msg}\n"
13
+ conversation += f"User: {message}\nBot:"
14
+ print(f"Built conversation prompt:\n{conversation}")
15
 
16
+ try:
17
+ response = generator(conversation, max_length=200, pad_token_id=50256)
18
+ bot_reply = response[0]['generated_text'].split("Bot:")[-1].strip()
19
+ print(f"Bot reply: {bot_reply}")
20
+ except Exception as e:
21
+ bot_reply = f"Error generating response: {e}"
22
+ print(f"Error: {e}")
23
+
24
+ history.append((message, bot_reply))
25
+ return bot_reply, history
26
+
27
+ gr.ChatInterface(fn=chat_fn).launch()