Muhammad Anas Akhtar
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,40 @@
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
-
|
|
|
4 |
from transformers import pipeline
|
5 |
|
6 |
-
model_path = ("../Models/models--deepset--roberta-base-squad2/snapshots"
|
7 |
-
"/cbf50ba81465d4d8676b8bab348e31835147541b")
|
8 |
|
9 |
-
question_answer = pipeline("question-answering",
|
|
|
|
|
10 |
|
11 |
-
def
|
12 |
"""
|
13 |
-
Reads the content of a
|
14 |
Parameters:
|
15 |
file_obj (file object): The file object to read from.
|
16 |
Returns:
|
17 |
-
str: The
|
18 |
"""
|
19 |
try:
|
20 |
-
with
|
21 |
-
|
22 |
-
|
23 |
-
text += page.extract_text()
|
24 |
-
return text
|
25 |
except Exception as e:
|
26 |
return f"An error occurred: {e}"
|
27 |
|
|
|
|
|
28 |
def get_answer(file, question):
|
29 |
-
|
30 |
-
context = read_pdf_content(file)
|
31 |
-
if context.startswith("An error occurred"):
|
32 |
-
return context
|
33 |
-
|
34 |
-
# Get the answer from the model
|
35 |
answer = question_answer(question=question, context=context)
|
36 |
return answer["answer"]
|
37 |
|
38 |
demo = gr.Interface(fn=get_answer,
|
39 |
-
inputs=[gr.File(label="Upload your
|
40 |
-
outputs=[gr.Textbox(label="Answer text",
|
41 |
title="@GenAILearniverse Project 5: Document Q & A",
|
42 |
-
description="
|
43 |
|
44 |
-
demo.launch()
|
|
|
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 |
+
model="deepset/roberta-base-squad2")
|
10 |
+
|
11 |
|
12 |
+
def read_file_content(file_obj):
|
13 |
"""
|
14 |
+
Reads the content of a file object and returns it.
|
15 |
Parameters:
|
16 |
file_obj (file object): The file object to read from.
|
17 |
Returns:
|
18 |
+
str: The content of the file.
|
19 |
"""
|
20 |
try:
|
21 |
+
with open(file_obj.name, 'r', encoding='utf-8') as file:
|
22 |
+
context = file.read()
|
23 |
+
return context
|
|
|
|
|
24 |
except Exception as e:
|
25 |
return f"An error occurred: {e}"
|
26 |
|
27 |
+
|
28 |
+
|
29 |
def get_answer(file, question):
|
30 |
+
context = read_file_content(file)
|
|
|
|
|
|
|
|
|
|
|
31 |
answer = question_answer(question=question, context=context)
|
32 |
return answer["answer"]
|
33 |
|
34 |
demo = gr.Interface(fn=get_answer,
|
35 |
+
inputs=[gr.File(label="Upload your file"), gr.Textbox(label="Input your question",lines=1)],
|
36 |
+
outputs=[gr.Textbox(label="Answer text",lines=1)],
|
37 |
title="@GenAILearniverse Project 5: Document Q & A",
|
38 |
+
description="THIS APPLICATION WILL BE USED TO ANSER QUESTIONS BASED ON CONTEXT PROVIDED.")
|
39 |
|
40 |
+
demo.launch()
|