arssite commited on
Commit
b5be996
·
verified ·
1 Parent(s): 12b2ca3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -62
app.py CHANGED
@@ -1,62 +1,61 @@
1
- import torch
2
- import gradio as gr
3
- from transformers import pipeline
4
- import fitz # PyMuPDF for PDF handling
5
- from docx import Document # For handling .docx files
6
-
7
- model_path = ("../Models/models--deepset--roberta-base-squad2/snapshots"
8
- "/cbf50ba81465d4d8676b8bab348e31835147541b")
9
-
10
- question_answer = pipeline("question-answering",
11
- model="deepset/roberta-base-squad2")
12
-
13
- def read_file_content(file_obj):
14
- try:
15
- # Determine the file extension
16
- file_extension = file_obj.name.split('.')[-1].lower()
17
-
18
- if file_extension == 'txt':
19
- # Reading text files
20
- with open(file_obj.name, 'r', encoding='utf-8') as file:
21
- context = file.read()
22
-
23
- elif file_extension == 'pdf':
24
- # Reading PDF files using PyMuPDF (fitz)
25
- with fitz.open(file_obj.name) as pdf:
26
- context = ""
27
- for page_num in range(pdf.page_count):
28
- page = pdf.load_page(page_num)
29
- context += page.get_text()
30
-
31
- elif file_extension == 'docx':
32
- # Reading Word documents using python-docx
33
- doc = Document(file_obj.name)
34
- context = "\n".join([para.text for para in doc.paragraphs])
35
-
36
- else:
37
- return "Unsupported file format. Please upload a .txt, .pdf, or .docx file."
38
-
39
- return context
40
-
41
- except Exception as e:
42
- return f"An error occurred: {e}"
43
-
44
- def get_answer(file, question):
45
- context = read_file_content(file)
46
- if "An error occurred" in context or "Unsupported" in context:
47
- return context # Return error message directly if present
48
- answer = question_answer(question=question, context=context)
49
- return answer["answer"]
50
-
51
- demo = gr.Interface(
52
- fn=get_answer,
53
- inputs=[
54
- gr.File(label="Upload your file"),
55
- gr.Textbox(label="Input your question", lines=1)
56
- ],
57
- outputs=[gr.Textbox(label="Answer text", lines=1)],
58
- title="Document Q & A",
59
- description="THIS APPLICATION WILL BE USED TO ANSWER QUESTIONS BASED ON CONTEXT PROVIDED."
60
- )
61
-
62
- demo.launch()
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ from PyPDF2 import PdfReader # Alternative for PDF handling
5
+ from docx import Document # For handling .docx files
6
+
7
+ model_path = ("../Models/models--deepset--roberta-base-squad2/snapshots"
8
+ "/cbf50ba81465d4d8676b8bab348e31835147541b")
9
+
10
+ question_answer = pipeline("question-answering",
11
+ model="deepset/roberta-base-squad2")
12
+
13
+ def read_file_content(file_obj):
14
+ try:
15
+ # Determine the file extension
16
+ file_extension = file_obj.name.split('.')[-1].lower()
17
+
18
+ if file_extension == 'txt':
19
+ # Reading text files
20
+ with open(file_obj.name, 'r', encoding='utf-8') as file:
21
+ context = file.read()
22
+
23
+ elif file_extension == 'pdf':
24
+ # Reading PDF files using PyPDF2
25
+ reader = PdfReader(file_obj.name)
26
+ context = ""
27
+ for page in reader.pages:
28
+ context += page.extract_text()
29
+
30
+ elif file_extension == 'docx':
31
+ # Reading Word documents using python-docx
32
+ doc = Document(file_obj.name)
33
+ context = "\n".join([para.text for para in doc.paragraphs])
34
+
35
+ else:
36
+ return "Unsupported file format. Please upload a .txt, .pdf, or .docx file."
37
+
38
+ return context
39
+
40
+ except Exception as e:
41
+ return f"An error occurred: {e}"
42
+
43
+ def get_answer(file, question):
44
+ context = read_file_content(file)
45
+ if "An error occurred" in context or "Unsupported" in context:
46
+ return context # Return error message directly if present
47
+ answer = question_answer(question=question, context=context)
48
+ return answer["answer"]
49
+
50
+ demo = gr.Interface(
51
+ fn=get_answer,
52
+ inputs=[
53
+ gr.File(label="Upload your file"),
54
+ gr.Textbox(label="Input your question", lines=1)
55
+ ],
56
+ outputs=[gr.Textbox(label="Answer text", lines=1)],
57
+ title="Explore Documents",
58
+ description="THIS APPLICATION WILL BE USED TO ANSWER QUESTIONS BASED ON CONTEXT PROVIDED."
59
+ )
60
+
61
+ demo.launch()