Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -18,6 +18,7 @@ import openai
|
|
18 |
import numpy as np
|
19 |
from pinecone.grpc import PineconeGRPC as Pinecone
|
20 |
import gradio as gr
|
|
|
21 |
|
22 |
|
23 |
load_dotenv()
|
@@ -35,21 +36,7 @@ try:
|
|
35 |
except Exception as e:
|
36 |
print(f"Error connecting to Pinecone: {str(e)}")
|
37 |
|
38 |
-
|
39 |
-
# pc.create_index(
|
40 |
-
# name=pinecone_index_name,
|
41 |
-
# dimension=1024, #1024- voyage-law-2, # '1536' is the dimension for ada-002 embeddings
|
42 |
-
# metric='cosine',
|
43 |
-
# spec=ServerlessSpec(
|
44 |
-
# cloud='aws',
|
45 |
-
# region=pinecone_environment
|
46 |
-
# )
|
47 |
-
# )
|
48 |
-
# print("Pinecone Index provisioned")
|
49 |
-
# else:
|
50 |
-
# print("Pinecone Index already provisioned")
|
51 |
-
|
52 |
-
#embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
|
53 |
embeddings = VoyageAIEmbeddings(
|
54 |
voyage_api_key=voyage_api_key, model="voyage-law-2"
|
55 |
)
|
@@ -170,6 +157,52 @@ def complete_workflow(query):
|
|
170 |
except Exception as e:
|
171 |
return {"results": [], "total_results": 0}, f"Error in workflow: {str(e)}"
|
172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
def gradio_app():
|
174 |
with gr.Blocks(css=".result-output {width: 150%; font-size: 16px; padding: 10px;}") as app:
|
175 |
gr.Markdown("### Intelligent Document Search Prototype-v0.1.2 ")
|
@@ -184,7 +217,7 @@ def gradio_app():
|
|
184 |
titles_output = gr.Textbox(label="Document Titles", interactive=False) # New Textbox for Titles
|
185 |
|
186 |
search_btn.click(
|
187 |
-
|
188 |
inputs=user_query,
|
189 |
outputs=[result_output, titles_output],
|
190 |
)
|
|
|
18 |
import numpy as np
|
19 |
from pinecone.grpc import PineconeGRPC as Pinecone
|
20 |
import gradio as gr
|
21 |
+
import asyncio
|
22 |
|
23 |
|
24 |
load_dotenv()
|
|
|
36 |
except Exception as e:
|
37 |
print(f"Error connecting to Pinecone: {str(e)}")
|
38 |
|
39 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
embeddings = VoyageAIEmbeddings(
|
41 |
voyage_api_key=voyage_api_key, model="voyage-law-2"
|
42 |
)
|
|
|
157 |
except Exception as e:
|
158 |
return {"results": [], "total_results": 0}, f"Error in workflow: {str(e)}"
|
159 |
|
160 |
+
|
161 |
+
async def async_complete_workflow(query):
|
162 |
+
try:
|
163 |
+
search_task = asyncio.to_thread(search_documents, query)
|
164 |
+
context_data = await search_task # Run search in parallel
|
165 |
+
|
166 |
+
rerank_task = asyncio.to_thread(rerank, query, context_data)
|
167 |
+
reranked = await rerank_task # Run rerank in parallel
|
168 |
+
|
169 |
+
# Process results
|
170 |
+
context_data = [
|
171 |
+
{
|
172 |
+
'chunk_id': entry['document']['chunk_id'],
|
173 |
+
'doc_id': entry['document']['doc_id'],
|
174 |
+
'title': entry['document']['title'],
|
175 |
+
'text': entry['document']['text'],
|
176 |
+
'page_number': str(entry['document']['page_number']),
|
177 |
+
'score': str(entry['score'])
|
178 |
+
}
|
179 |
+
for entry in reranked.data
|
180 |
+
]
|
181 |
+
|
182 |
+
# Generate responses
|
183 |
+
output_tasks = [asyncio.to_thread(generate_output, doc["text"], query) for doc in context_data]
|
184 |
+
outputs = await asyncio.gather(*output_tasks) # Run LLM calls in parallel
|
185 |
+
|
186 |
+
results = {
|
187 |
+
"results": [
|
188 |
+
{
|
189 |
+
"natural_language_output": outputs[i],
|
190 |
+
"chunk_id": doc["chunk_id"],
|
191 |
+
"document_id": doc["doc_id"],
|
192 |
+
"title": doc["title"],
|
193 |
+
"text": doc["text"],
|
194 |
+
"page_number": doc["page_number"],
|
195 |
+
"score": doc["score"],
|
196 |
+
}
|
197 |
+
for i, doc in enumerate(context_data)
|
198 |
+
],
|
199 |
+
"total_results": len(context_data)
|
200 |
+
}
|
201 |
+
|
202 |
+
return results
|
203 |
+
except Exception as e:
|
204 |
+
return {"results": [], "total_results": 0}, f"Error in workflow: {str(e)}"
|
205 |
+
|
206 |
def gradio_app():
|
207 |
with gr.Blocks(css=".result-output {width: 150%; font-size: 16px; padding: 10px;}") as app:
|
208 |
gr.Markdown("### Intelligent Document Search Prototype-v0.1.2 ")
|
|
|
217 |
titles_output = gr.Textbox(label="Document Titles", interactive=False) # New Textbox for Titles
|
218 |
|
219 |
search_btn.click(
|
220 |
+
async_complete_workflow,
|
221 |
inputs=user_query,
|
222 |
outputs=[result_output, titles_output],
|
223 |
)
|