Spaces:
Running
on
T4
Running
on
T4
File size: 3,823 Bytes
2e2dda5 6f4ec47 c2c6e99 2e2dda5 c2c6e99 2e2dda5 c2c6e99 2e2dda5 c2c6e99 2e2dda5 6f4ec47 2e2dda5 c2c6e99 2e2dda5 6f4ec47 2e2dda5 6f4ec47 2e2dda5 6f4ec47 2e2dda5 6f4ec47 2e2dda5 6f4ec47 2e2dda5 6f4ec47 2e2dda5 6f4ec47 2e2dda5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
import boto3
from botocore.exceptions import ClientError
import pprint
import time
import streamlit as st
from sentence_transformers import CrossEncoder
#model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2", max_length=512)
####### Add this Kendra Rescore ranking
#kendra_ranking = boto3.client("kendra-ranking",region_name = 'us-east-1')
#print("Create a rescore execution plan.")
# Provide a name for the rescore execution plan
#name = "MyRescoreExecutionPlan"
# Set your required additional capacity units
# Don't set capacity units if you don't require more than 1 unit given by default
#capacity_units = 2
# try:
# rescore_execution_plan_response = kendra_ranking.create_rescore_execution_plan(
# Name = name,
# CapacityUnits = {"RescoreCapacityUnits":capacity_units}
# )
# pprint.pprint(rescore_execution_plan_response)
# rescore_execution_plan_id = rescore_execution_plan_response["Id"]
# print("Wait for Amazon Kendra to create the rescore execution plan.")
# while True:
# # Get the details of the rescore execution plan, such as the status
# rescore_execution_plan_description = kendra_ranking.describe_rescore_execution_plan(
# Id = rescore_execution_plan_id
# )
# # When status is not CREATING quit.
# status = rescore_execution_plan_description["Status"]
# print(" Creating rescore execution plan. Status: "+status)
# time.sleep(60)
# if status != "CREATING":
# break
# except ClientError as e:
# print("%s" % e)
# print("Program ends.")
#########################
def re_rank(self_, rerank_type, search_type, question, answers):
ans = []
ids = []
ques_ans = []
query = question[0]['question']
for i in answers[0]['answer']:
if(self_ == "search"):
ans.append({
"Id": i['id'],
"Body": i["desc"],
"OriginalScore": i['score'],
"Title":i["desc"]
})
ids.append(i['id'])
ques_ans.append((query,i["desc"]))
else:
ans.append({'text':i})
ques_ans.append((query,i))
re_ranked = [{}]
####### Add this Kendra Rescore ranking
# if(rerank_type == 'Kendra Rescore'):
# rescore_response = kendra_ranking.rescore(
# RescoreExecutionPlanId = 'b2a4d4f3-98ff-4e17-8b69-4c61ed7d91eb',
# SearchQuery = query,
# Documents = ans
# )
# re_ranked[0]['answer']=[]
# for result in rescore_response["ResultItems"]:
# pos_ = ids.index(result['DocumentId'])
# re_ranked[0]['answer'].append(answers[0]['answer'][pos_])
# re_ranked[0]['search_type']=search_type,
# re_ranked[0]['id'] = len(question)
# return re_ranked
# if(rerank_type == 'Cross Encoder'):
# scores = model.predict(
# ques_ans
# )
# index__ = 0
# for i in ans:
# i['new_score'] = scores[index__]
# index__ = index__+1
# ans_sorted = sorted(ans, key=lambda d: d['new_score'],reverse=True)
# def retreive_only_text(item):
# return item['text']
# if(self_ == 'rag'):
# return list(map(retreive_only_text, ans_sorted))
# re_ranked[0]['answer']=[]
# for j in ans_sorted:
# pos_ = ids.index(j['Id'])
# re_ranked[0]['answer'].append(answers[0]['answer'][pos_])
# re_ranked[0]['search_type']= search_type,
# re_ranked[0]['id'] = len(question)
# return re_ranked
|