Spaces:
Sleeping
Sleeping
feat: Add example character loader to UI
Browse files
app.py
CHANGED
|
@@ -25,6 +25,8 @@ if api_key:
|
|
| 25 |
except Exception as e:
|
| 26 |
print(f"Error initializing Gemini: {e}")
|
| 27 |
|
|
|
|
|
|
|
| 28 |
# Load features from YAML
|
| 29 |
def load_features():
|
| 30 |
with open("features.yaml", "r") as f:
|
|
@@ -180,6 +182,25 @@ def load_character(file):
|
|
| 180 |
print(f"Error loading character: {e}")
|
| 181 |
return [gr.update()] * (len(FEATURE_SEQUENCE) + len(SECTIONS))
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
def refine_with_gemini(prompt):
|
| 184 |
if not gemini_active:
|
| 185 |
return "Gemini API key not found in .env file."
|
|
@@ -322,6 +343,10 @@ def build_ui():
|
|
| 322 |
refine_btn = gr.Button("🧠 Refine with Gemini", variant="primary")
|
| 323 |
gen_img_btn = gr.Button("🖼️ Generate Image (Gemini)", variant="primary")
|
| 324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
with gr.Row():
|
| 326 |
with gr.Column(scale=2):
|
| 327 |
prompt_output = gr.Textbox(label="Generated Technical Prompt", lines=5, interactive=False)
|
|
@@ -386,6 +411,22 @@ def build_ui():
|
|
| 386 |
fn=load_character,
|
| 387 |
inputs=load_btn,
|
| 388 |
outputs=dropdowns + extra_texts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
)
|
| 390 |
|
| 391 |
# Initialize
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
print(f"Error initializing Gemini: {e}")
|
| 27 |
|
| 28 |
+
EXAMPLES_DIR = "examples"
|
| 29 |
+
|
| 30 |
# Load features from YAML
|
| 31 |
def load_features():
|
| 32 |
with open("features.yaml", "r") as f:
|
|
|
|
| 182 |
print(f"Error loading character: {e}")
|
| 183 |
return [gr.update()] * (len(FEATURE_SEQUENCE) + len(SECTIONS))
|
| 184 |
|
| 185 |
+
def get_example_list():
|
| 186 |
+
"""Returns a list of JSON filenames in the examples directory."""
|
| 187 |
+
if not os.path.exists(EXAMPLES_DIR):
|
| 188 |
+
return []
|
| 189 |
+
return [f for f in os.listdir(EXAMPLES_DIR) if f.endswith('.json')]
|
| 190 |
+
|
| 191 |
+
def load_example_character(filename):
|
| 192 |
+
"""Loads a character config from the examples directory."""
|
| 193 |
+
if not filename:
|
| 194 |
+
return [gr.update()] * (len(FEATURE_SEQUENCE) + len(SECTIONS))
|
| 195 |
+
|
| 196 |
+
path = os.path.join(EXAMPLES_DIR, filename)
|
| 197 |
+
# Wrap path in a mock file object with a .name attribute for load_character
|
| 198 |
+
class MockFile:
|
| 199 |
+
def __init__(self, name):
|
| 200 |
+
self.name = name
|
| 201 |
+
|
| 202 |
+
return load_character(MockFile(path))
|
| 203 |
+
|
| 204 |
def refine_with_gemini(prompt):
|
| 205 |
if not gemini_active:
|
| 206 |
return "Gemini API key not found in .env file."
|
|
|
|
| 343 |
refine_btn = gr.Button("🧠 Refine with Gemini", variant="primary")
|
| 344 |
gen_img_btn = gr.Button("🖼️ Generate Image (Gemini)", variant="primary")
|
| 345 |
|
| 346 |
+
with gr.Row():
|
| 347 |
+
example_dropdown = gr.Dropdown(choices=get_example_list(), label="Example Characters", scale=3)
|
| 348 |
+
load_example_btn = gr.Button("📂 Load Example", variant="secondary", scale=1)
|
| 349 |
+
|
| 350 |
with gr.Row():
|
| 351 |
with gr.Column(scale=2):
|
| 352 |
prompt_output = gr.Textbox(label="Generated Technical Prompt", lines=5, interactive=False)
|
|
|
|
| 411 |
fn=load_character,
|
| 412 |
inputs=load_btn,
|
| 413 |
outputs=dropdowns + extra_texts
|
| 414 |
+
).then(
|
| 415 |
+
# Trigger prompt update after loading
|
| 416 |
+
fn=generate_prompt,
|
| 417 |
+
inputs=all_input_components,
|
| 418 |
+
outputs=prompt_output
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
load_example_btn.click(
|
| 422 |
+
fn=load_example_character,
|
| 423 |
+
inputs=example_dropdown,
|
| 424 |
+
outputs=dropdowns + extra_texts
|
| 425 |
+
).then(
|
| 426 |
+
# Trigger prompt update after loading
|
| 427 |
+
fn=generate_prompt,
|
| 428 |
+
inputs=all_input_components,
|
| 429 |
+
outputs=prompt_output
|
| 430 |
)
|
| 431 |
|
| 432 |
# Initialize
|