Muhammad Anas Akhtar
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,67 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
# Use a pipeline as a high-level helper
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
-
|
| 8 |
question_answer = pipeline("question-answering",
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# question_answer = pipeline("question-answering",
|
| 12 |
-
# model=model_path)
|
| 13 |
|
| 14 |
def read_file_content(file_obj):
|
| 15 |
"""
|
| 16 |
Reads the content of a file object and returns it.
|
|
|
|
|
|
|
| 17 |
Parameters:
|
| 18 |
file_obj (file object): The file object to read from.
|
| 19 |
Returns:
|
| 20 |
str: The content of the file.
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def get_answer(file, question):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
context = read_file_content(file)
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Use a pipeline as a high-level helper
|
| 6 |
question_answer = pipeline("question-answering",
|
| 7 |
+
model="deepset/roberta-base-squad2")
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def read_file_content(file_obj):
|
| 10 |
"""
|
| 11 |
Reads the content of a file object and returns it.
|
| 12 |
+
Attempts multiple encodings if the default fails.
|
| 13 |
+
|
| 14 |
Parameters:
|
| 15 |
file_obj (file object): The file object to read from.
|
| 16 |
Returns:
|
| 17 |
str: The content of the file.
|
| 18 |
"""
|
| 19 |
+
encodings = ['utf-8', 'latin-1', 'cp1252', 'ascii']
|
| 20 |
+
|
| 21 |
+
for encoding in encodings:
|
| 22 |
+
try:
|
| 23 |
+
with open(file_obj.name, 'r', encoding=encoding) as file:
|
| 24 |
+
context = file.read()
|
| 25 |
+
return context
|
| 26 |
+
except UnicodeDecodeError:
|
| 27 |
+
continue
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"An error occurred: {e}"
|
| 30 |
+
|
| 31 |
+
return "Error: Unable to read the file with any supported encoding"
|
| 32 |
|
| 33 |
def get_answer(file, question):
|
| 34 |
+
if file is None:
|
| 35 |
+
return "Please upload a file"
|
| 36 |
+
if not question:
|
| 37 |
+
return "Please enter a question"
|
| 38 |
+
|
| 39 |
context = read_file_content(file)
|
| 40 |
+
if context.startswith("An error occurred") or context.startswith("Error:"):
|
| 41 |
+
return context
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
answer = question_answer(question=question, context=context)
|
| 45 |
+
return answer["answer"]
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Error processing question: {str(e)}"
|
| 48 |
|
| 49 |
+
# Create the Gradio interface
|
| 50 |
+
demo = gr.Interface(
|
| 51 |
+
fn=get_answer,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.File(label="Upload your file (.txt)", type="file"),
|
| 54 |
+
gr.Textbox(label="Input your question", lines=1, placeholder="Enter your question here...")
|
| 55 |
+
],
|
| 56 |
+
outputs=[
|
| 57 |
+
gr.Textbox(label="Answer", lines=2)
|
| 58 |
+
],
|
| 59 |
+
title="Document Question & Answer Chatbot",
|
| 60 |
+
description="Upload a text document and ask questions about its content. The AI will find relevant answers from the text.",
|
| 61 |
+
examples=[
|
| 62 |
+
["sample.txt", "What is the main topic?"]
|
| 63 |
+
]
|
| 64 |
+
)
|
| 65 |
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|