Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,11 +78,35 @@ def embed_pdf(file, collection_name):
|
|
| 78 |
os.remove(file_path)
|
| 79 |
return {"message": f"Documents embedded in Weaviate collection '{collection_name}'"}
|
| 80 |
|
|
|
|
|
|
|
|
|
|
| 81 |
def retrieve_info():
|
| 82 |
query = request.json.get("query")
|
| 83 |
llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
|
| 84 |
qa = RetrievalQA.from_chain_type(llm, retriever=vectorstore.as_retriever())
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
return {"results": result}
|
| 87 |
|
| 88 |
# Gradio interface
|
|
@@ -106,4 +130,4 @@ iface.add_endpoint(
|
|
| 106 |
outputs="text"
|
| 107 |
)
|
| 108 |
|
| 109 |
-
iface.launch()
|
|
|
|
| 78 |
os.remove(file_path)
|
| 79 |
return {"message": f"Documents embedded in Weaviate collection '{collection_name}'"}
|
| 80 |
|
| 81 |
+
# Initialize Cohere client
|
| 82 |
+
co = cohere.Client(api_key=cohere_api_key)
|
| 83 |
+
|
| 84 |
def retrieve_info():
|
| 85 |
query = request.json.get("query")
|
| 86 |
llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
|
| 87 |
qa = RetrievalQA.from_chain_type(llm, retriever=vectorstore.as_retriever())
|
| 88 |
+
|
| 89 |
+
# Retrieve initial results
|
| 90 |
+
initial_results = qa({"query": query})
|
| 91 |
+
|
| 92 |
+
# Assuming initial_results are in the desired format, extract the top 25 documents
|
| 93 |
+
# Adjust this part according to the actual format of your initial_results
|
| 94 |
+
top_docs = initial_results[:25] # Adjust this if your result format is different
|
| 95 |
+
|
| 96 |
+
# Rerank the top 25 results
|
| 97 |
+
reranked_results = co.rerank(query=query, documents=top_docs, top_n=3, model='rerank-english-v2.0')
|
| 98 |
+
|
| 99 |
+
# Format the reranked results
|
| 100 |
+
formatted_results = []
|
| 101 |
+
for idx, r in enumerate(reranked_results):
|
| 102 |
+
formatted_result = {
|
| 103 |
+
"Document Rank": idx + 1,
|
| 104 |
+
"Document Index": r.index,
|
| 105 |
+
"Document": r.document['text'],
|
| 106 |
+
"Relevance Score": f"{r.relevance_score:.2f}"
|
| 107 |
+
}
|
| 108 |
+
formatted_results.append(formatted_result)
|
| 109 |
+
|
| 110 |
return {"results": result}
|
| 111 |
|
| 112 |
# Gradio interface
|
|
|
|
| 130 |
outputs="text"
|
| 131 |
)
|
| 132 |
|
| 133 |
+
iface.launch()
|