akhaliq HF Staff commited on
Commit
50d60ca
·
1 Parent(s): 403723e

Update workflow to use Nemotron Prompt Generator and Ideogram 4 Image Generator

Browse files
Files changed (2) hide show
  1. app.py +60 -42
  2. workflow.json +150 -1
app.py CHANGED
@@ -1,56 +1,74 @@
1
  import os
2
- import random
3
-
4
  import gradio as gr
 
5
 
6
 
7
- def describe_product(image) -> str:
8
- if image is None:
9
- return "a premium product"
10
- img_path = image.get("path") if isinstance(image, dict) else str(image)
11
- img_url = image.get("url", "") if isinstance(image, dict) else str(image)
 
12
  try:
13
- from gradio_client import Client, handle_file
14
- source = img_path if (img_path and os.path.exists(img_path)) else img_url
15
- if not source:
16
- return "a premium product"
17
- client = Client("vikhyatk/moondream2", verbose=False)
 
 
 
 
 
 
 
 
18
  result = client.predict(
19
- handle_file(source),
20
- "Describe this product briefly and concisely. What is it?",
21
- api_name="/answer_question",
 
 
22
  )
23
- return str(result).strip() if result else "a premium product"
24
- except Exception:
25
- return "a premium product"
 
 
 
 
 
 
26
 
27
 
28
- def craft_marketing_prompt(caption: str) -> str:
29
- if not caption:
30
- caption = "a premium product"
31
- caption = caption.strip().rstrip(".")
32
- styles = [
33
- (
34
- f"Professional product advertisement photograph of {caption}, "
35
- "studio lighting, clean white background, commercial photography, "
36
- "ultra-sharp, 8K quality"
37
- ),
38
- (
39
- f"Cinematic product shot of {caption}, dramatic lighting, "
40
- "aspirational lifestyle context, premium brand aesthetic, "
41
- "shot on Hasselblad, magazine cover quality"
42
- ),
43
- (
44
- f"Bold marketing campaign visual of {caption}, vibrant colors, "
45
- "dynamic composition, modern editorial style, "
46
- "award-winning commercial photography"
47
- ),
48
- ]
49
- return random.choice(styles)
 
 
50
 
51
 
52
- demo = gr.Workflow(bind=[describe_product, craft_marketing_prompt])
53
 
54
  if __name__ == "__main__":
55
  demo.launch()
56
-
 
1
  import os
2
+ import json
 
3
  import gradio as gr
4
+ from gradio_client import Client
5
 
6
 
7
+ def generate_prompt(concept: str) -> str:
8
+ """
9
+ Expands a simple concept into a detailed image prompt using the NVIDIA Nemotron model.
10
+ """
11
+ if not concept:
12
+ return "a ginger cat wearing a tiny wizard hat reading a spellbook"
13
  try:
14
+ client = Client("akhaliq/NVIDIA-Nemotron-3-Ultra-550B-A55B-NVFP4", verbose=False)
15
+ system_instruction = (
16
+ "You are an expert prompt engineer for text-to-image models. "
17
+ "Your task is to take a simple concept and expand it into a detailed, "
18
+ "vivid, and high-quality image prompt for Ideogram. "
19
+ "Describe the scene, lighting, materials, and aesthetic in detail. "
20
+ "Provide ONLY the final prompt text. Do not include any introductory or concluding text, "
21
+ "do not provide multiple options, and do not wrap the prompt in quotes."
22
+ )
23
+ messages = [
24
+ {"role": "system", "content": system_instruction},
25
+ {"role": "user", "content": f"Concept: {concept}"}
26
+ ]
27
  result = client.predict(
28
+ messages_json=json.dumps(messages),
29
+ temperature=0.7,
30
+ max_tokens=256,
31
+ custom_token=None,
32
+ api_name="/chat"
33
  )
34
+ clean_result = str(result).strip()
35
+ if clean_result.startswith('"') and clean_result.endswith('"'):
36
+ clean_result = clean_result[1:-1]
37
+ elif clean_result.startswith("'") and clean_result.endswith("'"):
38
+ clean_result = clean_result[1:-1]
39
+ return clean_result
40
+ except Exception as e:
41
+ print(f"Error calling Nemotron space: {e}")
42
+ return f"A detailed, high-quality, professional commercial product photograph of {concept}"
43
 
44
 
