Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -24,20 +24,26 @@ model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
|
|
| 24 |
qa_model = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 25 |
|
| 26 |
# β
Upload and process PDF
|
| 27 |
-
def upload_pdf(
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
token = str(uuid.uuid4())
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"β Error: {str(e)}"
|
| 40 |
|
|
|
|
| 41 |
# β
Generate Q&A using filename
|
| 42 |
def generate_qa(filename):
|
| 43 |
try:
|
|
@@ -123,13 +129,12 @@ with gr.Blocks(theme="default") as demo:
|
|
| 123 |
"""
|
| 124 |
)
|
| 125 |
|
| 126 |
-
with gr.Tab("
|
| 127 |
-
gr.
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
| 132 |
-
file.change(fn=upload_pdf, inputs=file, outputs=upload_out)
|
| 133 |
|
| 134 |
with gr.Tab("π§ 2. Generate Questions & Answers"):
|
| 135 |
gr.Markdown("### π€ Generate Q&A from your PDF")
|
|
|
|
| 24 |
qa_model = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 25 |
|
| 26 |
# β
Upload and process PDF
|
| 27 |
+
def upload_pdf(files): # <== make sure it's 'files', not 'file'
|
| 28 |
try:
|
| 29 |
+
messages = []
|
|
|
|
| 30 |
|
| 31 |
+
for file in files:
|
| 32 |
+
filename = file.name
|
| 33 |
+
token = str(uuid.uuid4())
|
| 34 |
|
| 35 |
+
pdf_reader = PyPDF2.PdfReader(file)
|
| 36 |
+
text = "".join([page.extract_text() or "" for page in pdf_reader.pages])
|
| 37 |
+
chunks = [text[i:i+500] for i in range(0, len(text), 500)]
|
| 38 |
+
|
| 39 |
+
create_db(token, chunks, filename, text)
|
| 40 |
+
messages.append(f"β
Uploaded: {filename} (Token: {token})")
|
| 41 |
+
|
| 42 |
+
return "\n".join(messages)
|
| 43 |
except Exception as e:
|
| 44 |
return f"β Error: {str(e)}"
|
| 45 |
|
| 46 |
+
|
| 47 |
# β
Generate Q&A using filename
|
| 48 |
def generate_qa(filename):
|
| 49 |
try:
|
|
|
|
| 129 |
"""
|
| 130 |
)
|
| 131 |
|
| 132 |
+
with gr.Tab("π 1. Upload PDFs"):
|
| 133 |
+
file_input = gr.File(label="π Upload one or more PDFs", file_types=[".pdf"], file_count="multiple")
|
| 134 |
+
upload_output = gr.Markdown()
|
| 135 |
+
upload_btn = gr.Button("π Upload")
|
| 136 |
|
| 137 |
+
upload_btn.click(fn=upload_pdf, inputs=file_input, outputs=upload_output)
|
|
|
|
| 138 |
|
| 139 |
with gr.Tab("π§ 2. Generate Questions & Answers"):
|
| 140 |
gr.Markdown("### π€ Generate Q&A from your PDF")
|