Shreyas094 commited on
Commit
b2aeef2
·
verified ·
1 Parent(s): e979894

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -37
app.py CHANGED
@@ -19,6 +19,9 @@ import logging
19
 
20
  # Set up basic configuration for logging
21
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
 
 
22
 
23
  # Environment variables and configurations
24
  huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
@@ -259,6 +262,7 @@ def get_response_from_cloudflare(query, num_calls=3, temperature=0.2):
259
 
260
  prompt = f"Write a detailed and complete response that answers the following user question: '{query}'"
261
 
 
262
  for i in range(num_calls):
263
  try:
264
  response = requests.post(
@@ -276,18 +280,21 @@ def get_response_from_cloudflare(query, num_calls=3, temperature=0.2):
276
  stream=True
277
  )
278
 
279
- partial_response = ""
280
  for line in response.iter_lines():
281
  if line:
282
  try:
283
  json_data = json.loads(line.decode('utf-8').split('data: ')[1])
284
- chunk = json_data['response']
285
- partial_response += chunk
286
- yield partial_response
287
- except json.JSONDecodeError:
 
288
  continue
289
  except Exception as e:
290
  print(f"Error in generating response from Cloudflare: {str(e)}")
 
 
 
291
 
292
  def get_response_with_search(query, model, num_calls=3, temperature=0.2):
293
  search_results = duckduckgo_search(query)
@@ -301,38 +308,8 @@ After writing the document, please provide a list of sources used in your respon
301
 
302
  if model == "@cf/meta/llama-3.1-8b-instruct":
303
  # Use Cloudflare API
304
- ACCOUNT_ID = "your-account-id" # Replace with your actual Cloudflare account ID
305
- AUTH_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
306
-
307
- main_content = ""
308
- for i in range(num_calls):
309
- try:
310
- response = requests.post(
311
- f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct",
312
- headers={"Authorization": f"Bearer {AUTH_TOKEN}"},
313
- json={
314
- "stream": True,
315
- "messages": [
316
- {"role": "system", "content": "You are a friendly assistant"},
317
- {"role": "user", "content": prompt}
318
- ],
319
- "max_tokens": 1000,
320
- "temperature": temperature
321
- },
322
- stream=True
323
- )
324
-
325
- for line in response.iter_lines():
326
- if line:
327
- try:
328
- json_data = json.loads(line.decode('utf-8').split('data: ')[1])
329
- chunk = json_data['response']
330
- main_content += chunk
331
- yield main_content, "" # Yield partial main content without sources
332
- except json.JSONDecodeError:
333
- continue
334
- except Exception as e:
335
- print(f"Error in generating response from Cloudflare: {str(e)}")
336
  else:
337
  # Use Hugging Face API
338
  client = InferenceClient(model, token=huggingface_token)
 
19
 
20
  # Set up basic configuration for logging
21
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
22
+ # In the get_response_from_cloudflare function:
23
+ logging.debug(f"Cloudflare API Response: {response.status_code}")
24
+ logging.debug(f"Cloudflare API Response Content: {response.text[:100]}...")
25
 
26
  # Environment variables and configurations
27
  huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
 
262
 
263
  prompt = f"Write a detailed and complete response that answers the following user question: '{query}'"
264
 
265
+ full_response = ""
266
  for i in range(num_calls):
267
  try:
268
  response = requests.post(
 
280
  stream=True
281
  )
282
 
 
283
  for line in response.iter_lines():
284
  if line:
285
  try:
286
  json_data = json.loads(line.decode('utf-8').split('data: ')[1])
287
+ chunk = json_data.get('response', '')
288
+ full_response += chunk
289
+ yield full_response
290
+ except (json.JSONDecodeError, IndexError) as e:
291
+ print(f"Error parsing response: {str(e)}")
292
  continue
293
  except Exception as e:
294
  print(f"Error in generating response from Cloudflare: {str(e)}")
295
+
296
+ if not full_response:
297
+ yield "I apologize, but I couldn't generate a response at this time. Please try again later."
298
 
299
  def get_response_with_search(query, model, num_calls=3, temperature=0.2):
300
  search_results = duckduckgo_search(query)
 
308
 
309
  if model == "@cf/meta/llama-3.1-8b-instruct":
310
  # Use Cloudflare API
311
+ for response in get_response_from_cloudflare(prompt, num_calls, temperature):
312
+ yield response, "" # Yield response without sources
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
  else:
314
  # Use Hugging Face API
315
  client = InferenceClient(model, token=huggingface_token)