letxinet / modules /metadata_tab.py
C2MV's picture
Initial upload for Build Small Hackathon
68fb5e2 verified
Raw
History Blame Contribute Delete
1.88 kB
import gradio as gr
import json
from backend.tools.metadata import fetch_metadata, recover_metadata
from .utils import format_error
async def metadata_handler(doi, url, title, action):
if not doi and not url and not title:
return "**Error:** Proporcione al menos un DOI, URL o t铆tulo."
try:
if action == "fetch":
result = await fetch_metadata(doi=doi or None, url=url or None, title=title or None)
elif action == "recover":
result = await recover_metadata(doi=doi or None, url=url or None, title=title or None)
elif action == "enrich":
result = await fetch_metadata(doi=doi or None, url=url or None, title=title or None)
else:
return "**Error:** Acci贸n no v谩lida."
if "error" in result:
return f"**Error:** {result['error']}"
return json.dumps(result, indent=2, ensure_ascii=False, default=str)
except Exception as e:
return format_error(e)
def create_metadata_tab():
with gr.Tab("馃搵 Metadatos", id="metadata"):
gr.Markdown("## Gesti贸n de Metadatos Acad茅micos")
with gr.Row():
with gr.Column():
doi = gr.Textbox(label="DOI", placeholder="10.1038/s41586-020-2649-2")
url = gr.Textbox(label="URL / Handle", placeholder="https://hdl.handle.net/20.500.12672/12345")
title = gr.Textbox(label="T铆tulo", placeholder="Attention Is All You Need")
action = gr.Radio(choices=["fetch", "recover", "enrich"], value="fetch", label="Acci贸n")
meta_btn = gr.Button("馃搵 Obtener Metadatos", variant="primary", size="lg")
with gr.Column():
output_json = gr.Code(label="Resultado JSON", language="json")
meta_btn.click(fn=metadata_handler, inputs=[doi, url, title, action], outputs=[output_json])