File size: 5,002 Bytes
63a30f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import random
import tempfile

import gradio as gr

from main import create_code_gif


def _create_code_gif(
    code: str,
    style_name: str = "monokai",
    start_color: str = "#FF6B6B",
    end_color: str = "#4ECDC4",
    title: str = "Code GIF",
    favicon: str = "https://huggingface.co/front/assets/huggingface_logo-noborder.svg",
    photo: str = "https://photo.jpg",
    aspect_ratio: float = 16 / 9,
):
    # Convert hex colors to RGB tuples
    def parse_color(color: str) -> tuple:
        if color.startswith("rgba"):
            # Extract numbers from rgba(r, g, b, a) format
            values = color.strip("rgba()").split(",")
            return tuple(int(float(x)) for x in values[:3])  # Only use RGB values
        else:
            # Handle hex colors
            return tuple(int(color.lstrip("#")[i : i + 2], 16) for i in (0, 2, 4))

    start_rgb = parse_color(start_color)
    end_rgb = parse_color(end_color)

    with tempfile.NamedTemporaryFile(suffix=".gif", delete=False) as tmp:
        create_code_gif(
            code=code,
            output_file=tmp.name,
            style=style_name,
            gradient_start=start_rgb,
            gradient_end=end_rgb,
            line_numbers=False,
            title=title,
            filename="code.py",
            favicon=favicon,
            photo=photo,
            aspect_ratio=aspect_ratio,
        )
        return tmp.name


code_placeholder = """
def main():
    print("Hello, World!")


if __name__ == "__main__":
    main()
"""


# Create Gradio interface
def get_random_color():
    return f"#{random.randint(0, 0xFFFFFF):06x}"


start_color = get_random_color()
end_color = get_random_color()

with gr.Blocks(title="Python Code GIF Generator") as demo:
    gr.Markdown("# Python Code GIF Generator")
    gr.Markdown("Generate animated code GIFs with customizable styles and effects")

    with gr.Row():
        with gr.Column():
            gr.Markdown("### Input Settings")
            code = gr.Code(
                label="Code", lines=10, language="python", value=code_placeholder
            )
            title = gr.Textbox(
                label="Title", value="Code GIF", placeholder="My animated code"
            )
            with gr.Row(variant="compact"):
                style = gr.Dropdown(
                    choices=[
                        "monokai",
                        "dracula",
                        "nord-darker",
                        "gruvbox-dark",
                        "solarized-dark",
                        "one-dark",
                        "github-dark",
                        "material",
                        "zenburn",
                        "vs-dark",
                        "tomorrow-night",
                        "paraiso-dark",
                        "native",
                        "fruity",
                        "vim",
                    ],
                    label="Style",
                    value="monokai",
                )
                aspect_ratio = gr.Dropdown(
                    label="Aspect Ratio",
                    choices=[("Portrait (9:16)", 9 / 16), ("Landscape (16:9)", 16 / 9)],
                    value=9 / 16,
                    type="value",
                )
            with gr.Row(variant="compact"):
                start_color_picker = gr.ColorPicker(
                    label="Start Color", value=start_color
                )
                end_color_picker = gr.ColorPicker(label="End Color", value=end_color)
                refresh_colors = gr.Button("Refresh Colors")
            with gr.Row():
                photo = gr.Textbox(
                    label="Photo",
                    value="https://www.indiewire.com/wp-content/uploads/2017/10/matrix-code.jpg",
                    placeholder="https://www.indiewire.com/wp-content/uploads/2017/10/matrix-code.jpg",
                )
                favicon = gr.Textbox(
                    label="Favicon",
                    value="https://huggingface.co/front/assets/huggingface_logo-noborder.svg",
                    placeholder="https://huggingface.co/front/assets/huggingface_logo-noborder.svg",
                )
            with gr.Row():
                submit_btn = gr.Button("Generate GIF")

        # Right column - Output display
        with gr.Column():
            gr.Markdown("### Generated Output")
            output_image = gr.Image(label="Generated GIF", type="filepath")

    submit_btn.click(
        fn=_create_code_gif,
        inputs=[
            code,
            style,
            start_color_picker,
            end_color_picker,
            title,
            favicon,
            photo,
            aspect_ratio,
        ],
        outputs=output_image,
    )
    gr.on(
        [refresh_colors.click, demo.load],
        fn=lambda: (get_random_color(), get_random_color()),
        inputs=[],
        outputs=[start_color_picker, end_color_picker],
    )


if __name__ == "__main__":
    demo.launch()