File size: 2,575 Bytes
eb7f075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
import os
import gradio as gr

def read_file(path: str, default_content: str = "") -> str:
    """
    Ensure file exists (with default_content if missing) and return its contents.
    """
    os.makedirs(os.path.dirname(path), exist_ok=True)
    if not os.path.exists(path):
        with open(path, "w", encoding="utf-8") as f:
            f.write(default_content)
    with open(path, "r", encoding="utf-8") as f:
        return f.read()
        
def build_demo(
    generation_fn,
    inputs,
    outputs,
    english_title: str,
    persian_title: str,
    assets_dir: str = "assets",
    app_title: str = "Demo"
):
    """
    args:
        generation_fn: callable for inference
        inputs: list of Gradio input components
        outputs: list of Gradio output components
    """
    md_dir = os.path.join(assets_dir, "markdown")
    css_dir = os.path.join(assets_dir, "css")
    english_md = os.path.join(md_dir, "english_summary.md")
    persian_md = os.path.join(md_dir, "persian_summary.md")
    english_summary = read_file(english_md)
    persian_summary = read_file(persian_md)

    css_file = os.path.join(css_dir, "custom.css")
    css = read_file(css_file, "/* Custom CSS overrides */\n")

    with gr.Blocks(css=css, title=app_title) as demo:
        title_md = gr.Markdown(english_title, elem_id="title")

        with gr.Row():
            english_btn = gr.Button("English")
            persian_btn = gr.Button("فارسی (Persian)")

        summary_md = gr.Markdown(english_summary, elem_id="summary")

        # generation panel
        with gr.Row(variant="panel"):
            with gr.Column(scale=1, variant="panel"):
                for inp in inputs:
                    inp.render()
                generate_btn = gr.Button("✨ Translate", variant="primary")

            with gr.Column(scale=1, variant="panel"):
                for out in outputs:
                    out.render()

        # events
        generate_btn.click(generation_fn, inputs=inputs, outputs=outputs)

        def set_english():
            return (
                gr.update(value=english_title, elem_classes=[]),
                gr.update(value=english_summary, elem_classes=[]),
            )

        def set_persian():
            return (
                gr.update(value=persian_title, elem_classes=["persian"]),
                gr.update(value=persian_summary, elem_classes=["persian"]),
            )

        english_btn.click(set_english, outputs=[title_md, summary_md])
        persian_btn.click(set_persian, outputs=[title_md, summary_md])

    return demo