Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
pipe = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
8 |
+
|
9 |
+
|
10 |
+
def read_file_content(file_obj):
|
11 |
+
try:
|
12 |
+
with open(file_obj.name, "r", encoding="utf-8") as f:
|
13 |
+
content = f.read()
|
14 |
+
return content
|
15 |
+
except Exception as e:
|
16 |
+
return f"An error occurred: {e}"
|
17 |
+
|
18 |
+
|
19 |
+
def get_answer(file, question):
|
20 |
+
context= read_file_content(file)
|
21 |
+
answer = pipe(question=question, context = context)
|
22 |
+
return (answer['answer'])
|
23 |
+
|
24 |
+
demo = gr.Interface(fn=get_answer,
|
25 |
+
inputs= [gr.File(label="Upload the file for context"), gr.Textbox(label='Ask the question')],
|
26 |
+
outputs=[gr.Textbox(label='Here is the answer to your question')],
|
27 |
+
title='Get the Answers to your question',
|
28 |
+
description='This application gives the answers to your questions'
|
29 |
+
|
30 |
+
|
31 |
+
)
|
32 |
+
demo.launch()
|