Update workflow to use Nemotron Prompt Generator and Ideogram 4 Image Generator
Browse files- app.py +60 -42
- workflow.json +150 -1
app.py
CHANGED
|
@@ -1,56 +1,74 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
result = client.predict(
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
-
demo = gr.Workflow(bind=[
|
| 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 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
}
|