Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,6 @@ from langchain.vectorstores import FAISS
|
|
| 6 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
from langchain.chains import RetrievalQA
|
| 8 |
from langchain.llms import HuggingFacePipeline
|
| 9 |
-
|
| 10 |
-
# 1. Load Granite 2B model
|
| 11 |
model_id = "ibm-granite/granite-3.3-2b-instruct"
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -24,8 +22,6 @@ def extract_text(pdf_file):
|
|
| 24 |
if page.extract_text():
|
| 25 |
text += page.extract_text() + "\n"
|
| 26 |
return text
|
| 27 |
-
|
| 28 |
-
# 3. Build retrieval-based QA chain
|
| 29 |
def build_qa_chain(pdf_text):
|
| 30 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 31 |
chunks = splitter.split_text(pdf_text)
|
|
@@ -52,8 +48,7 @@ def process_pdf(file):
|
|
| 52 |
global qa_chain
|
| 53 |
text = extract_text(file)
|
| 54 |
qa_chain = build_qa_chain(text)
|
| 55 |
-
return
|
| 56 |
-
|
| 57 |
def answer_question(question):
|
| 58 |
if qa_chain is None:
|
| 59 |
return "❌ Please upload a PDF first."
|
|
@@ -73,5 +68,4 @@ with gr.Blocks() as demo:
|
|
| 73 |
answer_output = gr.Textbox(label="Answer")
|
| 74 |
ask_btn = gr.Button("Ask")
|
| 75 |
ask_btn.click(answer_question, question_input, answer_output)
|
| 76 |
-
|
| 77 |
-
demo.launch()
|
|
|
|
| 6 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
from langchain.chains import RetrievalQA
|
| 8 |
from langchain.llms import HuggingFacePipeline
|
|
|
|
|
|
|
| 9 |
model_id = "ibm-granite/granite-3.3-2b-instruct"
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 22 |
if page.extract_text():
|
| 23 |
text += page.extract_text() + "\n"
|
| 24 |
return text
|
|
|
|
|
|
|
| 25 |
def build_qa_chain(pdf_text):
|
| 26 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 27 |
chunks = splitter.split_text(pdf_text)
|
|
|
|
| 48 |
global qa_chain
|
| 49 |
text = extract_text(file)
|
| 50 |
qa_chain = build_qa_chain(text)
|
| 51 |
+
return
|
|
|
|
| 52 |
def answer_question(question):
|
| 53 |
if qa_chain is None:
|
| 54 |
return "❌ Please upload a PDF first."
|
|
|
|
| 68 |
answer_output = gr.Textbox(label="Answer")
|
| 69 |
ask_btn = gr.Button("Ask")
|
| 70 |
ask_btn.click(answer_question, question_input, answer_output)
|
| 71 |
+
demo.launch()
|
|
|