keinne commited on
Commit
5215eed
·
verified ·
1 Parent(s): 73d5d1e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +39 -25
app.py CHANGED
@@ -1,6 +1,7 @@
1
  from pathlib import Path
2
  import sys
3
- import tempfile
 
4
 
5
  import gradio as gr
6
 
@@ -14,35 +15,38 @@ from dot2dot.config import AGE_PRESETS, KDP_DEFAULTS
14
 
15
 
16
  PIPELINE = DotToDotPipeline()
 
 
17
 
18
 
19
  def run_single_image(image, age_key, title, include_reference, trim_size):
20
  if image is None:
21
  raise gr.Error("Envie uma imagem de contorno preto e branco.")
22
 
23
- with tempfile.TemporaryDirectory() as temp_dir:
24
- temp_path = Path(temp_dir)
25
- result = PIPELINE.process_uploaded_image(
26
- image=image,
27
- output_dir=temp_path,
28
- age_key=age_key,
29
- page_title=title or "Dot to Dot",
30
- include_reference=include_reference,
31
- trim_size=trim_size,
32
- )
33
-
34
- summary = [
35
- f"Faixa etaria: {AGE_PRESETS[age_key].label}",
36
- f"Pontos gerados: {result.dot_count}",
37
- f"Tamanho final sugerido: {trim_size}",
38
- f"Miolo compativel com KDP: {'sim' if result.kdp_ready else 'revisar'}",
39
- ]
40
- return (
41
- str(result.activity_image),
42
- str(result.answer_image),
43
- str(result.bundle_zip),
44
- "\n".join(summary),
45
- )
 
46
 
47
 
48
  with gr.Blocks(theme=gr.themes.Soft(), title="Dot2Dot HF Pack") as demo:
@@ -88,4 +92,14 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Dot2Dot HF Pack") as demo:
88
 
89
 
90
  if __name__ == "__main__":
91
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
 
 
 
1
  from pathlib import Path
2
  import sys
3
+ import os
4
+ from uuid import uuid4
5
 
6
  import gradio as gr
7
 
 
15
 
16
 
17
  PIPELINE = DotToDotPipeline()
18
+ RUNS_DIR = Path("/tmp/dot2dot_runs")
19
+ RUNS_DIR.mkdir(parents=True, exist_ok=True)
20
 
21
 
22
  def run_single_image(image, age_key, title, include_reference, trim_size):
23
  if image is None:
24
  raise gr.Error("Envie uma imagem de contorno preto e branco.")
25
 
26
+ temp_path = RUNS_DIR / uuid4().hex
27
+ temp_path.mkdir(parents=True, exist_ok=True)
28
+ result = PIPELINE.process_uploaded_image(
29
+ image=image,
30
+ output_dir=temp_path,
31
+ age_key=age_key,
32
+ page_title=title or "Dot to Dot",
33
+ include_reference=include_reference,
34
+ trim_size=trim_size,
35
+ )
36
+
37
+ summary = [
38
+ f"Faixa etaria: {AGE_PRESETS[age_key].label}",
39
+ f"Pontos gerados: {result.dot_count}",
40
+ f"Tamanho final sugerido: {trim_size}",
41
+ f"Miolo compativel com KDP: {'sim' if result.kdp_ready else 'revisar'}",
42
+ f"Pacote salvo em: {result.bundle_zip.name}",
43
+ ]
44
+ return (
45
+ str(result.activity_image),
46
+ str(result.answer_image),
47
+ str(result.bundle_zip),
48
+ "\n".join(summary),
49
+ )
50
 
51
 
52
  with gr.Blocks(theme=gr.themes.Soft(), title="Dot2Dot HF Pack") as demo:
 
92
 
93
 
94
  if __name__ == "__main__":
95
+ is_space = bool(os.getenv("SPACE_ID") or os.getenv("SYSTEM") == "spaces")
96
+ launch_kwargs = {
97
+ "show_api": False,
98
+ "allowed_paths": [str(RUNS_DIR)],
99
+ }
100
+ if is_space:
101
+ launch_kwargs["share"] = True
102
+ else:
103
+ launch_kwargs["server_name"] = "0.0.0.0"
104
+ launch_kwargs["server_port"] = 7860
105
+ demo.launch(**launch_kwargs)