File size: 3,378 Bytes
71e45d9
a681399
fbb991c
 
 
a681399
fbb991c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a681399
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbb991c
a681399
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess
import gradio as gr
from huggingface_hub import hf_hub_download

# --- Tải về CLI tool từ Hugging Face Hub ---
CLI_FILENAME = "Texttoimage"
if not os.path.exists(CLI_FILENAME):
    hf_token = os.environ.get("HF_TOKEN")
    if not hf_token:
        print("Biến môi trường HF_TOKEN chưa được thiết lập!")
    else:
        try:
            cli_local_path = hf_hub_download(
                repo_id="ArrcttacsrjksX/Texttoimage",
                filename=CLI_FILENAME,
                token=hf_token
            )
            os.rename(cli_local_path, CLI_FILENAME)
            os.chmod(CLI_FILENAME, 0o755)
            print(f"Đã tải về CLI tool: {CLI_FILENAME}")
        except Exception as e:
            print(f"Lỗi khi tải CLI tool: {e}")

# --- Cấu hình giao diện Gradio ---
font_list = ["Arial", "Times New Roman", "Courier New", "Comic Sans MS", "Verdana"]
default_font = "Arial"

def text_to_image(text, font_size, width, height, bg_color, text_color, mode, font_name, align, image_format):
    if not os.path.exists(CLI_FILENAME):
        return "Lỗi: Không tìm thấy Texttoimage. Vui lòng kiểm tra lại."

    command = [
        f"./{CLI_FILENAME}",
        "--text", text,
        "--font-size", str(font_size),
        "--width", str(width),
        "--height", str(height),
        "--bg-color", bg_color,
        "--text-color", text_color,
        "--mode", mode.lower(),
        "--font", font_name,
        "--align", align.lower(),
        "--format", image_format.lower()
    ]

    try:
        output_path = "output_image.png" if image_format == "PNG" else "output_image.jpg"
        command.extend(["--output", output_path])

        subprocess.run(command, check=True)
        return output_path
    except subprocess.CalledProcessError as e:
        return f"Lỗi khi chạy Texttoimage: {e}"

with gr.Blocks() as demo:
    gr.Markdown("# 🖼️ Text to Image Converter")

    with gr.Row():
        input_text = gr.Textbox(label="Enter Text", placeholder="Type or paste text here...", lines=5)
        file_input = gr.File(label="Upload a Text File", type="filepath")

    with gr.Row():
        font_size = gr.Slider(10, 100, value=30, label="Font Size")
        font_name = gr.Dropdown(choices=font_list, value=default_font, label="Font")
        align = gr.Radio(["Left", "Center", "Right"], label="Text Alignment", value="Center")

    with gr.Row():
        width = gr.Slider(200, 2000, value=800, label="Image Width")
        height = gr.Slider(200, 2000, value=600, label="Base Height")

    with gr.Row():
        bg_color = gr.ColorPicker(label="Background Color", value="#FFFFFF")
        text_color = gr.ColorPicker(label="Text Color", value="#000000")

    with gr.Row():
        mode = gr.Radio(["Plain Text", "LaTeX Math"], label="Rendering Mode", value="Plain Text")
        image_format = gr.Radio(["PNG", "JPEG"], label="Image Format", value="PNG")

    output_image = gr.Image(label="Generated Image")

    with gr.Row():
        convert_button = gr.Button("Convert Text to Image")
        file_convert_button = gr.Button("Convert File to Image")

    convert_button.click(
        text_to_image,
        inputs=[input_text, font_size, width, height, bg_color, text_color, mode, font_name, align, image_format],
        outputs=output_image
    )

demo.launch()