Spaces:
Runtime error
Runtime error
Commit
Β·
3a8b68a
1
Parent(s):
6f9cae9
Adding chat
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from llama_index.core import Settings
|
|
| 6 |
from llama_index.llms.gemini import Gemini
|
| 7 |
from llama_index.core import Document, VectorStoreIndex
|
| 8 |
from llama_index.embeddings.gemini import GeminiEmbedding
|
|
|
|
| 9 |
|
| 10 |
reader = easyocr.Reader(['en'])
|
| 11 |
|
|
@@ -16,23 +17,32 @@ gemini_embedding_model = GeminiEmbedding(api_key=os.getenv('GEMINI_API_KEY'), mo
|
|
| 16 |
Settings.llm = llm
|
| 17 |
Settings.embed_model = gemini_embedding_model
|
| 18 |
|
| 19 |
-
def
|
| 20 |
output = reader.readtext(img_path, detail=0, slope_ths=0.7, ycenter_ths=0.9,
|
| 21 |
height_ths=0.8, width_ths=width_ths, add_margin=0.2)
|
| 22 |
|
| 23 |
output = "\n".join(output)
|
| 24 |
|
| 25 |
-
# create a Document object from the extracted text
|
| 26 |
doc = Document(text = output)
|
| 27 |
|
| 28 |
-
# Create an index from the documents and save it to the disk.
|
| 29 |
index = VectorStoreIndex.from_documents([doc])
|
| 30 |
|
| 31 |
-
# save the index
|
| 32 |
index.storage_context.persist(persist_dir = "./receiptsembeddings")
|
| 33 |
|
| 34 |
return output
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
title = "Receipt RAG"
|
| 37 |
description = "A simple Gradio interface to query receipts using RAG"
|
| 38 |
examples = [["data/receipt_00000.JPG", 7.7],
|
|
@@ -50,17 +60,19 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 50 |
with gr.Column():
|
| 51 |
ocr_out = gr.Textbox(label="OCR Output", type="text")
|
| 52 |
|
| 53 |
-
submit_btn.click(
|
| 54 |
-
clear_btn.click(lambda: [None, 7.7], inputs=[image, width_ths])
|
| 55 |
|
| 56 |
examples_obj = gr.Examples(examples=examples, inputs=[image, width_ths])
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
demo.launch()
|
|
|
|
| 6 |
from llama_index.llms.gemini import Gemini
|
| 7 |
from llama_index.core import Document, VectorStoreIndex
|
| 8 |
from llama_index.embeddings.gemini import GeminiEmbedding
|
| 9 |
+
from llama_index.core import load_index_from_storage, StorageContext
|
| 10 |
|
| 11 |
reader = easyocr.Reader(['en'])
|
| 12 |
|
|
|
|
| 17 |
Settings.llm = llm
|
| 18 |
Settings.embed_model = gemini_embedding_model
|
| 19 |
|
| 20 |
+
def ocr_inference(img_path, width_ths):
|
| 21 |
output = reader.readtext(img_path, detail=0, slope_ths=0.7, ycenter_ths=0.9,
|
| 22 |
height_ths=0.8, width_ths=width_ths, add_margin=0.2)
|
| 23 |
|
| 24 |
output = "\n".join(output)
|
| 25 |
|
|
|
|
| 26 |
doc = Document(text = output)
|
| 27 |
|
|
|
|
| 28 |
index = VectorStoreIndex.from_documents([doc])
|
| 29 |
|
|
|
|
| 30 |
index.storage_context.persist(persist_dir = "./receiptsembeddings")
|
| 31 |
|
| 32 |
return output
|
| 33 |
|
| 34 |
+
def inference(question):
|
| 35 |
+
persist_dir = "./receiptsembeddings"
|
| 36 |
+
|
| 37 |
+
storage_context = StorageContext.from_defaults(persist_dir = persist_dir)
|
| 38 |
+
index = load_index_from_storage(storage_context)
|
| 39 |
+
|
| 40 |
+
query_engine = index.as_query_engine()
|
| 41 |
+
|
| 42 |
+
response = query_engine.query(question)
|
| 43 |
+
|
| 44 |
+
return response
|
| 45 |
+
|
| 46 |
title = "Receipt RAG"
|
| 47 |
description = "A simple Gradio interface to query receipts using RAG"
|
| 48 |
examples = [["data/receipt_00000.JPG", 7.7],
|
|
|
|
| 60 |
with gr.Column():
|
| 61 |
ocr_out = gr.Textbox(label="OCR Output", type="text")
|
| 62 |
|
| 63 |
+
submit_btn.click(ocr_inference, inputs=[image, width_ths], outputs=ocr_out)
|
|
|
|
| 64 |
|
| 65 |
examples_obj = gr.Examples(examples=examples, inputs=[image, width_ths])
|
| 66 |
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
text = gr.Textbox(label="Question", type="text")
|
| 70 |
+
with gr.Row():
|
| 71 |
+
chat_clear_btn = gr.ClearButton(components=[text])
|
| 72 |
+
chat_submit_btn = gr.Button("Submit", variant='primary')
|
| 73 |
+
with gr.Column():
|
| 74 |
+
chat_out = gr.Textbox(label="Response", type="text")
|
| 75 |
+
|
| 76 |
+
chat_clear_btn.click(inference, inputs=[text], outputs=[chat_out])
|
| 77 |
|
| 78 |
demo.launch()
|