Spaces:
Runtime error
Runtime error
lionelgarnier
commited on
Commit
·
067e31b
1
Parent(s):
beab0ef
add default system prompt for text generation and update processing functions to accept it
Browse files
app.py
CHANGED
@@ -64,14 +64,24 @@ def get_text_gen_pipeline():
|
|
64 |
return None
|
65 |
return _text_gen_pipeline
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
@spaces.GPU()
|
68 |
-
def refine_prompt(prompt, progress=gr.Progress()):
|
69 |
text_gen = get_text_gen_pipeline()
|
70 |
if text_gen is None:
|
71 |
return "", "Text generation model is unavailable."
|
72 |
try:
|
73 |
messages = [
|
74 |
-
{"role": "system", "content":
|
|
|
75 |
]
|
76 |
|
77 |
# Indicate progress started
|
@@ -180,25 +190,20 @@ def preload_models():
|
|
180 |
print(status)
|
181 |
return success
|
182 |
|
183 |
-
# Add a new function to handle example selection
|
184 |
-
def handle_example_click(example_prompt):
|
185 |
-
# Immediately return the example prompt to update the UI
|
186 |
-
return example_prompt, "Example selected - click 'Refine prompt with Mistral' to process"
|
187 |
-
|
188 |
# Create a combined function that handles the whole pipeline from example to image
|
189 |
# This version gets the parameters from the UI components
|
190 |
@spaces.GPU()
|
191 |
-
def process_example_pipeline(example_prompt, progress=gr.Progress()):
|
192 |
# Step 1: Update status
|
193 |
progress(0, desc="Starting example processing")
|
194 |
progress_status = "Selected example: " + example_prompt
|
195 |
|
196 |
# Step 2: Refine the prompt
|
197 |
progress(0.1, desc="Refining prompt with Mistral")
|
198 |
-
refined, status = refine_prompt(example_prompt, progress)
|
199 |
|
200 |
if not refined:
|
201 |
-
return example_prompt, "",
|
202 |
|
203 |
# Return only the refined prompt and status - don't generate image
|
204 |
return example_prompt, refined, "Prompt refined successfully!"
|
@@ -256,6 +261,13 @@ def create_interface():
|
|
256 |
info="Higher values produce more diverse outputs",
|
257 |
)
|
258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
with gr.Tab("Flux"):
|
260 |
# Flux settings
|
261 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
@@ -277,7 +289,7 @@ def create_interface():
|
|
277 |
gr.Examples(
|
278 |
examples=examples, # Now just a list of prompts
|
279 |
fn=process_example_pipeline,
|
280 |
-
inputs=[prompt], #
|
281 |
outputs=[prompt, refined_prompt, error_box], # Don't output image
|
282 |
cache_examples=True,
|
283 |
)
|
@@ -286,7 +298,7 @@ def create_interface():
|
|
286 |
gr.on(
|
287 |
triggers=[prompt_button.click, prompt.submit],
|
288 |
fn=refine_prompt,
|
289 |
-
inputs=[prompt],
|
290 |
outputs=[refined_prompt, error_box]
|
291 |
)
|
292 |
|
|
|
64 |
return None
|
65 |
return _text_gen_pipeline
|
66 |
|
67 |
+
# Default system prompt for text generation
|
68 |
+
DEFAULT_SYSTEM_PROMPT = """Vous êtes un designer produit avec de solides connaissances dans la génération de texte en image. Vous recevrez une demande de produit sous forme de description succincte, et votre mission sera d'imaginer un nouveau design de produit répondant à ce besoin.
|
69 |
+
|
70 |
+
Le livrable (réponse générée) sera exclusivement un texte de prompt pour l'IA de texte to image FLUX.1-schnell.
|
71 |
+
|
72 |
+
Ce prompt devra inclure une description visuelle de l'objet mentionnant explicitement les aspects indispensables de sa fonction.
|
73 |
+
A coté de ça vous devez aussi explicitement mentionner dans ce prompt les caractéristiques esthétiques/photo du rendu image (ex : photoréaliste, haute qualité, focale, grain, etc.), sachant que l'image sera l'image principale de cet objet dans le catalogue produit. Le fond de l'image générée doit être entièrement blanc.
|
74 |
+
Le prompt doit être sans narration, peut être long mais ne doit pas dépasser 77 jetons."""
|
75 |
+
|
76 |
@spaces.GPU()
|
77 |
+
def refine_prompt(prompt, system_prompt=DEFAULT_SYSTEM_PROMPT, progress=gr.Progress()):
|
78 |
text_gen = get_text_gen_pipeline()
|
79 |
if text_gen is None:
|
80 |
return "", "Text generation model is unavailable."
|
81 |
try:
|
82 |
messages = [
|
83 |
+
{"role": "system", "content": system_prompt},
|
84 |
+
{"role": "user", "content": prompt},
|
85 |
]
|
86 |
|
87 |
# Indicate progress started
|
|
|
190 |
print(status)
|
191 |
return success
|
192 |
|
|
|
|
|
|
|
|
|
|
|
193 |
# Create a combined function that handles the whole pipeline from example to image
|
194 |
# This version gets the parameters from the UI components
|
195 |
@spaces.GPU()
|
196 |
+
def process_example_pipeline(example_prompt, system_prompt=DEFAULT_SYSTEM_PROMPT, progress=gr.Progress()):
|
197 |
# Step 1: Update status
|
198 |
progress(0, desc="Starting example processing")
|
199 |
progress_status = "Selected example: " + example_prompt
|
200 |
|
201 |
# Step 2: Refine the prompt
|
202 |
progress(0.1, desc="Refining prompt with Mistral")
|
203 |
+
refined, status = refine_prompt(example_prompt, system_prompt, progress)
|
204 |
|
205 |
if not refined:
|
206 |
+
return example_prompt, "", "Failed to refine prompt: " + status
|
207 |
|
208 |
# Return only the refined prompt and status - don't generate image
|
209 |
return example_prompt, refined, "Prompt refined successfully!"
|
|
|
261 |
info="Higher values produce more diverse outputs",
|
262 |
)
|
263 |
|
264 |
+
system_prompt = gr.Textbox(
|
265 |
+
label="System Prompt",
|
266 |
+
value=DEFAULT_SYSTEM_PROMPT,
|
267 |
+
lines=10,
|
268 |
+
info="Instructions for the Mistral model"
|
269 |
+
)
|
270 |
+
|
271 |
with gr.Tab("Flux"):
|
272 |
# Flux settings
|
273 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
|
|
289 |
gr.Examples(
|
290 |
examples=examples, # Now just a list of prompts
|
291 |
fn=process_example_pipeline,
|
292 |
+
inputs=[prompt, system_prompt], # Add system_prompt as input
|
293 |
outputs=[prompt, refined_prompt, error_box], # Don't output image
|
294 |
cache_examples=True,
|
295 |
)
|
|
|
298 |
gr.on(
|
299 |
triggers=[prompt_button.click, prompt.submit],
|
300 |
fn=refine_prompt,
|
301 |
+
inputs=[prompt, system_prompt], # Add system_prompt as input
|
302 |
outputs=[refined_prompt, error_box]
|
303 |
)
|
304 |
|