Doc_QA / app.py
Sahibhim's picture
Create app.py
b44c871 verified
import torch
import gradio as gr
# Use a pipeline as a high-level helper
from transformers import pipeline
model_path = ("../Models/models--deepset--roberta-base-squad2/snapshots"
"/adc3b06f79f797d1c575d5479d6f5efe54a9e3b4")
question_answer = pipeline("question-answering",
model="deepset/roberta-base-squad2")
# question_answer = pipeline("question-answering",model= model_path)
# context=("Abraham Lincoln[b] (February 12, 1809 – April 15, 1865) was the 16th president of the United States, "
# "serving from 1861 until his assassination in 1865. He led the United States through the American Civil War, "
# "defeating the Confederate States and playing a major role in the abolition of slavery."
# "During the early months of 1861, the situation around Fort Sumter increasingly began to resemble a siege. "
# "In March, Brigadier General P. G. T. Beauregard, the first general officer of the newly formed Confederate States Army, was placed in command of Confederate forces in Charleston. Beauregard energetically directed the strengthening of batteries around Charleston harbor aimed at Fort Sumter. "
# "Conditions in the fort deteriorated due to shortages of men, food, and supplies as the Union soldiers rushed to complete the installation of additional guns.")
# question="what is the Date of Birth of Abraham Lincoln?"
#
# answer = question_answer(question=question, context=context)
# print(answer)
def read_file_content(file_obj):
"""
Reads the content of a file object and returns it.
Parameters:
file_obj (file object): The file object to read from.
Returns:
str: The content of the file.
"""
try:
with open(file_obj.name, 'r', encoding='utf-8') as file:
context = file.read()
return context
except Exception as e:
return f"An error occurred: {e}"
def get_answer(file, question):
context = read_file_content(file)
answer = question_answer(question=question, context=context)
return answer["answer"]
demo = gr.Interface(fn=get_answer,
inputs=[gr.File(label="Upload your file"), gr.Textbox(label="Input your question",lines=1)],
outputs=[gr.Textbox(label="Answer text",lines=1)],
title="@Sahibhim GenAI Project 5: Document Q & A",
description="THIS APPLICATION WILL BE USED TO ANSwER QUESTIONS BASED ON CONTEXT PROVIDED.")
demo.launch()