Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=model_path)
|
| 10 |
+
|
| 11 |
+
question_answer = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
| 12 |
+
|
| 13 |
+
def read_file_content(file_obj):
|
| 14 |
+
"""
|
| 15 |
+
Reads the content of a file object and returns it.
|
| 16 |
+
Parameters:
|
| 17 |
+
file_obj (file object): The file object to read from.
|
| 18 |
+
Returns:
|
| 19 |
+
str: The content of the file.
|
| 20 |
+
"""
|
| 21 |
+
try:
|
| 22 |
+
with open(file_obj.name, 'r', encoding='utf-8') as file:
|
| 23 |
+
context = file.read()
|
| 24 |
+
return context
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"An error occurred: {e}"
|
| 27 |
+
|
| 28 |
+
# context = ("Mark Elliot Zuckerberg (/ˈzʌkərbɜːrɡ/; born May 14, 1984) is an American businessman who co-founded the social media service Facebook"
|
| 29 |
+
# " and its parent company Meta Platforms, of which he is the chairman, chief executive officer, and controlling shareholder. "
|
| 30 |
+
# "Zuckerberg has been the subject of multiple lawsuits regarding the creation and ownership of the website as well as issues such "
|
| 31 |
+
# "as user privacy. Zuckerberg briefly attended Harvard College, where he launched Facebook in February 2004 with his roommates "
|
| 32 |
+
# "Eduardo Saverin, Andrew McCollum, Dustin Moskovitz and Chris Hughes. Zuckerberg took the company public in May 2012 with majority "
|
| 33 |
+
# "shares. He became the world's youngest self-made billionaire[a] in 2008, at age 23, and has consistently ranked among the world's "
|
| 34 |
+
# "wealthiest individuals. According to Forbes, as of March 2025, Zuckerberg's estimated net worth stood at US$214.1 billion, "
|
| 35 |
+
# "making him the second richest individual in the world,[2] behind Elon Musk and before Jeff Bezos.")
|
| 36 |
+
#
|
| 37 |
+
# question = "What is DOB of Mark?"
|
| 38 |
+
|
| 39 |
+
# answer = question_answer(question=question, context=context)
|
| 40 |
+
#
|
| 41 |
+
# print(answer)
|
| 42 |
+
|
| 43 |
+
def get_answer(file, question):
|
| 44 |
+
context = read_file_content(file)
|
| 45 |
+
answer = question_answer(question=question, context=context)
|
| 46 |
+
return answer["answer"]
|
| 47 |
+
|
| 48 |
+
gr.close_all()
|
| 49 |
+
|
| 50 |
+
demo = gr.Interface(fn=get_answer,
|
| 51 |
+
inputs=[gr.File(label="Upload your file"), gr.Textbox(label="Input your question",lines=1)],
|
| 52 |
+
outputs=[gr.Textbox(label="Answered text",lines=4)],
|
| 53 |
+
title="@GenAILearniverse Project 5: Document Q n A",
|
| 54 |
+
description="THIS APPLICATION WILL BE USED TO ANSWER QUESTION BASED ON CONTEXT PROVIDED.")
|
| 55 |
+
|
| 56 |
+
demo.launch()
|