agentif_viewer / app.py
ehsk
initial commit
cb8d1a9
import json
import requests
import gradio as gr
# Load the JSON data once at startup
URL = "https://github.com/THU-KEG/AgentIF/raw/refs/heads/main/data/eval.json"
response = requests.get(URL)
data = response.json()
def format_input(input_list):
if not isinstance(input_list, list):
return "Invalid format for 'input' field."
formatted = []
for msg in input_list:
role = msg.get("role", "unknown").capitalize()
content = msg.get("content", "").strip()
block = f"### {role}:\n```\n{content}\n```"
formatted.append(block)
return "\n\n".join(formatted)
def get_entry(index):
try:
index = int(index)
if index < 0 or index >= len(data):
return f"Index {index} out of range.", "", ""
entry = data[index]
entry_type = entry.get("type", "")
# Prepare input
input_formatted = format_input(entry.get("input", []))
# Raw data excluding exec, output, and input
raw_data = json.dumps(entry, indent=2, ensure_ascii=False)
print(f"Showing entry {index}: {entry_type}")
return f"Showing entry {index}", input_formatted, raw_data
except ValueError:
return "Please enter a valid integer index.", "", ""
with gr.Blocks(title="AgentIF Viewer") as demo:
gr.Markdown("# AgentIF Viewer")
gr.Markdown(f"πŸ” [AgentIF](https://arxiv.org/abs/2505.16944) contains examples in **both English and Chinese**. Use the index below to browse them.")
index_range = f"[0, {len(data)})"
index_input = gr.Textbox(
label="Enter Index",
value="100",
info=f"Valid range: {index_range}",
placeholder="Enter an integer index"
)
status_output = gr.Textbox(label="Status", lines=1)
input_md = gr.Markdown(label="Input")
with gr.Accordion("Raw Data", open=False):
raw_data = gr.Code(label="Raw JSON (excluding input/output/exec)", language="json")
index_input.change(fn=get_entry, inputs=index_input, outputs=[status_output, input_md, raw_data])
demo.load(fn=lambda: get_entry("100"), outputs=[
status_output, input_md, raw_data
])
if __name__ == "__main__":
demo.launch()