ChevalierJoseph commited on
Commit
d4f4f37
·
verified ·
1 Parent(s): 12d5cd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -103
app.py CHANGED
@@ -42,7 +42,7 @@ def generate_glyphs_html(glyphs, cols=5, width=100, height=100):
42
  </g>
43
  </svg>
44
  """
45
- html_parts.append(f"<div style='display: flex; flex-direction: column; align-items: center; margin: 10px; text-align: center; background-color: #f8f9fa; border-radius: 8px; padding: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);'><h3 style='margin-bottom: 8px; color: #333;'>{lettre}</h3>{svg_content}</div>")
46
  grid_style = f"display: grid; grid-template-columns: repeat({cols}, 1fr); gap: 20px;"
47
  return f'<div style="{grid_style}">{"".join(html_parts)}</div>'
48
 
@@ -260,108 +260,52 @@ def respond(message: str, system_message: str, max_tokens: int, temperature: flo
260
  thread.join()
261
 
262
  def create_demo():
263
- theme = gr.themes.Soft(
264
- primary_hue="blue",
265
- secondary_hue="gray",
266
- neutral_hue="slate",
267
- spacing_size="md",
268
- radius_size="lg",
269
- font=[gr.themes.GoogleFont('Inter'), 'ui-sans-serif', 'system-ui'],
270
- ).set(
271
- body_background_fill="#f0f4f8",
272
- block_background_fill="white",
273
- block_border_width="1px",
274
- block_shadow="0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06)",
275
- button_primary_background_fill="#3b82f6",
276
- button_primary_background_fill_hover="#2563eb",
277
- button_primary_text_color="white",
278
- slider_color="#3b82f6",
279
- input_background_fill="#f9fafb",
280
- input_border_color="#d1d5db",
281
- )
282
 
283
- with gr.Blocks(theme=theme, title="TypTopType - Générateur de Glyphes Stylé") as demo:
284
- gr.Markdown(
285
- """
286
- # TypTopType ✨
287
- ## Créez des glyphes personnalisés à partir de votre texte !
288
- Entrez une description, ajustez les paramètres, et générez des glyphes uniques pour vos polices.
289
- """,
290
- elem_classes="header-markdown"
291
- )
292
-
293
- with gr.Row(variant="panel"):
294
  with gr.Column(scale=1):
295
- gr.Markdown("### Entrée Utilisateur")
296
- msg = gr.Textbox(
297
- label="Description du Style de Police",
298
- placeholder="Décrivez le style de vos glyphes (ex: 'une police futuriste avec des courbes fluides')...",
299
- lines=3,
300
- show_label=False,
301
- )
302
- send_btn = gr.Button("Générer Glyphes", variant="primary", size="lg")
303
-
304
- with gr.Accordion("⚙️ Paramètres Avancés", open=False):
305
  system_message = gr.Textbox(
306
  value="Based on the following text, give me the svgpath of the glyphs from A to Z.",
307
- label="Prompt Système",
308
- lines=2,
309
  )
310
  max_tokens = gr.Slider(
311
  minimum=1, maximum=9048, value=9048, step=1, label="Max Tokens"
312
  )
313
  temperature = gr.Slider(
314
- minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Température (Créativité)"
315
  )
316
  top_p = gr.Slider(
317
- minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top P (Diversité)"
318
  )
319
  cols = gr.Slider(
320
- minimum=1, maximum=10, value=5, step=1, label="Nombre de Colonnes"
321
  )
322
  width = gr.Slider(
323
- minimum=50, maximum=200, value=100, step=10, label="Largeur des Glyphes"
324
  )
325
  height = gr.Slider(
326
- minimum=50, maximum=200, value=100, step=10, label="Hauteur des Glyphes"
327
  )
328
-
329
- gr.Markdown("### Exemples Rapides")
330
- examples = gr.Examples(
331
- examples=[
332
- "Une police élégante et classique comme Times New Roman",
333
- "Une police futuriste avec des angles vifs et des effets néon",
334
- "Une police manuscrite cursive et fluide",
335
- ],
336
- inputs=[msg],
337
- label=None,
338
- )
339
-
340
- with gr.Row():
341
- download_btn = gr.Button("Télécharger SVG (ZIP)", variant="secondary", icon="📁")
342
- otf_export_btn = gr.Button("Exporter en OTF", variant="secondary", icon="📱")
343
-
344
  with gr.Column(scale=3):
345
- gr.Markdown("### Aperçu des Glyphes")
346
- svg_preview = gr.HTML(label="Aperçu SVG", show_label=False)
347
- download_output = gr.File(label="Téléchargements", visible=False)
348
 
349
- gr.Markdown("### Aperçu de la Police")
350
- font_preview_text = gr.Textbox(
351
- value="ABCDEFGHIJKLMNOPQRSTUVWXYZ",
352
- label="Testez votre police avec ce texte",
353
- interactive=True,
354
- )
355
- font_preview = gr.HTML(visible=False) # We'll add font preview later if possible
356
-
357
- # Custom CSS for enhanced styling
358
  demo.css = """
