igortech commited on
Commit
6b792a7
·
verified ·
1 Parent(s): fa0fde1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -1,29 +1,31 @@
1
  import logging
2
  logging.basicConfig(filename="debug.log", level=logging.DEBUG, format='%(asctime)s %(message)s')
 
3
  import gradio as gr
4
  from transformers import pipeline
5
 
6
  generator = pipeline("text-generation", model="microsoft/DialoGPT-small")
7
 
8
  def chat_fn(message, history=None):
9
- logging.debug(f"User message: {message}")
10
- if history is None:
11
- history = []
12
- conversation = ""
13
- for user_msg, bot_msg in history:
14
- conversation += f"User: {user_msg}\nBot: {bot_msg}\n"
15
- conversation += f"User: {message}\nBot:"
16
- logging.debug(f"Built conversation prompt:\n{conversation}")
17
-
18
  try:
 
 
 
 
 
 
 
 
 
19
  response = generator(conversation, max_length=200, pad_token_id=50256)
20
  bot_reply = response[0]['generated_text'].split("Bot:")[-1].strip()
21
  logging.debug(f"Bot reply: {bot_reply}")
22
- except Exception as e:
23
- bot_reply = f"Error generating response: {e}"
24
- logging.debug(f"Error: {e}")
25
 
26
- history.append((message, bot_reply))
27
- return bot_reply, history
 
 
 
 
28
 
29
  gr.ChatInterface(fn=chat_fn).launch()
 
1
  import logging
2
  logging.basicConfig(filename="debug.log", level=logging.DEBUG, format='%(asctime)s %(message)s')
3
+
4
  import gradio as gr
5
  from transformers import pipeline
6
 
7
  generator = pipeline("text-generation", model="microsoft/DialoGPT-small")
8
 
9
  def chat_fn(message, history=None):
 
 
 
 
 
 
 
 
 
10
  try:
11
+ logging.debug(f"User message: {message}")
12
+ if history is None:
13
+ history = []
14
+ conversation = ""
15
+ for user_msg, bot_msg in history:
16
+ conversation += f"User: {user_msg}\nBot: {bot_msg}\n"
17
+ conversation += f"User: {message}\nBot:"
18
+ logging.debug(f"Prompt:\n{conversation}")
19
+
20
  response = generator(conversation, max_length=200, pad_token_id=50256)
21
  bot_reply = response[0]['generated_text'].split("Bot:")[-1].strip()
22
  logging.debug(f"Bot reply: {bot_reply}")
 
 
 
23
 
24
+ history.append((message, bot_reply))
25
+ return bot_reply, history
26
+
27
+ except Exception as e:
28
+ logging.error(f"Exception in chat_fn: {e}", exc_info=True)
29
+ return f"Oops, something went wrong: {e}", history or []
30
 
31
  gr.ChatInterface(fn=chat_fn).launch()