File size: 2,211 Bytes
cb8d1a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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()