Rosan144 commited on
Commit
f23cc4f
·
verified ·
1 Parent(s): 135eead

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import os
4
+ from PyPDF2 import PdfReader
5
+ from docx import Document
6
+ import io
7
+
8
+
9
+ # Use a pipeline as a high-level helper
10
+ from transformers import pipeline
11
+
12
+ question_answer = pipeline("question-answering", model="deepset/roberta-base-squad2")
13
+
14
+
15
+
16
+ def extract_file_content(file_obj):
17
+ filename = file_obj.name
18
+ file_ext = filename.split('.')[-1].lower()
19
+
20
+ try:
21
+ if file_ext == "pdf":
22
+ reader = PdfReader(file_obj)
23
+ text = ""
24
+ for page in reader.pages:
25
+ text += page.extract_text() + "\n"
26
+ return text.strip()
27
+
28
+ elif file_ext == "txt":
29
+ return file_obj.read().decode('utf-8')
30
+
31
+ elif file_ext == "docx":
32
+ doc = Document(io.BytesIO(file_obj.read()))
33
+ text = ""
34
+ for para in doc.paragraphs:
35
+ text += para.text + "\n"
36
+ return text.strip()
37
+
38
+ else:
39
+ return "Unsupported file format. Please upload PDF, TXT, or DOCX."
40
+
41
+ except Exception as e:
42
+ return f"Error reading file: {str(e)}"
43
+
44
+ def get_ans(file,question):
45
+ context = extract_file_content(file)
46
+ answer = question_answer(question=question ,context=context)
47
+ return answer["answer"]
48
+
49
+
50
+ demo = gr.Interface(
51
+ fn=get_ans,
52
+ inputs=[gr.File(label="Upload Your File"), gr.Textbox(label="Enter question",lines=4)],
53
+ outputs=[gr.Textbox(label="Answer", lines=5)],
54
+ title="@RosangenAi Project 3: Document question answer")
55
+
56
+ demo.launch()