Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -62,6 +62,7 @@ if __name__ == "__main__":
|
|
62 |
import os
|
63 |
import gradio as gr
|
64 |
from huggingface_hub import InferenceClient
|
|
|
65 |
|
66 |
# Retrieve the API token from the environment variable
|
67 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
@@ -82,12 +83,27 @@ def hf_chat(user_input):
|
|
82 |
{"role": "user", "content": user_input}
|
83 |
]
|
84 |
response = ""
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
return response
|
92 |
|
93 |
# Gradio interface
|
|
|
62 |
import os
|
63 |
import gradio as gr
|
64 |
from huggingface_hub import InferenceClient
|
65 |
+
import json
|
66 |
|
67 |
# Retrieve the API token from the environment variable
|
68 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
|
|
83 |
{"role": "user", "content": user_input}
|
84 |
]
|
85 |
response = ""
|
86 |
+
|
87 |
+
try:
|
88 |
+
# Stream the response
|
89 |
+
for message in client.chat_completion(
|
90 |
+
messages=messages,
|
91 |
+
max_tokens=500,
|
92 |
+
stream=True,
|
93 |
+
):
|
94 |
+
try:
|
95 |
+
# Parse each part of the response carefully
|
96 |
+
content = message.choices[0].delta.content
|
97 |
+
response += content
|
98 |
+
except (KeyError, json.JSONDecodeError) as e:
|
99 |
+
# Print error details for debugging
|
100 |
+
print(f"Error while parsing response: {e}")
|
101 |
+
continue # Continue receiving the stream
|
102 |
+
|
103 |
+
except Exception as e:
|
104 |
+
# Catch and print any unexpected errors during the stream
|
105 |
+
return f"Error occurred: {str(e)}"
|
106 |
+
|
107 |
return response
|
108 |
|
109 |
# Gradio interface
|