Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -138,7 +138,7 @@ def generate_studio(prompt, model_ui, ratio, resolution, grounding, user_api_key
|
|
| 138 |
except Exception as e:
|
| 139 |
raise gr.Error(f"API Error: {str(e)}")
|
| 140 |
|
| 141 |
-
def generate_composition(prompt, files, model_ui, ratio, resolution, user_api_key):
|
| 142 |
"""Composition I2I (Supporte jusqu'à 14 images selon la doc)"""
|
| 143 |
cli = get_client(user_api_key)
|
| 144 |
model_name = MODELS[model_ui]
|
|
@@ -158,6 +158,9 @@ def generate_composition(prompt, files, model_ui, ratio, resolution, user_api_ke
|
|
| 158 |
img_conf["image_size"] = resolution
|
| 159 |
# <--- AJOUT : Activation du Thinking Mode aussi ici par sécurité
|
| 160 |
gen_conf["thinking_config"] = types.ThinkingConfig(include_thoughts=True)
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
gen_conf["image_config"] = types.ImageConfig(**img_conf)
|
| 163 |
|
|
@@ -182,7 +185,7 @@ def generate_composition(prompt, files, model_ui, ratio, resolution, user_api_ke
|
|
| 182 |
|
| 183 |
# --- CHAT LOGIC ---
|
| 184 |
|
| 185 |
-
def chat_respond(message, history, chat_history_data, img_input, model_ui, grounding, user_api_key):
|
| 186 |
"""Gestion du chat 'Stateless' conforme aux types Google GenAI"""
|
| 187 |
|
| 188 |
if not user_api_key: raise gr.Error("API Key manquante")
|
|
@@ -193,7 +196,11 @@ def chat_respond(message, history, chat_history_data, img_input, model_ui, groun
|
|
| 193 |
tools = None
|
| 194 |
thinking_conf = None # <--- AJOUT variable
|
| 195 |
|
|
|
|
|
|
|
|
|
|
| 196 |
if "gemini-3" in model_name:
|
|
|
|
| 197 |
# <--- AJOUT : Config Thinking
|
| 198 |
thinking_conf = types.ThinkingConfig(include_thoughts=True)
|
| 199 |
if grounding:
|
|
@@ -205,7 +212,8 @@ def chat_respond(message, history, chat_history_data, img_input, model_ui, groun
|
|
| 205 |
config=types.GenerateContentConfig(
|
| 206 |
response_modalities=['TEXT', 'IMAGE'],
|
| 207 |
tools=tools,
|
| 208 |
-
thinking_config=thinking_conf
|
|
|
|
| 209 |
),
|
| 210 |
history=chat_history_data
|
| 211 |
)
|
|
@@ -269,12 +277,12 @@ def chat_respond(message, history, chat_history_data, img_input, model_ui, groun
|
|
| 269 |
|
| 270 |
new_ui_history = history + [user_message_obj] + bot_messages
|
| 271 |
|
| 272 |
-
return "", new_ui_history, new_gemini_history, f_imgs
|
| 273 |
|
| 274 |
except Exception as e:
|
| 275 |
err_msg = f"❌ Error: {str(e)}"
|
| 276 |
bot_err_obj = {"role": "assistant", "content": err_msg}
|
| 277 |
-
return "", history + [user_message_obj, bot_err_obj], chat_history_data, []
|
| 278 |
|
| 279 |
def clear_chat():
|
| 280 |
return [], None, []
|
|
@@ -328,7 +336,7 @@ with gr.Blocks(title="Nano Vision Studio") as demo:
|
|
| 328 |
t1_text = gr.Markdown(label="Generated Text")
|
| 329 |
with gr.Accordion("🧠 Thought Process", open=False):
|
| 330 |
t1_thought_imgs = gr.Gallery(label="Visual Drafts", columns=4, height=150)
|
| 331 |
-
#
|
| 332 |
t1_thought_txt = gr.Markdown(label="Thought Stream")
|
| 333 |
|
| 334 |
t1_btn.click(
|
|
@@ -348,6 +356,7 @@ with gr.Blocks(title="Nano Vision Studio") as demo:
|
|
| 348 |
with gr.Row():
|
| 349 |
t2_ratio = gr.Dropdown(RATIOS, value="1:1", label="Aspect Ratio")
|
| 350 |
t2_res = gr.Dropdown(RESOLUTIONS, value="1K", label="Output Resolution")
|
|
|
|
| 351 |
t2_btn = gr.Button("Run", variant="primary")
|
| 352 |
|
| 353 |
with gr.Column(scale=2):
|
|
@@ -356,7 +365,7 @@ with gr.Blocks(title="Nano Vision Studio") as demo:
|
|
| 356 |
|
| 357 |
t2_btn.click(
|
| 358 |
generate_composition,
|
| 359 |
-
inputs=[t2_prompt, t2_files, t2_model, t2_ratio, t2_res, user_api_key_state],
|
| 360 |
outputs=[t2_gallery, t2_text]
|
| 361 |
)
|
| 362 |
|
|
@@ -374,6 +383,9 @@ with gr.Blocks(title="Nano Vision Studio") as demo:
|
|
| 374 |
clear_btn = gr.Button("🗑️ New Session")
|
| 375 |
with gr.Accordion("Chat Options", open=False):
|
| 376 |
c_model = gr.Dropdown(list(MODELS.keys()), value="🧠 Gemini 3 Pro Preview (Recommended)", label="Model")
|
|
|
|
|
|
|
|
|
|
| 377 |
c_grounding = gr.Checkbox(label="Grounding")
|
| 378 |
|
| 379 |
with gr.Column(scale=1):
|
|
@@ -381,8 +393,8 @@ with gr.Blocks(title="Nano Vision Studio") as demo:
|
|
| 381 |
|
| 382 |
chat_btn.click(
|
| 383 |
chat_respond,
|
| 384 |
-
inputs=[chat_input, chat_history, chat_state, chat_img, c_model, c_grounding, user_api_key_state],
|
| 385 |
-
outputs=[chat_input, chat_history, chat_state, chat_gallery_zoom]
|
| 386 |
)
|
| 387 |
clear_btn.click(
|
| 388 |
clear_chat,
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
raise gr.Error(f"API Error: {str(e)}")
|
| 140 |
|
| 141 |
+
def generate_composition(prompt, files, model_ui, ratio, resolution, grounding, user_api_key):
|
| 142 |
"""Composition I2I (Supporte jusqu'à 14 images selon la doc)"""
|
| 143 |
cli = get_client(user_api_key)
|
| 144 |
model_name = MODELS[model_ui]
|
|
|
|
| 158 |
img_conf["image_size"] = resolution
|
| 159 |
# <--- AJOUT : Activation du Thinking Mode aussi ici par sécurité
|
| 160 |
gen_conf["thinking_config"] = types.ThinkingConfig(include_thoughts=True)
|
| 161 |
+
# <--- AJOUT : Grounding
|
| 162 |
+
if grounding:
|
| 163 |
+
gen_conf["tools"] = [{"google_search": {}}]
|
| 164 |
|
| 165 |
gen_conf["image_config"] = types.ImageConfig(**img_conf)
|
| 166 |
|
|
|
|
| 185 |
|
| 186 |
# --- CHAT LOGIC ---
|
| 187 |
|
| 188 |
+
def chat_respond(message, history, chat_history_data, img_input, model_ui, grounding, ratio, resolution, user_api_key):
|
| 189 |
"""Gestion du chat 'Stateless' conforme aux types Google GenAI"""
|
| 190 |
|
| 191 |
if not user_api_key: raise gr.Error("API Key manquante")
|
|
|
|
| 196 |
tools = None
|
| 197 |
thinking_conf = None # <--- AJOUT variable
|
| 198 |
|
| 199 |
+
# Configuration Image
|
| 200 |
+
img_conf = {"aspect_ratio": ratio}
|
| 201 |
+
|
| 202 |
if "gemini-3" in model_name:
|
| 203 |
+
img_conf["image_size"] = resolution
|
| 204 |
# <--- AJOUT : Config Thinking
|
| 205 |
thinking_conf = types.ThinkingConfig(include_thoughts=True)
|
| 206 |
if grounding:
|
|
|
|
| 212 |
config=types.GenerateContentConfig(
|
| 213 |
response_modalities=['TEXT', 'IMAGE'],
|
| 214 |
tools=tools,
|
| 215 |
+
thinking_config=thinking_conf,
|
| 216 |
+
image_config=types.ImageConfig(**img_conf) # <--- AJOUT Image Config dans Chat
|
| 217 |
),
|
| 218 |
history=chat_history_data
|
| 219 |
)
|
|
|
|
| 277 |
|
| 278 |
new_ui_history = history + [user_message_obj] + bot_messages
|
| 279 |
|
| 280 |
+
return "", None, new_ui_history, new_gemini_history, f_imgs
|
| 281 |
|
| 282 |
except Exception as e:
|
| 283 |
err_msg = f"❌ Error: {str(e)}"
|
| 284 |
bot_err_obj = {"role": "assistant", "content": err_msg}
|
| 285 |
+
return "", None, history + [user_message_obj, bot_err_obj], chat_history_data, []
|
| 286 |
|
| 287 |
def clear_chat():
|
| 288 |
return [], None, []
|
|
|
|
| 336 |
t1_text = gr.Markdown(label="Generated Text")
|
| 337 |
with gr.Accordion("🧠 Thought Process", open=False):
|
| 338 |
t1_thought_imgs = gr.Gallery(label="Visual Drafts", columns=4, height=150)
|
| 339 |
+
# Utilisation de Markdown pour un meilleur rendu du flux de pensée
|
| 340 |
t1_thought_txt = gr.Markdown(label="Thought Stream")
|
| 341 |
|
| 342 |
t1_btn.click(
|
|
|
|
| 356 |
with gr.Row():
|
| 357 |
t2_ratio = gr.Dropdown(RATIOS, value="1:1", label="Aspect Ratio")
|
| 358 |
t2_res = gr.Dropdown(RESOLUTIONS, value="1K", label="Output Resolution")
|
| 359 |
+
t2_grounding = gr.Checkbox(label="Google Search (Grounding)") # <--- AJOUT Grounding
|
| 360 |
t2_btn = gr.Button("Run", variant="primary")
|
| 361 |
|
| 362 |
with gr.Column(scale=2):
|
|
|
|
| 365 |
|
| 366 |
t2_btn.click(
|
| 367 |
generate_composition,
|
| 368 |
+
inputs=[t2_prompt, t2_files, t2_model, t2_ratio, t2_res, t2_grounding, user_api_key_state],
|
| 369 |
outputs=[t2_gallery, t2_text]
|
| 370 |
)
|
| 371 |
|
|
|
|
| 383 |
clear_btn = gr.Button("🗑️ New Session")
|
| 384 |
with gr.Accordion("Chat Options", open=False):
|
| 385 |
c_model = gr.Dropdown(list(MODELS.keys()), value="🧠 Gemini 3 Pro Preview (Recommended)", label="Model")
|
| 386 |
+
with gr.Row():
|
| 387 |
+
c_ratio = gr.Dropdown(RATIOS, value="16:9", label="Aspect Ratio") # <--- AJOUT Ratio
|
| 388 |
+
c_res = gr.Dropdown(RESOLUTIONS, value="2K", label="Resolution (Pro only)") # <--- AJOUT Resolution
|
| 389 |
c_grounding = gr.Checkbox(label="Grounding")
|
| 390 |
|
| 391 |
with gr.Column(scale=1):
|
|
|
|
| 393 |
|
| 394 |
chat_btn.click(
|
| 395 |
chat_respond,
|
| 396 |
+
inputs=[chat_input, chat_history, chat_state, chat_img, c_model, c_grounding, c_ratio, c_res, user_api_key_state],
|
| 397 |
+
outputs=[chat_input, chat_img, chat_history, chat_state, chat_gallery_zoom]
|
| 398 |
)
|
| 399 |
clear_btn.click(
|
| 400 |
clear_chat,
|