Spaces:
Sleeping
Sleeping
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() | |