SamLemdas commited on
Commit
54a10a6
·
verified ·
1 Parent(s): c40d621

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64, io, random, torch, gradio as gr
2
+ from diffusers import DiffusionPipeline
3
+ from PIL import Image
4
+
5
+ MODEL_ID = "JingyeChen22/textdiffuser2-full-ft"
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ dtype = torch.float16 if device == "cuda" else torch.float32
9
+
10
+ pipe = DiffusionPipeline.from_pretrained(MODEL_ID, torch_dtype=dtype).to(device)
11
+
12
+ def gen_image(prompt, main_text, sub_text, negative_prompt, width, height, steps, guidance, seed):
13
+ # seed
14
+ if seed is None or str(seed).strip() == "":
15
+ seed = random.randint(1, 2**31 - 1)
16
+ g = torch.Generator(device=device).manual_seed(int(seed))
17
+
18
+ # prompt structuré pour TextDiffuser-2
19
+ full_prompt = f"{prompt}\ntext: '{main_text}'\nsubtext: '{sub_text}'"
20
+
21
+ img = pipe(
22
+ prompt=full_prompt,
23
+ negative_prompt=negative_prompt if negative_prompt else None,
24
+ num_inference_steps=int(steps),
25
+ guidance_scale=float(guidance),
26
+ width=int(width),
27
+ height=int(height),
28
+ generator=g
29
+ ).images[0]
30
+
31
+ # retour base64 pour un usage direct via API
32
+ buf = io.BytesIO()
33
+ img.save(buf, format="PNG")
34
+ b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
35
+ return img, str(seed), b64
36
+
37
+ with gr.Blocks() as demo:
38
+ gr.Markdown("### TextDiffuser-2 — API pour visuels LinkedIn (HF Space)")
39
+ with gr.Row():
40
+ with gr.Column():
41
+ prompt = gr.Textbox(label="Background prompt", value="Professional LinkedIn poster background, deep blue gradient, Quebec map in connected nodes, innovation & cybersecurity icons, clean corporate layout, high contrast")
42
+ main_text = gr.Textbox(label="Main title", value="SOUVERAINETÉ NUMÉRIQUE AU QUÉBEC")
43
+ sub_text = gr.Textbox(label="Subtitle", value="Un avantage compétitif réel ?")
44
+ negative_prompt = gr.Textbox(label="Negative prompt (optional)", value="blurry, misspelled text, distorted letters, artifacts, cluttered layout")
45
+ width = gr.Number(label="Width", value=1200, precision=0)
46
+ height = gr.Number(label="Height", value=627, precision=0)
47
+ steps = gr.Slider(10, 60, value=40, step=1, label="Steps")
48
+ guidance = gr.Slider(3.0, 12.0, value=7.5, step=0.5, label="Guidance")
49
+ seed = gr.Textbox(label="Seed (optional)")
50
+ btn = gr.Button("Generate")
51
+ with gr.Column():
52
+ out_img = gr.Image(label="Result", type="pil")
53
+ out_seed = gr.Textbox(label="Used seed")
54
+ out_b64 = gr.Textbox(label="Base64 PNG (for API)")
55
+ btn.click(gen_image, inputs=[prompt, main_text, sub_text, negative_prompt, width, height, steps, guidance, seed],
56
+ outputs=[out_img, out_seed, out_b64])
57
+
58
+ demo.launch()