qa_economia / app.py
igoracmorais's picture
Update app.py
95f446b verified
import gradio as gr
import json
# Lista para armazenar os dados
data = []
# Função para adicionar contextos, perguntas e respostas
def add_entry(context, question, answer, answer_start, entry_id):
global data
entry = {
"context": context,
"qas":[
{
"id": entry_id,
"question": question,
"answers": [
{
"text": answer,
"answer_start": answer_start
}
]
}
]
}
data.append(entry)
return "Entry added!"
# Função para salvar os dados em um arquivo JSON e retornar o caminho do arquivo
def save_to_json(file_name):
global data
file_path = "squad_dataset.json"
with open(file_path, "w", encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
return file_path
# Interface Gradio
with gr.Blocks() as demo:
with gr.Row():
title = gr.Textbox(label="Title")
context = gr.Textbox(lines=5, label="Context")
question = gr.Textbox(lines=2, label="Question")
answer = gr.Textbox(lines=2, label="Answer")
answer_start = gr.Number(label="Answer Start")
entry_id = gr.Textbox(label="ID")
add_button = gr.Button("Add Entry")
with gr.Row():
file_name = gr.Textbox(label="File Name", value="squad_dataset")
save_button = gr.Button("Save to JSON")
output = gr.Textbox(label="Output")
download_link = gr.File(label="Download JSON", interactive=False)
add_button.click(fn=add_entry, inputs=[title, context, question, answer, answer_start, entry_id], outputs=output)
save_button.click(fn=save_to_json, inputs=file_name, outputs=download_link)
demo.launch()