HedronCreeper commited on
Commit
4b7bbcf
·
verified ·
1 Parent(s): a1f6914

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -16,8 +16,8 @@ def predict(message, history):
16
  yield "Error: API_KEY secret is missing in Hugging Face Space settings."
17
  return
18
 
19
- # Modern Gradio versions provide 'history' directly as a list of dicts.
20
- # We copy it and append the latest user message.
21
  messages = list(history) if history else []
22
  messages.append({"role": "user", "content": message})
23
 
@@ -29,24 +29,23 @@ def predict(message, history):
29
  stream=True
30
  )
31
 
32
- # Yield chunks for the streaming typing effect
33
  partial_message = ""
34
  for chunk in response:
35
- if chunk.choices and chunk.choices[0].delta.content:
36
- partial_message += chunk.choices[0].delta.content
37
  yield partial_message
38
 
39
  except Exception as e:
40
  yield f"An error occurred: {str(e)}"
41
 
42
- # Define the ChatInterface using modern Gradio specifications
43
  demo = gr.ChatInterface(
44
  fn=predict,
45
- type="messages", # Dictates the modern list-of-dicts history format
46
  title="OpenRouter Chatbot",
47
- description="Running on the latest version of Gradio.",
48
  )
49
 
50
  if __name__ == "__main__":
51
- # Explicitly turn off experimental SSR mode to prevent 404 heartbeat errors
52
  demo.launch(ssr_mode=False)
 
16
  yield "Error: API_KEY secret is missing in Hugging Face Space settings."
17
  return
18
 
19
+ # In modern Gradio versions, 'history' is natively a list of dicts.
20
+ # Copy existing history and append the latest user entry.
21
  messages = list(history) if history else []
22
  messages.append({"role": "user", "content": message})
23
 
 
29
  stream=True
30
  )
31
 
32
+ # Yield chunks for a smooth, streaming typing effect
33
  partial_message = ""
34
  for chunk in response:
35
+ if chunk.choices and chunk.choices.delta.content:
36
+ partial_message += chunk.choices.delta.content
37
  yield partial_message
38
 
39
  except Exception as e:
40
  yield f"An error occurred: {str(e)}"
41
 
42
+ # Define the ChatInterface cleanly for Gradio's newest standard
43
  demo = gr.ChatInterface(
44
  fn=predict,
 
45
  title="OpenRouter Chatbot",
46
+ description="Running seamlessly on the absolute latest version of Gradio.",
47
  )
48
 
49
  if __name__ == "__main__":
50
+ # Disable experimental SSR mode to prevent 404 heartbeat connection errors
51
  demo.launch(ssr_mode=False)