Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,61 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
import
|
| 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
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
context
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
answer
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
gr.
|
| 55 |
-
|
| 56 |
-
],
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 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()
|
|
|