hsuwill000 commited on
Commit
325f636
·
verified ·
1 Parent(s): c786907

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -17
app.py CHANGED
@@ -22,29 +22,24 @@ def user(user_message, history: list):
22
 
23
 
24
  def bot(history: list, user_message):
25
- # Use OpenVINO to generate a response
26
- full_response = "" # Store the complete response
27
 
28
- def streamer(subword): # Local streamer function
29
- nonlocal full_response # Allow modification of outer scope variable
30
- full_response += subword # Accumulate the subword
31
- history[-1]['content'] = full_response # Update chatbot content
32
  yield history
33
  return ov_genai.StreamingStatus.RUNNING
34
 
35
-
36
- # Initialize the bot message in history
37
  history.append({"role": "assistant", "content": ""})
38
 
39
- # Generate the response using the streaming function
40
- for updated_history in pipe.generate(user_message, streamer=streamer, max_new_tokens=100):
41
- yield updated_history
42
-
43
- # Alternatively, without the step-by-step updates, you can just do this:
44
- # full_response = pipe.generate(user_message, max_new_tokens=100) # but this will skip the steaming
45
- # history[-1]['content'] = full_response
46
- # yield history
47
-
48
 
49
  with gr.Blocks() as demo:
50
  chatbot = gr.Chatbot(type="messages")
 
22
 
23
 
24
  def bot(history: list, user_message):
25
+ full_response = ""
 
26
 
27
+ def streamer(subword):
28
+ nonlocal full_response
29
+ full_response += subword
30
+ history[-1]['content'] = full_response
31
  yield history
32
  return ov_genai.StreamingStatus.RUNNING
33
 
 
 
34
  history.append({"role": "assistant", "content": ""})
35
 
36
+ try:
37
+ for updated_history in pipe.generate(user_message, streamer=streamer, max_new_tokens=100):
38
+ yield updated_history
39
+ except Exception as e:
40
+ print(f"Error during OpenVINO generation: {e}") # Log the error!
41
+ history[-1]['content'] = "An error occurred while generating the response."
42
+ yield history
 
 
43
 
44
  with gr.Blocks() as demo:
45
  chatbot = gr.Chatbot(type="messages")