Spaces:
Sleeping
Sleeping
Update app.py
Browse filesi have changed the title from Dynamic PDF Question Answering to Ask My PDF
app.py
CHANGED
|
@@ -1,75 +1,75 @@
|
|
| 1 |
-
from langchain_community.document_loaders import PDFPlumberLoader
|
| 2 |
-
from langchain_ollama import OllamaLLM
|
| 3 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 4 |
-
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
|
| 5 |
-
import gradio as gr
|
| 6 |
-
|
| 7 |
-
def process_pdf(file):
|
| 8 |
-
try:
|
| 9 |
-
loader = PDFPlumberLoader(file.name)
|
| 10 |
-
result = loader.load()
|
| 11 |
-
splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=20)
|
| 12 |
-
return splitter.split_documents(result)
|
| 13 |
-
except Exception as e:
|
| 14 |
-
return f"Error processing PDF: {str(e)}"
|
| 15 |
-
|
| 16 |
-
def initialize_llm():
|
| 17 |
-
return OllamaLLM(model="qwen2", temperature=0.7, base_url="https://alpaca-upright-vulture.ngrok-free.app")
|
| 18 |
-
|
| 19 |
-
def create_prompt():
|
| 20 |
-
examples = [
|
| 21 |
-
{"input": "What is the main topic discussed in the document?",
|
| 22 |
-
"output": "The document discusses the concept and details of Neural Networks."},
|
| 23 |
-
{"input": "Explain the term 'activation function' as used in this document.",
|
| 24 |
-
"output": "An activation function in the context of this document refers to a mathematical function applied to neurons' output to introduce non-linearity in the model."}
|
| 25 |
-
]
|
| 26 |
-
|
| 27 |
-
example_template = PromptTemplate(
|
| 28 |
-
input_variables=["input", "output"],
|
| 29 |
-
template="Human: {input}\nAssistant: {output}"
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
return FewShotPromptTemplate(
|
| 33 |
-
examples=examples,
|
| 34 |
-
example_prompt=example_template,
|
| 35 |
-
prefix="You are an AI assistant that provides specific and accurate answers based on the provided document.",
|
| 36 |
-
suffix="Human: {input}\nAssistant:",
|
| 37 |
-
input_variables=["input"]
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
def generate_answer(chain, user_input):
|
| 41 |
-
try:
|
| 42 |
-
response = chain.invoke({"input": user_input})
|
| 43 |
-
return response
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return f"Error generating answer: {str(e)}"
|
| 46 |
-
|
| 47 |
-
def handle_file(file, user_input):
|
| 48 |
-
if not file:
|
| 49 |
-
return "Please upload a PDF document."
|
| 50 |
-
|
| 51 |
-
data = process_pdf(file)
|
| 52 |
-
if isinstance(data, str):
|
| 53 |
-
return data
|
| 54 |
-
|
| 55 |
-
llm = initialize_llm()
|
| 56 |
-
prompt = create_prompt()
|
| 57 |
-
chain = prompt | llm
|
| 58 |
-
|
| 59 |
-
if not user_input.strip():
|
| 60 |
-
return "Please enter a question."
|
| 61 |
-
|
| 62 |
-
return generate_answer(chain, user_input)
|
| 63 |
-
|
| 64 |
-
interface = gr.Interface(
|
| 65 |
-
fn=handle_file,
|
| 66 |
-
inputs=[
|
| 67 |
-
gr.File(label="Upload PDF"),
|
| 68 |
-
gr.Textbox(lines=2, placeholder="Enter your question here...")
|
| 69 |
-
],
|
| 70 |
-
outputs=gr.Textbox(label="Answer"),
|
| 71 |
-
title="
|
| 72 |
-
description="Upload a PDF document and ask questions about its content."
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
interface.launch()
|
|
|
|
| 1 |
+
from langchain_community.document_loaders import PDFPlumberLoader
|
| 2 |
+
from langchain_ollama import OllamaLLM
|
| 3 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
def process_pdf(file):
|
| 8 |
+
try:
|
| 9 |
+
loader = PDFPlumberLoader(file.name)
|
| 10 |
+
result = loader.load()
|
| 11 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=20)
|
| 12 |
+
return splitter.split_documents(result)
|
| 13 |
+
except Exception as e:
|
| 14 |
+
return f"Error processing PDF: {str(e)}"
|
| 15 |
+
|
| 16 |
+
def initialize_llm():
|
| 17 |
+
return OllamaLLM(model="qwen2", temperature=0.7, base_url="https://alpaca-upright-vulture.ngrok-free.app")
|
| 18 |
+
|
| 19 |
+
def create_prompt():
|
| 20 |
+
examples = [
|
| 21 |
+
{"input": "What is the main topic discussed in the document?",
|
| 22 |
+
"output": "The document discusses the concept and details of Neural Networks."},
|
| 23 |
+
{"input": "Explain the term 'activation function' as used in this document.",
|
| 24 |
+
"output": "An activation function in the context of this document refers to a mathematical function applied to neurons' output to introduce non-linearity in the model."}
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
example_template = PromptTemplate(
|
| 28 |
+
input_variables=["input", "output"],
|
| 29 |
+
template="Human: {input}\nAssistant: {output}"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
return FewShotPromptTemplate(
|
| 33 |
+
examples=examples,
|
| 34 |
+
example_prompt=example_template,
|
| 35 |
+
prefix="You are an AI assistant that provides specific and accurate answers based on the provided document.",
|
| 36 |
+
suffix="Human: {input}\nAssistant:",
|
| 37 |
+
input_variables=["input"]
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def generate_answer(chain, user_input):
|
| 41 |
+
try:
|
| 42 |
+
response = chain.invoke({"input": user_input})
|
| 43 |
+
return response
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error generating answer: {str(e)}"
|
| 46 |
+
|
| 47 |
+
def handle_file(file, user_input):
|
| 48 |
+
if not file:
|
| 49 |
+
return "Please upload a PDF document."
|
| 50 |
+
|
| 51 |
+
data = process_pdf(file)
|
| 52 |
+
if isinstance(data, str):
|
| 53 |
+
return data
|
| 54 |
+
|
| 55 |
+
llm = initialize_llm()
|
| 56 |
+
prompt = create_prompt()
|
| 57 |
+
chain = prompt | llm
|
| 58 |
+
|
| 59 |
+
if not user_input.strip():
|
| 60 |
+
return "Please enter a question."
|
| 61 |
+
|
| 62 |
+
return generate_answer(chain, user_input)
|
| 63 |
+
|
| 64 |
+
interface = gr.Interface(
|
| 65 |
+
fn=handle_file,
|
| 66 |
+
inputs=[
|
| 67 |
+
gr.File(label="Upload PDF"),
|
| 68 |
+
gr.Textbox(lines=2, placeholder="Enter your question here...")
|
| 69 |
+
],
|
| 70 |
+
outputs=gr.Textbox(label="Answer"),
|
| 71 |
+
title="Ask My PDF",
|
| 72 |
+
description="Upload a PDF document and ask questions about its content."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
interface.launch()
|