hadadrjt commited on
Commit
62027e8
·
1 Parent(s): e29b7bd

ai: Enable stream/real-time responses.

Browse files

* Revert 68ce31f, and integrated with
gradio docs.

Ref: https://www.gradio.app/docs/gradio/chatbot

Files changed (1) hide show
  1. jarvis.py +7 -3
jarvis.py CHANGED
@@ -112,7 +112,8 @@ def chat_with_model(history, user_input, selected_model_display, sess):
112
  def respond(multi_input, history, selected_model_display, sess):
113
  message = {"text": multi_input.get("text", "").strip(), "files": multi_input.get("files", [])}
114
  if not message["text"] and not message["files"]:
115
- return history, gr.MultimodalTextbox(value=None, interactive=True), sess
 
116
  combined_input = ""
117
  for file_item in message["files"]:
118
  if isinstance(file_item, dict) and "name" in file_item:
@@ -125,8 +126,11 @@ def respond(multi_input, history, selected_model_display, sess):
125
  combined_input += message["text"]
126
  history.append([combined_input, ""])
127
  ai_response = chat_with_model(history, combined_input, selected_model_display, sess)
128
- history[-1][1] = ai_response
129
- return history, gr.MultimodalTextbox(value=None, interactive=True), sess
 
 
 
130
 
131
  def change_model(new_model_display):
132
  return [], create_session(), new_model_display
 
112
  def respond(multi_input, history, selected_model_display, sess):
113
  message = {"text": multi_input.get("text", "").strip(), "files": multi_input.get("files", [])}
114
  if not message["text"] and not message["files"]:
115
+ yield history, gr.MultimodalTextbox(value=None, interactive=True), sess
116
+ return
117
  combined_input = ""
118
  for file_item in message["files"]:
119
  if isinstance(file_item, dict) and "name" in file_item:
 
126
  combined_input += message["text"]
127
  history.append([combined_input, ""])
128
  ai_response = chat_with_model(history, combined_input, selected_model_display, sess)
129
+ history[-1][1] = ""
130
+ for character in ai_response:
131
+ history[-1][1] += character
132
+ time.sleep(0.0005)
133
+ yield history, gr.MultimodalTextbox(value=None, interactive=True), sess
134
 
135
  def change_model(new_model_display):
136
  return [], create_session(), new_model_display