VirtualOasis commited on
Commit
cf9170b
·
verified ·
1 Parent(s): f7e2faa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -25,21 +25,21 @@ def draw_lattice_grid(draw, width, height, spacing=100, color=(235, 235, 235)):
25
  draw.line([(0, y), (width, y)], fill=color, width=2)
26
 
27
 
28
- def text_to_base64_images_generator(text_content, style='lines'):
29
  """
30
- Converts text into a series of images and returns them as a list
31
- of base64 encoded strings, suitable for an API/MCP client.
32
 
33
  Args:
34
  text_content (str): The text to be converted.
35
  style (str, optional): The background style ('plain', 'lines', 'dots', 'grid').
36
 
37
  Returns:
38
- list: A list of base64 encoded PNG image strings.
39
  """
40
  if not text_content or not text_content.strip():
41
  gr.Warning("Input text is empty. Please enter some text to generate images.")
42
- return []
43
 
44
  # --- Configuration ---
45
  IMG_WIDTH = 1080
@@ -104,6 +104,7 @@ def text_to_base64_images_generator(text_content, style='lines'):
104
  all_lines_and_breaks.append(None)
105
 
106
  # --- Image Generation & Encoding ---
 
107
  generated_base64_list = []
108
  page_content = []
109
  y_text = PADDING_Y
@@ -136,7 +137,10 @@ def text_to_base64_images_generator(text_content, style='lines'):
136
  else:
137
  current_y += PARAGRAPH_SPACING
138
 
139
- # Encode image to base64 string
 
 
 
140
  buffered = io.BytesIO()
141
  img.save(buffered, format="PNG")
142
  img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
@@ -158,7 +162,7 @@ def text_to_base64_images_generator(text_content, style='lines'):
158
  if page_content:
159
  create_and_encode_page(page_content)
160
 
161
- return generated_base64_list
162
 
163
  # --- Gradio Interface ---
164
 
@@ -167,14 +171,17 @@ example_text = """In the heart of a bustling city, there lived a clockmaker name
167
  One day, a young girl with eyes as curious as a cat's wandered into his shop. She wasn't interested in the shiny new watches but was drawn to the grandfather clock in the corner. "What's its story?" she asked, her voice soft. Alistair smiled, for he knew he had found the next guardian of the stories. The legacy of the whispering clock would live on."""
168
 
169
  demo = gr.Interface(
170
- fn=text_to_base64_images_generator,
171
  inputs=[
172
  gr.Textbox(lines=15, label="Text Content", placeholder="Paste your long-form text here...", value=example_text),
173
  gr.Radio(['lines', 'dots', 'grid', 'plain'], label="Background Style", value='lines')
174
  ],
175
- outputs=gr.JSON(label="Base64 Encoded Images"),
176
- title="Text-to-Image API Server",
177
- description="Transforms long-form text into a series of images and returns a JSON list of base64-encoded strings.",
 
 
 
178
  allow_flagging="never"
179
  )
180
 
 
25
  draw.line([(0, y), (width, y)], fill=color, width=2)
26
 
27
 
28
+ def text_to_images_and_base64_generator(text_content, style='lines'):
29
  """
30
+ Converts text into images, returning both PIL objects for UI preview
31
+ and base64 strings for an API/MCP client.
32
 
33
  Args:
34
  text_content (str): The text to be converted.
35
  style (str, optional): The background style ('plain', 'lines', 'dots', 'grid').
36
 
37
  Returns:
38
+ tuple: A tuple containing (list of PIL.Image.Image, list of base64 strings).
39
  """
40
  if not text_content or not text_content.strip():
41
  gr.Warning("Input text is empty. Please enter some text to generate images.")
42
+ return [], []
43
 
44
  # --- Configuration ---
45
  IMG_WIDTH = 1080
 
104
  all_lines_and_breaks.append(None)
105
 
106
  # --- Image Generation & Encoding ---
107
+ generated_images = []
108
  generated_base64_list = []
109
  page_content = []
110
  y_text = PADDING_Y
 
137
  else:
138
  current_y += PARAGRAPH_SPACING
139
 
140
+ # Store the PIL image object for the gallery
141
+ generated_images.append(img)
142
+
143
+ # Encode image to base64 string for the API
144
  buffered = io.BytesIO()
145
  img.save(buffered, format="PNG")
146
  img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
 
162
  if page_content:
163
  create_and_encode_page(page_content)
164
 
165
+ return generated_images, generated_base64_list
166
 
167
  # --- Gradio Interface ---
168
 
 
171
  One day, a young girl with eyes as curious as a cat's wandered into his shop. She wasn't interested in the shiny new watches but was drawn to the grandfather clock in the corner. "What's its story?" she asked, her voice soft. Alistair smiled, for he knew he had found the next guardian of the stories. The legacy of the whispering clock would live on."""
172
 
173
  demo = gr.Interface(
174
+ fn=text_to_images_and_base64_generator,
175
  inputs=[
176
  gr.Textbox(lines=15, label="Text Content", placeholder="Paste your long-form text here...", value=example_text),
177
  gr.Radio(['lines', 'dots', 'grid', 'plain'], label="Background Style", value='lines')
178
  ],
179
+ outputs=[
180
+ gr.Gallery(label="Generated Images (Preview)", show_label=True, preview=True),
181
+ gr.JSON(label="API Output (Base64 Strings)")
182
+ ],
183
+ title="Text-to-Image Server",
184
+ description="Transforms long-form text into images. A gallery is shown for UI preview, and a JSON list of base64 strings is returned for API/MCP clients.",
185
  allow_flagging="never"
186
  )
187