Muhammad Anas Akhtar
Update app.py
d74cbad verified
raw
history blame
1.46 kB
import torch
import gradio as gr
from transformers import pipeline
# Use the Hugging Face model pipeline
question_answer = pipeline("question-answering", model="deepset/roberta-base-squad2")
def read_file_content(file_obj):
"""
Reads the content of a file object, cleans it, and returns it.
"""
try:
with open(file_obj.name, 'r', encoding='utf-8') as file:
context = file.read().strip()
return context
except Exception as e:
return f"An error occurred: {e}"
def get_answer(file, question):
context = read_file_content(file)
if isinstance(context, str) and context.startswith("An error occurred"):
return context # Return file reading error
if not question.strip():
return "Please provide a valid question."
try:
print(f"Context:\n{context}") # Debug
print(f"Question: {question}") # Debug
answer = question_answer(question=question, context=context)
return answer["answer"]
except Exception as e:
return f"An error occurred during question answering: {e}"
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="@GenAILearniverse Project 5: Document Q & A",
description="This application answers questions based on the uploaded context file."
)
demo.launch()