359
- .header-markdown { text-align: center; margin-bottom: 20px; }
360
- .gr-button { min-width: 150px; }
361
- .gr-accordion .gr-box { background-color: #ffffff; }
362
- .gr-examples { margin-top: 20px; }
363
- .gr-html svg { transition: transform 0.2s; }
364
- .gr-html svg:hover { transform: scale(1.05); }
 
365
  """
366
 
367
  glyphs_state = gr.State([])
@@ -380,36 +324,33 @@ def create_demo():
380
  glyphs_list = glyphs
381
  svg_html = generate_glyphs_html(glyphs_list, cols=cols, width=width, height=height)
382
  else:
383
- svg_html = "<p style='text-align: center; color: #ef4444;'>Aucun glyphe détecté. Essayez une autre description !</p>"
384
  yield svg_html, glyphs_list
385
 
386
  def download_svg(glyphs, width, height):
387
  if not glyphs:
388
- return gr.Warning("Générez des glyphes d'abord !")
389
  svg_files = generate_svg_files(glyphs, width=width, height=height)
390
  zip_path = create_zip(svg_files)
391
  return zip_path
392
 
393
  def export_to_otf(glyphs):
394
  if not glyphs:
395
- return gr.Warning("Générez des glyphes d'abord !")
396
  with tempfile.NamedTemporaryFile(delete=False, suffix=".otf") as tmp_file:
397
  otf_path = tmp_file.name
398
  created_path = create_otf_font(glyphs, otf_path)
399
  if created_path:
400
  return created_path
401
  else:
402
- return gr.Warning("Erreur lors de la création du fichier OTF.")
403
-
404
- # Note: Font preview would require loading the OTF in browser, which is complex. Skipping for now.
405
 
406
  send_btn.click(
407
  user, [msg, message_history], [msg, message_history], queue=False
408
  ).then(
409
  bot,
410
  [message_history, system_message, max_tokens, temperature, top_p, cols, width, height],
411
- [svg_preview, glyphs_state],
412
- show_progress="full"
413
  )
414
 
415
  msg.submit(
@@ -417,31 +358,23 @@ def create_demo():
417
  ).then(
418
  bot,
419
  [message_history, system_message, max_tokens, temperature, top_p, cols, width, height],
420
- [svg_preview, glyphs_state],
421
- show_progress="full"
422
  )
423
 
424
  download_btn.click(
425
  download_svg,
426
  inputs=[glyphs_state, width, height],
427
- outputs=download_output,
428
- show_progress="full"
429
  )
430
-
431
  otf_export_btn.click(
432
  export_to_otf,
433
  inputs=[glyphs_state],
434
- outputs=download_output,
435
- show_progress="full"
436
  )
437
 
438
- # Add loading indicator for generation
439
- send_btn.click(fn=lambda: gr.Info("Génération en cours... Cela peut prendre un moment."), js="""() => {
440
- document.querySelector('.gr-progress').style.display = 'block';
441
- }""")
442
-
443
- return demo
444
 
445
  demo = create_demo()
 
446
  if __name__ == "__main__":
447
  demo.launch()
 
42
  </g>
43
  </svg>
44
  """
45
+ html_parts.append(f"<div style='display: inline-block; margin: 10px; text-align: center;'><h3>{lettre}</h3>{svg_content}</div>")
46
  grid_style = f"display: grid; grid-template-columns: repeat({cols}, 1fr); gap: 20px;"
47
  return f'<div style="{grid_style}">{"".join(html_parts)}</div>'
48
 
 
260
  thread.join()
261
 
262
  def create_demo():
263
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
264
+ gr.Markdown("# TypTopType")
265
+ gr.Markdown("## Générateur de glyphes à partir de texte")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
 
267
+ with gr.Row():
 
 
 
 
 
 
 
 
 
 
268
  with gr.Column(scale=1):
269
+ msg = gr.Textbox(label="Entrée", placeholder="Tapez votre texte ici...")
270
+ send_btn = gr.Button("Envoyer", variant="primary", icon="📤")
271
+ with gr.Accordion("Paramètres avancés", open=False):
 
 
 
 
 
 
 
272
  system_message = gr.Textbox(
273
  value="Based on the following text, give me the svgpath of the glyphs from A to Z.",
274
+ label="Message système"
 
275
  )
276
  max_tokens = gr.Slider(
277
  minimum=1, maximum=9048, value=9048, step=1, label="Max Tokens"
278
  )
279
  temperature = gr.Slider(
280
+ minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"
281
  )
282
  top_p = gr.Slider(
283
+ minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top P"
284
  )
285
  cols = gr.Slider(
286
+ minimum=1, maximum=10, value=5, step=1, label="Colonnes"
287
  )
288
  width = gr.Slider(
289
+ minimum=50, maximum=200, value=100, step=10, label="Largeur"
290
  )
291
  height = gr.Slider(
292
+ minimum=50, maximum=200, value=100, step=10, label="Hauteur"
293
  )
294
+ download_btn = gr.Button("Télécharger SVG", variant="primary", icon="📁")
295
+ otf_export_btn = gr.Button("Exporter OTF", variant="primary", icon="📱")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
  with gr.Column(scale=3):
297
+ gr.Markdown("## Aperçu")
298
+ svg_preview = gr.HTML(label="Aperçu SVG")
299
+ download_output = gr.File(label="Télécharger ZIP")
300
 
 
 
 
 
 
 
 
 
 
301
  demo.css = """
302
+ .gr-box {
303
+ border-radius: 10px;
304
+ box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
305
+ }
306
+ .gr-button {
307
+ border-radius: 5px;
308
+ }
309
  """
310
 
311
  glyphs_state = gr.State([])
 
324
  glyphs_list = glyphs
325
  svg_html = generate_glyphs_html(glyphs_list, cols=cols, width=width, height=height)
326
  else:
327
+ svg_html = "No glyphs found."
328
  yield svg_html, glyphs_list
329
 
330
  def download_svg(glyphs, width, height):
331
  if not glyphs:
332
+ return None
333
  svg_files = generate_svg_files(glyphs, width=width, height=height)
334
  zip_path = create_zip(svg_files)
335
  return zip_path
336
 
337
  def export_to_otf(glyphs):
338
  if not glyphs:
339
+ return None
340
  with tempfile.NamedTemporaryFile(delete=False, suffix=".otf") as tmp_file:
341
  otf_path = tmp_file.name
342
  created_path = create_otf_font(glyphs, otf_path)
343
  if created_path:
344
  return created_path
345
  else:
346
+ return None
 
 
347
 
348
  send_btn.click(
349
  user, [msg, message_history], [msg, message_history], queue=False
350
  ).then(
351
  bot,
352
  [message_history, system_message, max_tokens, temperature, top_p, cols, width, height],
353
+ [svg_preview, glyphs_state]
 
354
  )
355
 
356
  msg.submit(
 
358
  ).then(
359
  bot,
360
  [message_history, system_message, max_tokens, temperature, top_p, cols, width, height],
361
+ [svg_preview, glyphs_state]
 
362
  )
363
 
364
  download_btn.click(
365
  download_svg,
366
  inputs=[glyphs_state, width, height],
367
+ outputs=download_output
 
368
  )
 
369
  otf_export_btn.click(
370
  export_to_otf,
371
  inputs=[glyphs_state],
372
+ outputs=download_output
 
373
  )
374
 
375
+ return demo
 
 
 
 
 
376
 
377
  demo = create_demo()
378
+
379
  if __name__ == "__main__":
380
  demo.launch()