Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ from huggingface_hub import HfApi, HfFolder
|
|
12 |
# Hugging Face repo and token
|
13 |
HF_REPO = "ArrcttacsrjksX/Texttoimage"
|
14 |
HF_ENGINE_URL = "https://huggingface.co/ArrcttacsrjksX/Texttoimage/resolve/main/engine"
|
15 |
-
HF_TOKEN = os.getenv("HF_TOKEN") #
|
16 |
ENGINE_EXECUTABLE = "./engine"
|
17 |
|
18 |
def download_engine():
|
@@ -29,12 +29,10 @@ def download_engine():
|
|
29 |
raise Exception("Failed to download engine")
|
30 |
|
31 |
def ensure_executable(file_path):
|
32 |
-
"""Ensure the file is executable."""
|
33 |
if not os.access(file_path, os.X_OK):
|
34 |
os.chmod(file_path, os.stat(file_path).st_mode | stat.S_IXUSR)
|
35 |
|
36 |
def extract_and_load_fonts(directory="fontfile", extract_to="extracted_fonts"):
|
37 |
-
"""Extract fonts from ZIP files and load them."""
|
38 |
if not os.path.exists(extract_to):
|
39 |
os.makedirs(extract_to)
|
40 |
fonts = []
|
@@ -54,19 +52,11 @@ def extract_and_load_fonts(directory="fontfile", extract_to="extracted_fonts"):
|
|
54 |
return fonts
|
55 |
|
56 |
def get_system_fonts():
|
57 |
-
"""Get system-installed fonts."""
|
58 |
return matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
|
59 |
|
60 |
def get_available_fonts():
|
61 |
-
"""Get a prioritized list of available fonts."""
|
62 |
system_fonts = get_system_fonts()
|
63 |
extracted_fonts = extract_and_load_fonts()
|
64 |
-
# Prioritize fonts with good Unicode support
|
65 |
-
preferred_fonts = ["DejaVuSans.ttf", "Arial.ttf", "LiberationSans-Regular.ttf"]
|
66 |
-
for font in preferred_fonts:
|
67 |
-
for path in system_fonts + extracted_fonts:
|
68 |
-
if font in path:
|
69 |
-
return [path] + sorted(set(system_fonts + extracted_fonts))
|
70 |
return sorted(set(system_fonts + extracted_fonts))
|
71 |
|
72 |
def upload_to_huggingface(file_path, text_content, timestamp_folder):
|
@@ -81,17 +71,10 @@ def upload_to_huggingface(file_path, text_content, timestamp_folder):
|
|
81 |
os.remove("temp_text.txt")
|
82 |
|
83 |
def call_engine(file_input, input_text, font_size, width, height, bg_color, text_color, mode, font_name, align, line_spacing, image_format):
|
84 |
-
"""Call the engine to generate an image."""
|
85 |
download_engine()
|
86 |
ensure_executable(ENGINE_EXECUTABLE)
|
87 |
if file_input:
|
88 |
input_text = read_file_content(file_input)
|
89 |
-
|
90 |
-
# Validate LaTeX math syntax
|
91 |
-
if mode == "LaTeX Math":
|
92 |
-
if not input_text.startswith("$") or not input_text.endswith("$"):
|
93 |
-
input_text = f"${input_text}$"
|
94 |
-
|
95 |
input_data = {
|
96 |
"input_text": input_text,
|
97 |
"font_size": font_size,
|
@@ -155,4 +138,4 @@ with gr.Blocks() as demo:
|
|
155 |
outputs=output_image
|
156 |
)
|
157 |
|
158 |
-
demo.launch()
|
|
|
12 |
# Hugging Face repo and token
|
13 |
HF_REPO = "ArrcttacsrjksX/Texttoimage"
|
14 |
HF_ENGINE_URL = "https://huggingface.co/ArrcttacsrjksX/Texttoimage/resolve/main/engine"
|
15 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Lấy token từ biến môi trường
|
16 |
ENGINE_EXECUTABLE = "./engine"
|
17 |
|
18 |
def download_engine():
|
|
|
29 |
raise Exception("Failed to download engine")
|
30 |
|
31 |
def ensure_executable(file_path):
|
|
|
32 |
if not os.access(file_path, os.X_OK):
|
33 |
os.chmod(file_path, os.stat(file_path).st_mode | stat.S_IXUSR)
|
34 |
|
35 |
def extract_and_load_fonts(directory="fontfile", extract_to="extracted_fonts"):
|
|
|
36 |
if not os.path.exists(extract_to):
|
37 |
os.makedirs(extract_to)
|
38 |
fonts = []
|
|
|
52 |
return fonts
|
53 |
|
54 |
def get_system_fonts():
|
|
|
55 |
return matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
|
56 |
|
57 |
def get_available_fonts():
|
|
|
58 |
system_fonts = get_system_fonts()
|
59 |
extracted_fonts = extract_and_load_fonts()
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
return sorted(set(system_fonts + extracted_fonts))
|
61 |
|
62 |
def upload_to_huggingface(file_path, text_content, timestamp_folder):
|
|
|
71 |
os.remove("temp_text.txt")
|
72 |
|
73 |
def call_engine(file_input, input_text, font_size, width, height, bg_color, text_color, mode, font_name, align, line_spacing, image_format):
|
|
|
74 |
download_engine()
|
75 |
ensure_executable(ENGINE_EXECUTABLE)
|
76 |
if file_input:
|
77 |
input_text = read_file_content(file_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
input_data = {
|
79 |
"input_text": input_text,
|
80 |
"font_size": font_size,
|
|
|
138 |
outputs=output_image
|
139 |
)
|
140 |
|
141 |
+
demo.launch()
|