Shreyas094 commited on
Commit
922ee31
·
verified ·
1 Parent(s): 58e81de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -19
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 = {"Authorization": f"Bearer {API_TOKEN}"}
 
 
 
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
- response = requests.post(f"{API_BASE_URL}{model}", headers=headers, json={"messages": inputs})
272
-
273
- logging.debug(f"Cloudflare API Response Status: {response.status_code}")
274
- logging.debug(f"Cloudflare API Response Headers: {response.headers}")
275
- logging.debug(f"Cloudflare API Response Content: {response.text[:500]}...") # First 500 characters
276
-
277
- if response.status_code == 200:
278
- json_response = response.json()
279
- if 'result' in json_response and 'response' in json_response['result']:
280
- chunk = json_response['result']['response']
281
- full_response += chunk
282
- yield full_response
 
 
 
 
283
  else:
284
- logging.error(f"Unexpected JSON structure: {json_response}")
285
- yield "I apologize, but I received an unexpected response format. Please try again later."
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)