Muhammad Anas Akhtar
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# 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 |
+
try:
|
| 23 |
+
with open(file_obj.name, 'r', encoding='utf-8') as file:
|
| 24 |
+
context = file.read()
|
| 25 |
+
return context
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"An error occurred: {e}"
|
| 28 |
+
|
| 29 |
+
#
|
| 30 |
+
|
| 31 |
+
def get_answer(file, question):
|
| 32 |
+
context = read_file_content(file)
|
| 33 |
+
answer = question_answer(question=question, context=context)
|
| 34 |
+
return answer["answer"]
|
| 35 |
+
|
| 36 |
+
demo = gr.Interface(fn=get_answer,
|
| 37 |
+
inputs=[gr.File(label="Upload your file"), gr.Textbox(label="Input your question",lines=1)],
|
| 38 |
+
outputs=[gr.Textbox(label="Answer text",lines=1)],
|
| 39 |
+
title="@GenAILearniverse Project 5: Document Q & A",
|
| 40 |
+
description="THIS APPLICATION WILL BE USED TO ANSER QUESTIONS BASED ON CONTEXT PROVIDED.")
|
| 41 |
+
|
| 42 |
+
demo.launch()
|