Shreyas094 commited on
Commit
063c321
·
verified ·
1 Parent(s): 28413b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -15
app.py CHANGED
@@ -298,21 +298,57 @@ def get_response_with_search(query, model, num_calls=3, temperature=0.2):
298
  {context}
299
  Write a detailed and complete research document that fulfills the following user request: '{query}'
300
  After writing the document, please provide a list of sources used in your response."""
301
-
302
- client = InferenceClient(model, token=huggingface_token)
303
-
304
- main_content = ""
305
- for i in range(num_calls):
306
- for message in client.chat_completion(
307
- messages=[{"role": "user", "content": prompt}],
308
- max_tokens=1000,
309
- temperature=temperature,
310
- stream=True,
311
- ):
312
- if message.choices and message.choices[0].delta and message.choices[0].delta.content:
313
- chunk = message.choices[0].delta.content
314
- main_content += chunk
315
- yield main_content, "" # Yield partial main content without sources
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
 
317
  def get_response_from_pdf(query, model, num_calls=3, temperature=0.2):
318
  embed = get_embeddings()
 
298
  {context}
299
  Write a detailed and complete research document that fulfills the following user request: '{query}'
300
  After writing the document, please provide a list of sources used in your response."""
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)
339
+
340
+ main_content = ""
341
+ for i in range(num_calls):
342
+ for message in client.chat_completion(
343
+ messages=[{"role": "user", "content": prompt}],
344
+ max_tokens=1000,
345
+ temperature=temperature,
346
+ stream=True,
347
+ ):
348
+ if message.choices and message.choices[0].delta and message.choices[0].delta.content:
349
+ chunk = message.choices[0].delta.content
350
+ main_content += chunk
351
+ yield main_content, "" # Yield partial main content without sources
352
 
353
  def get_response_from_pdf(query, model, num_calls=3, temperature=0.2):
354
  embed = get_embeddings()