Shreyas094 commited on
Commit
40983c7
1 Parent(s): 5e31ee5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -90
app.py CHANGED
@@ -187,96 +187,6 @@ def delete_documents(selected_docs):
187
 
188
  return f"Deleted documents: {', '.join(deleted_docs)}", display_documents()
189
 
190
- def generate_chunked_response(prompt, model, max_tokens=10000, num_calls=3, temperature=0.2, should_stop=False):
191
- print(f"Starting generate_chunked_response with {num_calls} calls")
192
- full_response = ""
193
- messages = [{"role": "user", "content": prompt}]
194
-
195
- if model == "@cf/meta/llama-3.1-8b-instruct":
196
- # Cloudflare API
197
- for i in range(num_calls):
198
- print(f"Starting Cloudflare API call {i+1}")
199
- if should_stop:
200
- print("Stop clicked, breaking loop")
201
- break
202
- try:
203
- response = requests.post(
204
- f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct",
205
- headers={"Authorization": f"Bearer {API_TOKEN}"},
206
- json={
207
- "stream": true,
208
- "messages": [
209
- {"role": "system", "content": "You are a friendly assistant"},
210
- {"role": "user", "content": prompt}
211
- ],
212
- "max_tokens": max_tokens,
213
- "temperature": temperature
214
- },
215
- stream=true
216
- )
217
-
218
- for line in response.iter_lines():
219
- if should_stop:
220
- print("Stop clicked during streaming, breaking")
221
- break
222
- if line:
223
- try:
224
- json_data = json.loads(line.decode('utf-8').split('data: ')[1])
225
- chunk = json_data['response']
226
- full_response += chunk
227
- except json.JSONDecodeError:
228
- continue
229
- print(f"Cloudflare API call {i+1} completed")
230
- except Exception as e:
231
- print(f"Error in generating response from Cloudflare: {str(e)}")
232
- else:
233
- # Original Hugging Face API logic
234
- client = InferenceClient(model, token=huggingface_token)
235
-
236
- for i in range(num_calls):
237
- print(f"Starting Hugging Face API call {i+1}")
238
- if should_stop:
239
- print("Stop clicked, breaking loop")
240
- break
241
- try:
242
- for message in client.chat_completion(
243
- messages=messages,
244
- max_tokens=max_tokens,
245
- temperature=temperature,
246
- stream=True,
247
- ):
248
- if should_stop:
249
- print("Stop clicked during streaming, breaking")
250
- break
251
- if message.choices and message.choices[0].delta and message.choices[0].delta.content:
252
- chunk = message.choices[0].delta.content
253
- full_response += chunk
254
- print(f"Hugging Face API call {i+1} completed")
255
- except Exception as e:
256
- print(f"Error in generating response from Hugging Face: {str(e)}")
257
-
258
- # Clean up the response
259
- clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', full_response, flags=re.DOTALL)
260
- clean_response = clean_response.replace("Using the following context:", "").strip()
261
- clean_response = clean_response.replace("Using the following context from the PDF documents:", "").strip()
262
-
263
- # Remove duplicate paragraphs and sentences
264
- paragraphs = clean_response.split('\n\n')
265
- unique_paragraphs = []
266
- for paragraph in paragraphs:
267
- if paragraph not in unique_paragraphs:
268
- sentences = paragraph.split('. ')
269
- unique_sentences = []
270
- for sentence in sentences:
271
- if sentence not in unique_sentences:
272
- unique_sentences.append(sentence)
273
- unique_paragraphs.append('. '.join(unique_sentences))
274
-
275
- final_response = '\n\n'.join(unique_paragraphs)
276
-
277
- print(f"Final clean response: {final_response[:100]}...")
278
- return final_response
279
-
280
  def chatbot_interface(message, history, model, temperature, num_calls):
281
  if not message.strip():
282
  return "", history
 
187
 
188
  return f"Deleted documents: {', '.join(deleted_docs)}", display_documents()
189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  def chatbot_interface(message, history, model, temperature, num_calls):
191
  if not message.strip():
192
  return "", history