Switch image generation from Ideogram to FLUX.1-schnell
Browse files- app.py +13 -20
- workflow.json +2 -2
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import gradio as gr
|
| 4 |
-
from gradio_client import Client
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
|
|
@@ -16,7 +15,7 @@ def generate_prompt(concept: str) -> str:
|
|
| 16 |
system_instruction = (
|
| 17 |
"You are an expert prompt engineer for text-to-image models. "
|
| 18 |
"Your task is to take a simple concept and expand it into a detailed, "
|
| 19 |
-
"vivid, and high-quality image prompt for
|
| 20 |
"Describe the scene, lighting, materials, and aesthetic in detail. "
|
| 21 |
"Provide ONLY the final prompt text. Do not include any introductory or concluding text, "
|
| 22 |
"do not provide multiple options, and do not wrap the prompt in quotes."
|
|
@@ -44,35 +43,29 @@ def generate_prompt(concept: str) -> str:
|
|
| 44 |
|
| 45 |
def generate_image(prompt: str) -> dict:
|
| 46 |
"""
|
| 47 |
-
Generates an image from a prompt using the
|
| 48 |
Returns a dictionary structure compatible with Gradio's image viewer.
|
| 49 |
"""
|
| 50 |
if not prompt:
|
| 51 |
prompt = "a ginger cat wearing a tiny wizard hat reading a spellbook"
|
| 52 |
try:
|
| 53 |
-
client =
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
)
|
| 64 |
-
if isinstance(result, (list, tuple)) and len(result) > 0:
|
| 65 |
-
filepath = result[0]
|
| 66 |
-
else:
|
| 67 |
-
filepath = result
|
| 68 |
-
|
| 69 |
return {
|
| 70 |
"path": filepath,
|
| 71 |
"url": f"/gradio_api/file={filepath}",
|
| 72 |
"is_file": True
|
| 73 |
}
|
| 74 |
except Exception as e:
|
| 75 |
-
print(f"Error calling
|
| 76 |
raise e
|
| 77 |
|
| 78 |
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
|
|
|
|
| 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 FLUX.1-schnell. "
|
| 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."
|
|
|
|
| 43 |
|
| 44 |
def generate_image(prompt: str) -> dict:
|
| 45 |
"""
|
| 46 |
+
Generates an image from a prompt using the FLUX.1-schnell model.
|
| 47 |
Returns a dictionary structure compatible with Gradio's image viewer.
|
| 48 |
"""
|
| 49 |
if not prompt:
|
| 50 |
prompt = "a ginger cat wearing a tiny wizard hat reading a spellbook"
|
| 51 |
try:
|
| 52 |
+
client = InferenceClient("black-forest-labs/FLUX.1-schnell")
|
| 53 |
+
image = client.text_to_image(prompt)
|
| 54 |
+
|
| 55 |
+
import tempfile
|
| 56 |
+
import uuid
|
| 57 |
+
|
| 58 |
+
temp_dir = tempfile.gettempdir()
|
| 59 |
+
filepath = os.path.join(temp_dir, f"{uuid.uuid4()}.png")
|
| 60 |
+
image.save(filepath)
|
| 61 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return {
|
| 63 |
"path": filepath,
|
| 64 |
"url": f"/gradio_api/file={filepath}",
|
| 65 |
"is_file": True
|
| 66 |
}
|
| 67 |
except Exception as e:
|
| 68 |
+
print(f"Error calling FLUX.1-schnell model: {e}")
|
| 69 |
raise e
|
| 70 |
|
| 71 |
|
workflow.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
{
|
| 2 |
"schema_version": "2",
|
| 3 |
-
"name": "
|
| 4 |
"runtime": {
|
| 5 |
"default": "client"
|
| 6 |
},
|
|
@@ -90,7 +90,7 @@
|
|
| 90 |
},
|
| 91 |
{
|
| 92 |
"id": "op_generate_image",
|
| 93 |
-
"label": "
|
| 94 |
"role": "operator",
|
| 95 |
"kind": "fn",
|
| 96 |
"fn": "generate_image",
|
|
|
|
| 1 |
{
|
| 2 |
"schema_version": "2",
|
| 3 |
+
"name": "FLUX.1-schnell Workflow",
|
| 4 |
"runtime": {
|
| 5 |
"default": "client"
|
| 6 |
},
|
|
|
|
| 90 |
},
|
| 91 |
{
|
| 92 |
"id": "op_generate_image",
|
| 93 |
+
"label": "FLUX.1-schnell Image Generator",
|
| 94 |
"role": "operator",
|
| 95 |
"kind": "fn",
|
| 96 |
"fn": "generate_image",
|