Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -252,7 +252,10 @@ def respond(message, history, model, temperature, num_calls, use_web_search):
|
|
252 |
logging.basicConfig(level=logging.DEBUG)
|
253 |
|
254 |
def get_response_from_cloudflare(query, num_calls=3, temperature=0.2):
|
255 |
-
headers = {
|
|
|
|
|
|
|
256 |
model = "@cf/meta/llama-3.1-8b-instruct"
|
257 |
|
258 |
logging.debug(f"API_BASE_URL: {API_BASE_URL}")
|
@@ -265,27 +268,33 @@ def get_response_from_cloudflare(query, num_calls=3, temperature=0.2):
|
|
265 |
{"role": "user", "content": prompt}
|
266 |
]
|
267 |
|
|
|
|
|
|
|
|
|
|
|
268 |
full_response = ""
|
269 |
for i in range(num_calls):
|
270 |
try:
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
|
|
|
|
|
|
|
|
283 |
else:
|
284 |
-
logging.error(f"
|
285 |
-
yield "I apologize, but I
|
286 |
-
else:
|
287 |
-
logging.error(f"HTTP Error: {response.status_code}, Response: {response.text}")
|
288 |
-
yield f"I apologize, but I encountered an HTTP error: {response.status_code}. Please try again later."
|
289 |
except Exception as e:
|
290 |
logging.error(f"Error in generating response from Cloudflare: {str(e)}")
|
291 |
yield f"I apologize, but an error occurred: {str(e)}. Please try again later."
|
@@ -306,7 +315,7 @@ After writing the document, please provide a list of sources used in your respon
|
|
306 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
307 |
# Use Cloudflare API
|
308 |
for response in get_response_from_cloudflare(prompt, num_calls, temperature):
|
309 |
-
yield response, "" # Yield response without sources
|
310 |
else:
|
311 |
# Use Hugging Face API
|
312 |
client = InferenceClient(model, token=huggingface_token)
|
|
|
252 |
logging.basicConfig(level=logging.DEBUG)
|
253 |
|
254 |
def get_response_from_cloudflare(query, num_calls=3, temperature=0.2):
|
255 |
+
headers = {
|
256 |
+
"Authorization": f"Bearer {API_TOKEN}",
|
257 |
+
"Content-Type": "application/json"
|
258 |
+
}
|
259 |
model = "@cf/meta/llama-3.1-8b-instruct"
|
260 |
|
261 |
logging.debug(f"API_BASE_URL: {API_BASE_URL}")
|
|
|
268 |
{"role": "user", "content": prompt}
|
269 |
]
|
270 |
|
271 |
+
payload = {
|
272 |
+
"messages": inputs,
|
273 |
+
"stream": True
|
274 |
+
}
|
275 |
+
|
276 |
full_response = ""
|
277 |
for i in range(num_calls):
|
278 |
try:
|
279 |
+
with requests.post(f"{API_BASE_URL}{model}", headers=headers, json=payload, stream=True) as response:
|
280 |
+
logging.debug(f"Cloudflare API Response Status: {response.status_code}")
|
281 |
+
logging.debug(f"Cloudflare API Response Headers: {response.headers}")
|
282 |
+
|
283 |
+
if response.status_code == 200:
|
284 |
+
for line in response.iter_lines():
|
285 |
+
if line:
|
286 |
+
try:
|
287 |
+
json_response = json.loads(line.decode('utf-8').split('data: ')[1])
|
288 |
+
if 'response' in json_response:
|
289 |
+
chunk = json_response['response']
|
290 |
+
full_response += chunk
|
291 |
+
yield full_response
|
292 |
+
except (json.JSONDecodeError, IndexError) as e:
|
293 |
+
logging.error(f"Error parsing streaming response: {str(e)}")
|
294 |
+
continue
|
295 |
else:
|
296 |
+
logging.error(f"HTTP Error: {response.status_code}, Response: {response.text}")
|
297 |
+
yield f"I apologize, but I encountered an HTTP error: {response.status_code}. Please try again later."
|
|
|
|
|
|
|
298 |
except Exception as e:
|
299 |
logging.error(f"Error in generating response from Cloudflare: {str(e)}")
|
300 |
yield f"I apologize, but an error occurred: {str(e)}. Please try again later."
|
|
|
315 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
316 |
# Use Cloudflare API
|
317 |
for response in get_response_from_cloudflare(prompt, num_calls, temperature):
|
318 |
+
yield response, "" # Yield streaming response without sources
|
319 |
else:
|
320 |
# Use Hugging Face API
|
321 |
client = InferenceClient(model, token=huggingface_token)
|