File size: 2,609 Bytes
fbb991c
5527dff
 
8e73422
 
 
 
5527dff
 
 
8e73422
5527dff
bce7832
8e73422
5527dff
fbb991c
 
5527dff
8e73422
 
5527dff
 
8e73422
5527dff
 
 
 
 
 
a681399
 
 
 
 
5527dff
 
 
 
a681399
8e73422
fbb991c
5527dff
 
 
8e73422
5527dff
 
 
8e73422
5527dff
 
 
 
 
 
8e73422
5527dff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a681399
bce7832
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
import gradio as gr
import subprocess
import os
import logging

def setup_cli_tool():
    """Download and set up the CLI tool with proper error handling."""
    CLI_FILENAME = "Texttoimage"
    REPO_ID = "ArrcttacsrjksX/Texttoimage"
    
    if os.path.exists(CLI_FILENAME):
        logging.info(f"CLI tool already exists: {CLI_FILENAME}")
        os.chmod(CLI_FILENAME, 0o755)  # Cấp quyền thực thi
        return True
    
    hf_token = os.environ.get("HF_TOKEN")
    if not hf_token:
        logging.error("HF_TOKEN environment variable not set!")
        return False

def run_texttoimage(text, file, font_size, width, height, bg_color, text_color, mode, font, align):
    command = ["./Texttoimage"]
    
    if text:
        command += ["-t", text]
    elif file:
        command += ["-f", file.name]
    
    command += [
        "--font-size", str(font_size),
        "--width", str(width),
        "--height", str(height),
        "--bg-color", bg_color,
        "--text-color", text_color,
        "--mode", mode,
        "--font", font,
        "--align", align,
        "--output", "output.png"
    ]
    
    try:
        result = subprocess.run(command, capture_output=True, text=True)
        if result.returncode == 0:
            return "output.png"
        else:
            return f"Error: {result.stderr}"
    except Exception as e:
        return f"Execution failed: {e}"

# Danh sách font mẫu
AVAILABLE_FONTS = [
    "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
    "/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf",
    "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf"
]

# Tạo giao diện Gradio
demo = gr.Interface(
    fn=run_texttoimage,
    inputs=[
        gr.Textbox(label="Input Text", lines=3, placeholder="Nhập văn bản..."),
        gr.File(label="Upload File"),
        gr.Slider(10, 100, value=30, label="Font Size"),
        gr.Number(value=800, label="Image Width"),
        gr.Number(value=600, label="Image Height"),
        gr.ColorPicker(value="#FFFFFF", label="Background Color", interactive=True),
        gr.ColorPicker(value="#000000", label="Text Color", interactive=True),
        gr.Radio(["plain", "math"], value="plain", label="Rendering Mode"),
        gr.Dropdown(AVAILABLE_FONTS, value=AVAILABLE_FONTS[0], label="Font Selection"),
        gr.Radio(["left", "center", "right"], value="center", label="Text Alignment")
    ],
    outputs=gr.Image(label="Generated Image"),
    title="Text to Image Converter",
    description="Ứng dụng chuyển văn bản thành ảnh sử dụng Texttoimage."
)

demo.launch()