aymanemalih commited on
Commit
8700121
·
verified ·
1 Parent(s): b780e75

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +68 -0
main.py CHANGED
@@ -347,6 +347,74 @@ def ask_question():
347
  # Yazid Methode ends here
348
 
349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
  if __name__ == '__main__':
351
  app.debug = True
352
  app.run()
 
347
  # Yazid Methode ends here
348
 
349
 
350
+ # History approach starts here :
351
+
352
+ # Used functions starts here :
353
+ def extract_data(input_str):
354
+ pattern = r'(\d+(?:\.\d+)?)(?:\((\d+(?:\.\d+)?)\))?(?:([a-zA-Z]+))?'
355
+ match = re.match(pattern, input_str)
356
+ if match:
357
+ groups = match.groups()
358
+ return groups
359
+ else:
360
+ return None
361
+
362
+ def perform_search_and_get_results_histroy_filter(collection_name, query, reference,limit=6):
363
+ # Extract data from the reference
364
+ article_number, paragraph_number, characters = extract_data(reference)
365
+ print(article_number, paragraph_number, characters)
366
+ # Construct filters
367
+ filters = []
368
+ if article_number:
369
+ filters.append(models.FieldCondition(key="article", match=models.MatchValue(value=article_number+'a')))
370
+ if paragraph_number:
371
+ filters.append(models.FieldCondition(key="paragraph", match=models.MatchValue(value=paragraph_number+'a')))
372
+ if characters:
373
+ filters.append(models.FieldCondition(key="characters", match=models.MatchValue(value=characters+'a')))
374
+
375
+ # Perform the search
376
+ history_results = client.search(
377
+ collection_name=collection_name,
378
+ query_filter=models.Filter(should=filters),
379
+ query_vector=model.encode(query).tolist(),
380
+ limit=1
381
+ )
382
+
383
+ return history_results
384
+ # Used function ends here :
385
+ @app.route('/history', methods=['OPTIONS'])
386
+ def historyOptions():
387
+ response = make_response()
388
+ response.headers.add("Access-Control-Allow-Origin", "*")
389
+ response.headers.add("Access-Control-Allow-Methods", "POST")
390
+ response.headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization")
391
+ response.headers.add("Access-Control-Allow-Credentials", "true")
392
+ return response
393
+
394
+ @app.route('/history', methods=['POST'])
395
+ def history():
396
+ try:
397
+ data = request.get_json()
398
+ messages = data.get('messages', [])
399
+ finale_result = []
400
+ if messages:
401
+ prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
402
+
403
+ search_results = perform_search_and_get_results('history', prompt)
404
+ for result in search_results:
405
+ history_answers = result.payload['reponse'].splite(',')
406
+ for item in history_answers:
407
+ result = perform_search_and_get_results_histroy_filter('paragraph2', query, item, 3)
408
+ for res in result:
409
+ if res.score > 0.85:
410
+ finale_result.extend(res)
411
+ return jsonify({'result_history': finale_result})
412
+ else:
413
+ return jsonify({'error': 'Invalid request'}), 400
414
+ except Exception as e:
415
+ return jsonify({'error': str(e)}), 500
416
+
417
+ # History approach ends here :
418
  if __name__ == '__main__':
419
  app.debug = True
420
  app.run()