Spaces:
Running
on
Zero
Running
on
Zero
Ayushnangia
commited on
Commit
•
6372be7
1
Parent(s):
3b6b76c
Update app.py
Browse files
app.py
CHANGED
@@ -267,47 +267,44 @@ from langchain_core.runnables import RunnablePassthrough
|
|
267 |
import chromadb
|
268 |
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
269 |
os.environ['MISTRAL_API_KEY'] = 'XuyOObDE7trMbpAeI7OXYr3dnmoWy3L0'
|
|
|
270 |
class VectorData():
|
271 |
def __init__(self):
|
272 |
embedding_model_name = 'l3cube-pune/punjabi-sentence-similarity-sbert'
|
273 |
-
|
274 |
-
|
|
|
275 |
self.embeddings = HuggingFaceEmbeddings(
|
276 |
model_name=embedding_model_name,
|
277 |
model_kwargs=model_kwargs
|
278 |
)
|
279 |
|
280 |
-
|
281 |
-
self.client = chromadb.PersistentClient(path="chroma_db")
|
282 |
-
|
283 |
-
# Create or get collection with embeddings
|
284 |
-
self.collection = self.client.get_or_create_collection(
|
285 |
-
name="my_collection",
|
286 |
-
metadata={"hnsw:space": "cosine"}
|
287 |
-
)
|
288 |
-
|
289 |
-
# Initialize vectorstore
|
290 |
-
self.vectorstore = Chroma(
|
291 |
-
client=self.client,
|
292 |
-
collection_name="my_collection",
|
293 |
-
embedding_function=self.embeddings
|
294 |
-
)
|
295 |
-
|
296 |
self.retriever = self.vectorstore.as_retriever()
|
297 |
self.ingested_files = []
|
298 |
-
|
299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
self.llm = ChatMistralAI(model="mistral-large-latest")
|
301 |
self.rag_chain = (
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
|
|
308 |
if file is not None:
|
309 |
self.ingested_files.append(file.name.split('/')[-1])
|
310 |
-
self.retriever, self.vectorstore = utils.add_doc(file,
|
311 |
self.rag_chain = (
|
312 |
{"context": self.retriever, "question": RunnablePassthrough()}
|
313 |
| self.prompt
|
@@ -316,9 +313,9 @@ class VectorData():
|
|
316 |
)
|
317 |
return [[name] for name in self.ingested_files]
|
318 |
|
319 |
-
def delete_file_by_name(self,
|
320 |
if file_name in self.ingested_files:
|
321 |
-
self.retriever, self.vectorstore = utils.delete_doc(file_name,
|
322 |
self.ingested_files.remove(file_name)
|
323 |
return [[name] for name in self.ingested_files]
|
324 |
|
@@ -333,9 +330,7 @@ class VectorData():
|
|
333 |
"ਆਰਗਨ ਆਪਣੇ ਸਾਥੀ ਦੇ ਆਪਣੀ ਪਤਨੀ ਪ੍ਰਤੀ ਸਤਿਕਾਰ ਅਤੇ ਸੇਵਾ ਨੂੰ ਕਿਵੇਂ ਵੇਖਾਉਂਦਾ ਹੈ?",
|
334 |
"ਜਦੋਂ ਲਕਸ਼ਮਣ ਨੇ ਭਗਵਾਨ ਰਾਮ ਨੂੰ ਜੰਗਲ ਵਿੱਚ ਜਾਣ ਦਾ ਫੈਸਲਾ ਕੀਤਾ ਤਾਂ ਇਹ ਬਿਰਤਾਂਤ ਉਸ ਦੀਆਂ ਭਾਵਨਾਵਾਂ ਨੂੰ ਕਿਵੇਂ ਬਿਆਨ ਕਰਦਾ ਹੈ?"
|
335 |
]
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
data_obj = VectorData()
|
340 |
|
341 |
# Function to handle question answering
|
@@ -345,52 +340,67 @@ def answer_question(question):
|
|
345 |
return "Please enter a question."
|
346 |
|
347 |
with gr.Blocks() as rag_interface:
|
|
|
348 |
gr.Markdown("# RAG Interface")
|
349 |
gr.Markdown("Manage documents and ask questions with a Retrieval-Augmented Generation (RAG) system.")
|
350 |
|
351 |
with gr.Row():
|
|
|
352 |
with gr.Column():
|
353 |
gr.Markdown("### File Management")
|
|
|
|
|
354 |
file_input = gr.File(label="Upload File to Ingest")
|
355 |
add_file_button = gr.Button("Ingest File")
|
356 |
|
|
|
|
|
|
|
357 |
ingested_files_box = gr.Dataframe(
|
358 |
headers=["Files"],
|
359 |
datatype="str",
|
360 |
-
row_count=4,
|
361 |
interactive=False
|
362 |
)
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
|
|
|
|
|
|
371 |
)
|
|
|
|
|
|
|
|
|
372 |
delete_button = gr.Button("Delete Selected")
|
373 |
|
|
|
374 |
def toggle_file_input(option):
|
375 |
return gr.update(visible=(option == "Delete by File Name"))
|
376 |
|
377 |
-
delete_option.change(
|
378 |
-
|
379 |
-
|
380 |
-
outputs=file_name_input
|
381 |
-
)
|
382 |
add_file_button.click(
|
383 |
fn=data_obj.add_file,
|
384 |
inputs=file_input,
|
385 |
outputs=ingested_files_box
|
386 |
)
|
387 |
|
|
|
388 |
def delete_action(delete_option, file_name):
|
389 |
if delete_option == "Delete by File Name" and file_name:
|
390 |
return data_obj.delete_file_by_name(file_name)
|
391 |
elif delete_option == "Delete All Files":
|
392 |
return data_obj.delete_all_files()
|
393 |
-
|
|
|
394 |
|
395 |
delete_button.click(
|
396 |
fn=delete_action,
|
@@ -398,12 +408,21 @@ with gr.Blocks() as rag_interface:
|
|
398 |
outputs=ingested_files_box
|
399 |
)
|
400 |
|
|
|
401 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
402 |
gr.Markdown("### Ask a Question")
|
403 |
-
example_questions = gr.Radio(
|
404 |
-
choices=data_obj.get_example_questions(),
|
405 |
-
label="Example Questions"
|
406 |
-
)
|
407 |
question_input = gr.Textbox(label="Enter your question")
|
408 |
ask_button = gr.Button("Get Answer")
|
409 |
answer_output = gr.Textbox(label="Answer", interactive=False)
|
@@ -411,42 +430,8 @@ with gr.Blocks() as rag_interface:
|
|
411 |
def set_example_question(example):
|
412 |
return gr.update(value=example)
|
413 |
|
414 |
-
example_questions.change(
|
415 |
-
|
416 |
-
inputs=example_questions,
|
417 |
-
outputs=question_input
|
418 |
-
)
|
419 |
-
ask_button.click(
|
420 |
-
fn=answer_question,
|
421 |
-
inputs=question_input,
|
422 |
-
outputs=answer_output
|
423 |
-
)
|
424 |
-
|
425 |
-
|
426 |
-
# Right Column: Question Answering
|
427 |
-
# with gr.Column():
|
428 |
-
# # gr.Markdown("### Ask a Question")
|
429 |
-
|
430 |
-
# # Question input
|
431 |
-
# # question_input = gr.Textbox(label="Enter your question")
|
432 |
-
|
433 |
-
# # # Get answer button and answer output
|
434 |
-
# # ask_button = gr.Button("Get Answer")
|
435 |
-
# # answer_output = gr.Textbox(label="Answer", interactive=False)
|
436 |
-
|
437 |
-
# # ask_button.click(fn=answer_question, inputs=question_input, outputs=answer_output)
|
438 |
-
|
439 |
-
# gr.Markdown("### Ask a Question")
|
440 |
-
# example_questions = gr.Radio(choices=data_obj.get_example_questions(), label="Example Questions")
|
441 |
-
# question_input = gr.Textbox(label="Enter your question")
|
442 |
-
# ask_button = gr.Button("Get Answer")
|
443 |
-
# answer_output = gr.Textbox(label="Answer", interactive=False)
|
444 |
-
|
445 |
-
# def set_example_question(example):
|
446 |
-
# return gr.update(value=example)
|
447 |
-
|
448 |
-
# example_questions.change(fn=set_example_question, inputs=example_questions, outputs=question_input)
|
449 |
-
# ask_button.click(fn=answer_question, inputs=question_input, outputs=answer_output)
|
450 |
|
451 |
|
452 |
with gr.Blocks() as demo:
|
|
|
267 |
import chromadb
|
268 |
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
269 |
os.environ['MISTRAL_API_KEY'] = 'XuyOObDE7trMbpAeI7OXYr3dnmoWy3L0'
|
270 |
+
|
271 |
class VectorData():
|
272 |
def __init__(self):
|
273 |
embedding_model_name = 'l3cube-pune/punjabi-sentence-similarity-sbert'
|
274 |
+
|
275 |
+
model_kwargs = {'device':'cpu',"trust_remote_code": True}
|
276 |
+
|
277 |
self.embeddings = HuggingFaceEmbeddings(
|
278 |
model_name=embedding_model_name,
|
279 |
model_kwargs=model_kwargs
|
280 |
)
|
281 |
|
282 |
+
self.vectorstore = Chroma(persist_directory="chroma_db", embedding_function=self.embeddings)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
self.retriever = self.vectorstore.as_retriever()
|
284 |
self.ingested_files = []
|
285 |
+
self.prompt = ChatPromptTemplate.from_messages(
|
286 |
+
[
|
287 |
+
(
|
288 |
+
"system",
|
289 |
+
"""ਦਿੱਤੇ ਗਏ ਸੰਦਰਭ ਦੇ ਆਧਾਰ 'ਤੇ ਸਵਾਲ ਦਾ ਜਵਾਬ ਦਿਓ। ਜੇਕਰ ਸਵਾਲ ਦਾ ਪ੍ਰਸੰਗ ਵੈਧ ਨਹੀਂ ਹੈ ਤਾਂ ਕੋਈ ਜਵਾਬ ਨਾ ਦਿਓ। ਹਮੇਸ਼ਾ ਜਵਾਬ ਦੇ ਅੰਤ ਵਿੱਚ ਸੰਦਰਭ ਦਾ ਸਰੋਤ ਦਿਓ:
|
290 |
+
{context}
|
291 |
+
""",
|
292 |
+
),
|
293 |
+
("human", "{question}"),
|
294 |
+
]
|
295 |
+
)
|
296 |
self.llm = ChatMistralAI(model="mistral-large-latest")
|
297 |
self.rag_chain = (
|
298 |
+
{"context": self.retriever, "question": RunnablePassthrough()}
|
299 |
+
| self.prompt
|
300 |
+
| self.llm
|
301 |
+
| StrOutputParser()
|
302 |
+
)
|
303 |
+
|
304 |
+
def add_file(self,file):
|
305 |
if file is not None:
|
306 |
self.ingested_files.append(file.name.split('/')[-1])
|
307 |
+
self.retriever, self.vectorstore = utils.add_doc(file,self.vectorstore)
|
308 |
self.rag_chain = (
|
309 |
{"context": self.retriever, "question": RunnablePassthrough()}
|
310 |
| self.prompt
|
|
|
313 |
)
|
314 |
return [[name] for name in self.ingested_files]
|
315 |
|
316 |
+
def delete_file_by_name(self,file_name):
|
317 |
if file_name in self.ingested_files:
|
318 |
+
self.retriever, self.vectorstore = utils.delete_doc(file_name,self.vectorstore)
|
319 |
self.ingested_files.remove(file_name)
|
320 |
return [[name] for name in self.ingested_files]
|
321 |
|
|
|
330 |
"ਆਰਗਨ ਆਪਣੇ ਸਾਥੀ ਦੇ ਆਪਣੀ ਪਤਨੀ ਪ੍ਰਤੀ ਸਤਿਕਾਰ ਅਤੇ ਸੇਵਾ ਨੂੰ ਕਿਵੇਂ ਵੇਖਾਉਂਦਾ ਹੈ?",
|
331 |
"ਜਦੋਂ ਲਕਸ਼ਮਣ ਨੇ ਭਗਵਾਨ ਰਾਮ ਨੂੰ ਜੰਗਲ ਵਿੱਚ ਜਾਣ ਦਾ ਫੈਸਲਾ ਕੀਤਾ ਤਾਂ ਇਹ ਬਿਰਤਾਂਤ ਉਸ ਦੀਆਂ ਭਾਵਨਾਵਾਂ ਨੂੰ ਕਿਵੇਂ ਬਿਆਨ ਕਰਦਾ ਹੈ?"
|
332 |
]
|
333 |
+
|
|
|
|
|
334 |
data_obj = VectorData()
|
335 |
|
336 |
# Function to handle question answering
|
|
|
340 |
return "Please enter a question."
|
341 |
|
342 |
with gr.Blocks() as rag_interface:
|
343 |
+
# Title and Description
|
344 |
gr.Markdown("# RAG Interface")
|
345 |
gr.Markdown("Manage documents and ask questions with a Retrieval-Augmented Generation (RAG) system.")
|
346 |
|
347 |
with gr.Row():
|
348 |
+
# Left Column: File Management
|
349 |
with gr.Column():
|
350 |
gr.Markdown("### File Management")
|
351 |
+
|
352 |
+
# File upload and ingest
|
353 |
file_input = gr.File(label="Upload File to Ingest")
|
354 |
add_file_button = gr.Button("Ingest File")
|
355 |
|
356 |
+
# Add examples for file upload with proper function and outputs
|
357 |
+
|
358 |
+
# Scrollable list for ingested files
|
359 |
ingested_files_box = gr.Dataframe(
|
360 |
headers=["Files"],
|
361 |
datatype="str",
|
362 |
+
row_count=4, # Limits the visible rows to create a scrollable view
|
363 |
interactive=False
|
364 |
)
|
365 |
+
gr.Examples(
|
366 |
+
examples=[
|
367 |
+
["Examples/RESULT_OCR.txt"],
|
368 |
+
["Examples/RESULT_OCR_2.txt"],
|
369 |
+
["Examples/RESULT_OCR_3.txt"]
|
370 |
+
],
|
371 |
+
inputs=file_input,
|
372 |
+
outputs=ingested_files_box,
|
373 |
+
fn=data_obj.add_file,
|
374 |
+
cache_examples=True,
|
375 |
+
label="Example Files"
|
376 |
)
|
377 |
+
|
378 |
+
# Radio buttons to choose delete option
|
379 |
+
delete_option = gr.Radio(choices=["Delete by File Name", "Delete All Files"], label="Delete Option")
|
380 |
+
file_name_input = gr.Textbox(label="Enter File Name to Delete", visible=False)
|
381 |
delete_button = gr.Button("Delete Selected")
|
382 |
|
383 |
+
# Show or hide file name input based on delete option selection
|
384 |
def toggle_file_input(option):
|
385 |
return gr.update(visible=(option == "Delete by File Name"))
|
386 |
|
387 |
+
delete_option.change(fn=toggle_file_input, inputs=delete_option, outputs=file_name_input)
|
388 |
+
|
389 |
+
# Handle file ingestion
|
|
|
|
|
390 |
add_file_button.click(
|
391 |
fn=data_obj.add_file,
|
392 |
inputs=file_input,
|
393 |
outputs=ingested_files_box
|
394 |
)
|
395 |
|
396 |
+
# Handle delete based on selected option
|
397 |
def delete_action(delete_option, file_name):
|
398 |
if delete_option == "Delete by File Name" and file_name:
|
399 |
return data_obj.delete_file_by_name(file_name)
|
400 |
elif delete_option == "Delete All Files":
|
401 |
return data_obj.delete_all_files()
|
402 |
+
else:
|
403 |
+
return [[name] for name in data_obj.ingested_files]
|
404 |
|
405 |
delete_button.click(
|
406 |
fn=delete_action,
|
|
|
408 |
outputs=ingested_files_box
|
409 |
)
|
410 |
|
411 |
+
# Right Column: Question Answering
|
412 |
with gr.Column():
|
413 |
+
# gr.Markdown("### Ask a Question")
|
414 |
+
|
415 |
+
# Question input
|
416 |
+
# question_input = gr.Textbox(label="Enter your question")
|
417 |
+
|
418 |
+
# # Get answer button and answer output
|
419 |
+
# ask_button = gr.Button("Get Answer")
|
420 |
+
# answer_output = gr.Textbox(label="Answer", interactive=False)
|
421 |
+
|
422 |
+
# ask_button.click(fn=answer_question, inputs=question_input, outputs=answer_output)
|
423 |
+
|
424 |
gr.Markdown("### Ask a Question")
|
425 |
+
example_questions = gr.Radio(choices=data_obj.get_example_questions(), label="Example Questions")
|
|
|
|
|
|
|
426 |
question_input = gr.Textbox(label="Enter your question")
|
427 |
ask_button = gr.Button("Get Answer")
|
428 |
answer_output = gr.Textbox(label="Answer", interactive=False)
|
|
|
430 |
def set_example_question(example):
|
431 |
return gr.update(value=example)
|
432 |
|
433 |
+
example_questions.change(fn=set_example_question, inputs=example_questions, outputs=question_input)
|
434 |
+
ask_button.click(fn=answer_question, inputs=question_input, outputs=answer_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
435 |
|
436 |
|
437 |
with gr.Blocks() as demo:
|