Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from PIL import Image
|
4 |
+
import pytesseract
|
5 |
+
from pdf2image import convert_from_path
|
6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
7 |
+
from langchain.prompts import PromptTemplate
|
8 |
+
from langchain.chains import RetrievalQA
|
9 |
+
from langchain.memory import ConversationBufferMemory
|
10 |
+
from langchain_groq import ChatGroq
|
11 |
+
from langchain_community.vectorstores import FAISS
|
12 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
13 |
+
|
14 |
+
# Initialize the Groq API Key and the model
|
15 |
+
os.environ["GROQ_API_KEY"] = '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'
|
16 |
+
llm = ChatGroq(
|
17 |
+
model='llama3-70b-8192',
|
18 |
+
temperature=0.5,
|
19 |
+
max_tokens=None,
|
20 |
+
timeout=None,
|
21 |
+
max_retries=2
|
22 |
+
)
|
23 |
+
|
24 |
+
# OCR functions
|
25 |
+
def ocr_image(image_path, language='eng+guj'):
|
26 |
+
img = Image.open(image_path)
|
27 |
+
return pytesseract.image_to_string(img, lang=language)
|
28 |
+
|
29 |
+
def ocr_pdf(pdf_path, language='eng+guj'):
|
30 |
+
images = convert_from_path(pdf_path)
|
31 |
+
all_text = "\n".join(pytesseract.image_to_string(img, lang=language) for img in images)
|
32 |
+
return all_text
|
33 |
+
|
34 |
+
def ocr_file(file_path):
|
35 |
+
ext = os.path.splitext(file_path)[1].lower()
|
36 |
+
if ext == ".pdf":
|
37 |
+
return ocr_pdf(file_path)
|
38 |
+
elif ext in [".jpg", ".jpeg", ".png", ".bmp"]:
|
39 |
+
return ocr_image(file_path)
|
40 |
+
else:
|
41 |
+
return "Unsupported file format."
|
42 |
+
|
43 |
+
def get_text_chunks(text):
|
44 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
45 |
+
return splitter.split_text(text)
|
46 |
+
|
47 |
+
def get_vector_store(chunks):
|
48 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
49 |
+
vector_store = FAISS.from_texts(chunks, embedding=embeddings)
|
50 |
+
os.makedirs("faiss_index", exist_ok=True)
|
51 |
+
vector_store.save_local("faiss_index")
|
52 |
+
return vector_store
|
53 |
+
|
54 |
+
# Conversational chain
|
55 |
+
def get_conversational_chain():
|
56 |
+
template = """<Insert your prompt here>"""
|
57 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-MiniLM-L6-v2")
|
58 |
+
vector_store = FAISS.load_local("faiss_index", embeddings)
|
59 |
+
qa_chain = RetrievalQA.from_chain_type(
|
60 |
+
llm,
|
61 |
+
retriever=vector_store.as_retriever(),
|
62 |
+
chain_type='stuff',
|
63 |
+
verbose=True,
|
64 |
+
chain_type_kwargs={
|
65 |
+
"prompt": PromptTemplate(input_variables=["history", "context", "question"], template=template),
|
66 |
+
"memory": ConversationBufferMemory(memory_key="history", input_key="question"),
|
67 |
+
}
|
68 |
+
)
|
69 |
+
return qa_chain
|
70 |
+
|
71 |
+
# File and question handling
|
72 |
+
def process_files(files, question):
|
73 |
+
text = ""
|
74 |
+
for file in files:
|
75 |
+
file_path = os.path.join("temp", file.name)
|
76 |
+
with open(file_path, "wb") as f:
|
77 |
+
f.write(file.read())
|
78 |
+
text += ocr_file(file_path) + "\n"
|
79 |
+
|
80 |
+
chunks = get_text_chunks(text)
|
81 |
+
vector_store = get_vector_store(chunks)
|
82 |
+
|
83 |
+
qa_chain = get_conversational_chain()
|
84 |
+
response = qa_chain({"query": question})
|
85 |
+
return response.get("result", "No result found.")
|
86 |
+
|
87 |
+
# Gradio Interface
|
88 |
+
def app(files, question):
|
89 |
+
return process_files(files, question)
|
90 |
+
|
91 |
+
iface = gr.Interface(
|
92 |
+
fn=app,
|
93 |
+
inputs=[gr.File(file_types=[".pdf", ".jpg", ".jpeg", ".png", ".bmp"], label="Upload Files"), gr.Textbox(label="Ask a Question")],
|
94 |
+
outputs="text",
|
95 |
+
title="OCR and Document Query System",
|
96 |
+
description="Upload PDF or image files and ask questions based on their content."
|
97 |
+
)
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
iface.launch()
|