Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# model_path="../models/models--deepset--roberta-base-squad2/snapshots/adc3b06f79f797d1c575d5479d6f5efe54a9e3b4"
|
8 |
+
|
9 |
+
question_answer = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
10 |
+
|
11 |
+
# question_answer = pipeline("question-answering", model=model_path)
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
def read_file_content(file_path):
|
17 |
+
"""
|
18 |
+
Reads the content of a file given its file path and returns it as a string.
|
19 |
+
"""
|
20 |
+
try:
|
21 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
22 |
+
return file.read()
|
23 |
+
except Exception as e:
|
24 |
+
return f"Error reading file: {e}"
|
25 |
+
|
26 |
+
def get_answer(file, question):
|
27 |
+
context = read_file_content(file) # 'file' is a path string, not a file object
|
28 |
+
if context.startswith("Error"):
|
29 |
+
return context # Return error message if file reading fails
|
30 |
+
answer = question_answer(question=question, context=context)
|
31 |
+
return answer["answer"]
|
32 |
+
|
33 |
+
demo = gr.Interface(fn=get_answer,
|
34 |
+
inputs=[gr.File(label="input your context"),gr.Textbox(label="input your question",lines=1)],
|
35 |
+
outputs=[gr.Textbox(label="Summarized text",lines=1)],
|
36 |
+
title="@Naseem GenAI Project 2: Question Answering based on context provided",
|
37 |
+
description="THIS APPLICATION WILL PROVIDE ANSWER BASED ON CONTEXT PROVIDED")
|
38 |
+
demo.launch()
|
39 |
+
|