Spaces:
Sleeping
Sleeping
Added app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
pipe = pipeline(
|
7 |
+
"question-answering",
|
8 |
+
model="deepset/roberta-base-squad2")
|
9 |
+
|
10 |
+
# Function to read the content of a file object
|
11 |
+
def read_file_content(file_obj):
|
12 |
+
"""
|
13 |
+
Reads the content of a file object and returns it.
|
14 |
+
Parameters:
|
15 |
+
file_obj (file object): The file object to read from.
|
16 |
+
Returns:
|
17 |
+
str: The content of the file.
|
18 |
+
"""
|
19 |
+
try:
|
20 |
+
with open(file_obj.name, 'r', encoding='utf-8') as file:
|
21 |
+
context = file.read()
|
22 |
+
return context
|
23 |
+
except Exception as e:
|
24 |
+
return f"An error occurred: {e}"
|
25 |
+
|
26 |
+
# Function to get the answer to a question from a file
|
27 |
+
def get_answer(file, question):
|
28 |
+
"""
|
29 |
+
Answers a question based on the content of a file.
|
30 |
+
Parameters:
|
31 |
+
file (file object): The file object containing the context.
|
32 |
+
question (str): The question to answer.
|
33 |
+
Returns:
|
34 |
+
str: The answer to the question.
|
35 |
+
"""
|
36 |
+
if not question or not file:
|
37 |
+
return "Please provide both a question and a file."
|
38 |
+
context = read_file_content(file)
|
39 |
+
answer = pipe(question=question, context=context)
|
40 |
+
return answer["answer"]
|
41 |
+
|
42 |
+
# Create the Gradio interface
|
43 |
+
demo = gr.Interface(fn=get_answer,
|
44 |
+
inputs=[gr.File(label="File Upload"), gr.Textbox(label="Prompt Input", lines=1)],
|
45 |
+
outputs=[gr.Textbox(label="Response", lines=1)],
|
46 |
+
title="@caesar-2series: Rag Application",
|
47 |
+
description="Retrieval Augmented Generation Questions-Answering Application")
|
48 |
+
|
49 |
+
# Launch the Gradio interface
|
50 |
+
demo.launch()
|