Yatheshr commited on
Commit
e21fbd8
Β·
verified Β·
1 Parent(s): 7b697b7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import fitz # PyMuPDF
3
+ from transformers import pipeline
4
+
5
+ # Load BERT QA pipeline
6
+ qa_pipeline = pipeline("question-answering", model="bert-large-uncased-whole-word-masking-finetuned-squad")
7
+
8
+ # Extract text from uploaded PDF
9
+ def extract_text(pdf_file):
10
+ text = ""
11
+ with fitz.open(stream=pdf_file.read(), filetype="pdf") as doc:
12
+ for page in doc:
13
+ text += page.get_text()
14
+ return text
15
+
16
+ # Main function for Gradio
17
+ def qa_from_pdf_upload(pdf_file, question):
18
+ if not pdf_file:
19
+ return "❌ Please upload a PDF.", "", "", ""
20
+
21
+ context = extract_text(pdf_file)
22
+ if not context.strip():
23
+ return "❌ Could not extract text from the PDF.", "", "", ""
24
+
25
+ result = qa_pipeline(question=question, context=context)
26
+ return result["answer"], round(result["score"] * 100, 2), result["start"], result["end"]
27
+
28
+ # Gradio UI
29
+ gr.Interface(
30
+ fn=qa_from_pdf_upload,
31
+ inputs=[
32
+ gr.File(label="πŸ“₯ Upload Fund PDF"),
33
+ gr.Textbox(label="❓ Your Question", placeholder="e.g., Who is the fund manager?")
34
+ ],
35
+ outputs=[
36
+ gr.Textbox(label="βœ… Answer"),
37
+ gr.Textbox(label="πŸ“Š Confidence Score (%)"),
38
+ gr.Textbox(label="Start Index"),
39
+ gr.Textbox(label="End Index")
40
+ ],
41
+ title="πŸ“˜ Morningstar Fund QA from PDF",
42
+ description="Upload a fund report in PDF format and ask questions using BERT."
43
+ ).launch()