45
+ def generate_image(prompt: str) -> str:
46
+ """
47
+ Generates an image from a prompt using the Ideogram 4 model.
48
+ """
49
+ if not prompt:
50
+ prompt = "a ginger cat wearing a tiny wizard hat reading a spellbook"
51
+ try:
52
+ client = Client("ideogram-ai/ideogram4", verbose=False)
53
+ result = client.predict(
54
+ prompt=prompt,
55
+ mode="Default · 20 steps",
56
+ upsampler="Ideogram (remote)",
57
+ width=1024,
58
+ height=1024,
59
+ seed=0,
60
+ randomize_seed=True,
61
+ api_name="/generate"
62
+ )
63
+ if isinstance(result, (list, tuple)) and len(result) > 0:
64
+ return result[0]
65
+ return result
66
+ except Exception as e:
67
+ print(f"Error calling Ideogram 4 space: {e}")
68
+ raise e
69
 
70
 
71
+ demo = gr.Workflow(bind=[generate_prompt, generate_image])
72
 
73
  if __name__ == "__main__":
74
  demo.launch()
 
workflow.json CHANGED
@@ -1 +1,150 @@
1
- {"schema_version":"2","name":"Marketing Image Creator","runtime":{"default":"client"},"references":[{"label":"Text","inputs":[{"id":"in","label":"Text","type":"text"}],"outputs":[{"id":"out","label":"Text","type":"text"}],"width":220,"height":163,"asset_type":"text","role":"reference","id":"1539a1e9-4906-4008-b345-e6d05bb69b22","x":740,"y":80,"data":{"in":null}},{"label":"Image","inputs":[{"id":"in","label":"Image","type":"image"}],"outputs":[{"id":"out","label":"Image","type":"image"}],"width":220,"height":124,"asset_type":"image","role":"reference","id":"13e92b22-7173-416c-81c6-1ca097a2a299","x":80,"y":80,"data":{}}],"operators":[{"id":"n_flux","label":"Generate Image","inputs":[{"id":"in","label":"Prompt","type":"text","required":true}],"outputs":[{"id":"out","label":"Image","type":"image"}],"data":{"out":97500097},"x":1040,"y":80,"width":220,"height":124,"role":"operator","kind":"space","source":"hf://spaces/multimodalart/FLUX.1-merged","space_id":"multimodalart/FLUX.1-merged","runtime":"client","endpoints":[{"name":"/infer","inputs":[{"id":"in_0","label":"Prompt","type":"text","required":true},{"id":"in_1","label":"Seed","type":"number","required":false},{"id":"in_2","label":"Randomize seed","type":"boolean","required":false},{"id":"in_3","label":"Width","type":"number","required":false},{"id":"in_4","label":"Height","type":"number","required":false},{"id":"in_5","label":"Guidance Scale","type":"number","required":false},{"id":"in_6","label":"Number of inference steps","type":"number","required":false}],"outputs":[{"id":"out_0","label":"Result","type":"image","output_index":0},{"id":"out_1","label":"Seed","type":"number","output_index":1}]}]},{"label":"Image To Prompt","inputs":[{"id":"in_0","label":"Input Image","type":"image","required":true}],"outputs":[{"id":"out_0","label":"Output Prompt","type":"text","output_index":0}],"width":280,"height":124,"kind":"space","space_id":"ovi054/image-to-prompt","endpoint":"/predict","endpoints":[{"name":"/predict","inputs":[{"id":"in_0","label":"Input Image","type":"image","required":true}],"outputs":[{"id":"out_0","label":"Output Prompt","type":"text","output_index":0}]}],"pipeline_tag":"Image Captioning","role":"operator","id":"897592dc-17b5-473b-9e89-4ebdae4073d6","x":380,"y":80,"data":{}}],"subjects":[{"id":"n_output","label":"Marketing Image","inputs":[{"id":"in","label":"Image","type":"image"}],"outputs":[{"id":"out","label":"Image","type":"image"}],"data":{"in":null},"x":1340,"y":80,"width":220,"height":107,"role":"subject","asset_type":"image"}],"edges":[{"id":"e4","from_node_id":"n_flux","from_port_id":"out","to_node_id":"n_output","to_port_id":"in","type":"image"},{"from_node_id":"1539a1e9-4906-4008-b345-e6d05bb69b22","from_port_id":"out","to_node_id":"n_flux","to_port_id":"in","type":"text","id":"b44e83fe-23b1-4dcc-83d3-55b3481721bc"},{"from_node_id":"13e92b22-7173-416c-81c6-1ca097a2a299","from_port_id":"out","to_node_id":"897592dc-17b5-473b-9e89-4ebdae4073d6","to_port_id":"in_0","type":"image","id":"22796709-ff7d-4250-92c0-2d541f9e6a99"},{"from_node_id":"897592dc-17b5-473b-9e89-4ebdae4073d6","from_port_id":"out_0","to_node_id":"1539a1e9-4906-4008-b345-e6d05bb69b22","to_port_id":"in","type":"text","id":"96dab823-0f2c-4356-98c4-aa82feca5e0a"}],"view":{"default":"canvas"}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "schema_version": "2",
3
+ "name": "Ideogram 4 Workflow",
4
+ "runtime": {
5
+ "default": "client"
6
+ },
7
+ "references": [
8
+ {
9
+ "id": "ref_concept",
10
+ "label": "Text",
11
+ "inputs": [
12
+ {
13
+ "id": "in",
14
+ "label": "Text",
15
+ "type": "text"
16
+ }
17
+ ],
18
+ "outputs": [
19
+ {
20
+ "id": "out",
21
+ "label": "Text",
22
+ "type": "text"
23
+ }
24
+ ],
25
+ "width": 220,
26
+ "height": 163,
27
+ "asset_type": "text",
28
+ "role": "reference",
29
+ "data": {
30
+ "in": "a ginger cat wearing a tiny wizard hat reading a spellbook"
31
+ },
32
+ "x": 80,
33
+ "y": 80
34
+ }
35
+ ],
36
+ "operators": [
37
+ {
38
+ "id": "op_generate_prompt",
39
+ "label": "Nemotron Prompt Generator",
40
+ "role": "operator",
41
+ "kind": "fn",
42
+ "fn": "generate_prompt",
43
+ "inputs": [
44
+ {
45
+ "id": "in_0",
46
+ "label": "concept",
47
+ "type": "text",
48
+ "required": true
49
+ }
50
+ ],
51
+ "outputs": [
52
+ {
53
+ "id": "out_0",
54
+ "label": "output",
55
+ "type": "text"
56
+ }
57
+ ],
58
+ "data": {},
59
+ "x": 380,
60
+ "y": 80,
61
+ "width": 280,
62
+ "height": 124
63
+ },
64
+ {
65
+ "id": "op_generate_image",
66
+ "label": "Ideogram 4 Image Generator",
67
+ "role": "operator",
68
+ "kind": "fn",
69
+ "fn": "generate_image",
70
+ "inputs": [
71
+ {
72
+ "id": "in_0",
73
+ "label": "prompt",
74
+ "type": "text",
75
+ "required": true
76
+ }
77
+ ],
78
+ "outputs": [
79
+ {
80
+ "id": "out_0",
81
+ "label": "output",
82
+ "type": "image"
83
+ }
84
+ ],
85
+ "data": {},
86
+ "x": 740,
87
+ "y": 80,
88
+ "width": 280,
89
+ "height": 124
90
+ }
91
+ ],
92
+ "subjects": [
93
+ {
94
+ "id": "subj_output_image",
95
+ "label": "Generated Image",
96
+ "role": "subject",
97
+ "asset_type": "image",
98
+ "inputs": [
99
+ {
100
+ "id": "in",
101
+ "label": "Image",
102
+ "type": "image"
103
+ }
104
+ ],
105
+ "outputs": [
106
+ {
107
+ "id": "out",
108
+ "label": "Image",
109
+ "type": "image"
110
+ }
111
+ ],
112
+ "data": {
113
+ "in": null
114
+ },
115
+ "x": 1100,
116
+ "y": 80,
117
+ "width": 220,
118
+ "height": 107
119
+ }
120
+ ],
121
+ "edges": [
122
+ {
123
+ "id": "edge_1",
124
+ "from_node_id": "ref_concept",
125
+ "from_port_id": "out",
126
+ "to_node_id": "op_generate_prompt",
127
+ "to_port_id": "in_0",
128
+ "type": "text"
129
+ },
130
+ {
131
+ "id": "edge_2",
132
+ "from_node_id": "op_generate_prompt",
133
+ "from_port_id": "out_0",
134
+ "to_node_id": "op_generate_image",
135
+ "to_port_id": "in_0",
136
+ "type": "text"
137
+ },
138
+ {
139
+ "id": "edge_3",
140
+ "from_node_id": "op_generate_image",
141
+ "from_port_id": "out_0",
142
+ "to_node_id": "subj_output_image",
143
+ "to_port_id": "in",
144
+ "type": "image"
145
+ }
146
+ ],
147
+ "view": {
148
+ "default": "canvas"
149
+ }
150
+ }