ArrcttacsrjksX commited on
Commit
b59e314
·
verified ·
1 Parent(s): eec0d01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -6
app.py CHANGED
@@ -1,11 +1,58 @@
 
1
  import gradio as gr
2
  import subprocess
3
  import json
4
  import os
 
 
 
5
 
6
  # Path to the compiled engine executable
7
  ENGINE_EXECUTABLE = "./engine" # File thực thi trên Linux
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def call_engine(input_text, font_size, width, height, bg_color, text_color, mode, font_name, align, line_spacing, image_format):
10
  # Prepare input data as a dictionary
11
  input_data = {
@@ -16,22 +63,23 @@ def call_engine(input_text, font_size, width, height, bg_color, text_color, mode
16
  "bg_color": bg_color,
17
  "text_color": text_color,
18
  "mode": mode,
19
- "font_path": font_name,
20
  "align": align,
21
  "line_spacing": line_spacing,
22
  "image_format": image_format
23
  }
24
 
25
- # Convert input data to JSON string
26
- input_json = json.dumps(input_data)
27
-
28
  # Call the engine executable with input data
29
  result = subprocess.run(
30
- [ENGINE_EXECUTABLE, input_json],
31
  capture_output=True,
32
  text=True
33
  )
34
 
 
 
 
 
35
  # Get the output image path from stdout
36
  output_path = result.stdout.strip()
37
 
@@ -50,7 +98,7 @@ with gr.Blocks() as demo:
50
 
51
  with gr.Row():
52
  font_size = gr.Slider(10, 100, value=30, label="Font Size")
53
- font_name = gr.Dropdown(choices=[], value="", label="Font") # Populate dynamically
54
  align = gr.Radio(["Left", "Center", "Right"], label="Text Alignment", value="Center")
55
 
56
  with gr.Row():
 
1
+ # interface.py
2
  import gradio as gr
3
  import subprocess
4
  import json
5
  import os
6
+ import zipfile
7
+ from PIL import ImageFont
8
+ import matplotlib.font_manager
9
 
10
  # Path to the compiled engine executable
11
  ENGINE_EXECUTABLE = "./engine" # File thực thi trên Linux
12
 
13
+ def extract_and_load_fonts_from_directory(directory="fontfile", extract_to="extracted_fonts"):
14
+ if not os.path.exists(extract_to):
15
+ os.makedirs(extract_to)
16
+
17
+ fonts = []
18
+
19
+ # Extract fonts from zip files
20
+ for root, dirs, files in os.walk(directory):
21
+ for file in files:
22
+ if file.endswith(".zip"):
23
+ zip_path = os.path.join(root, file)
24
+ try:
25
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
26
+ zip_ref.extractall(extract_to)
27
+ print(f"Extracted: {zip_path}")
28
+ except Exception as e:
29
+ print(f"Failed to extract {zip_path}: {e}")
30
+
31
+ # Collect all .ttf and .shx fonts
32
+ for root, dirs, files in os.walk(extract_to):
33
+ for file in files:
34
+ if file.endswith(".ttf") or file.endswith(".shx"):
35
+ fonts.append(os.path.join(root, file))
36
+
37
+ return fonts
38
+
39
+ def get_system_fonts():
40
+ fonts = []
41
+ for font in matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf'):
42
+ fonts.append(font)
43
+ return fonts
44
+
45
+ def get_available_fonts():
46
+ # Get system fonts
47
+ system_fonts = get_system_fonts()
48
+
49
+ # Extract and load custom fonts
50
+ extracted_fonts = extract_and_load_fonts_from_directory()
51
+
52
+ # Combine and deduplicate fonts
53
+ all_fonts = list(set(system_fonts + extracted_fonts))
54
+ return sorted(all_fonts)
55
+
56
  def call_engine(input_text, font_size, width, height, bg_color, text_color, mode, font_name, align, line_spacing, image_format):
57
  # Prepare input data as a dictionary
58
  input_data = {
 
63
  "bg_color": bg_color,
64
  "text_color": text_color,
65
  "mode": mode,
66
+ "font_path": font_name, # Pass the selected font path
67
  "align": align,
68
  "line_spacing": line_spacing,
69
  "image_format": image_format
70
  }
71
 
 
 
 
72
  # Call the engine executable with input data
73
  result = subprocess.run(
74
+ [ENGINE_EXECUTABLE, json.dumps(input_data)],
75
  capture_output=True,
76
  text=True
77
  )
78
 
79
+ # Handle errors
80
+ if result.returncode != 0:
81
+ raise Exception(f"Engine failed with error: {result.stderr}")
82
+
83
  # Get the output image path from stdout
84
  output_path = result.stdout.strip()
85
 
 
98
 
99
  with gr.Row():
100
  font_size = gr.Slider(10, 100, value=30, label="Font Size")
101
+ font_name = gr.Dropdown(choices=get_available_fonts(), value="", label="Font")
102
  align = gr.Radio(["Left", "Center", "Right"], label="Text Alignment", value="Center")
103
 
104
  with gr.Row():