TextToImage / app.py
ArrcttacsrjksX's picture
Update app.py
5527dff verified
raw
history blame
2.54 kB
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}")
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()