Spaces:
Running
Running
PanigrahiNirma
commited on
Commit
•
abc928c
1
Parent(s):
0616740
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
+
from transformers.pipelines import pipeline
|
4 |
+
import PyPDF2
|
5 |
+
|
6 |
+
# Load T5 model and tokenizer
|
7 |
+
model_name = "t5-large"
|
8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
qa_pipeline = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
11 |
+
|
12 |
+
def read_pdf(file):
|
13 |
+
reader = PyPDF2.PdfFileReader(file)
|
14 |
+
text = ""
|
15 |
+
for page_num in range(reader.numPages):
|
16 |
+
page = reader.getPage(page_num)
|
17 |
+
text += page.extract_text()
|
18 |
+
return text
|
19 |
+
|
20 |
+
def answer_question(pdf, question):
|
21 |
+
context = read_pdf(pdf)
|
22 |
+
input_text = f"question: {question} context: {context}"
|
23 |
+
response = qa_pipeline(input_text, max_length=512, do_sample=False)
|
24 |
+
return response[0]['generated_text']
|
25 |
+
|
26 |
+
# Define Gradio interface
|
27 |
+
iface = gr.Interface(
|
28 |
+
fn=answer_question,
|
29 |
+
inputs=[gr.inputs.File(type="file", label="Upload PDF"), gr.inputs.Textbox(lines=2, placeholder="Ask a question")],
|
30 |
+
outputs=gr.outputs.Textbox(label="Answer"),
|
31 |
+
title="PDF Q&A with T5"
|
32 |
+
)
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
iface.launch()
|