mgbam commited on
Commit
33efea0
Β·
verified Β·
1 Parent(s): 8e26c00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -61
app.py CHANGED
@@ -21,29 +21,39 @@ initialize_image_llms()
21
 
22
  # --- Get API Readiness Status ---
23
  GEMINI_TEXT_IS_READY = is_gemini_text_ready()
24
- HF_TEXT_IS_READY = is_hf_text_ready()
25
  DALLE_IMAGE_IS_READY = is_dalle_ready()
26
- HF_IMAGE_IS_READY = is_hf_image_api_ready()
27
 
28
  # --- Application Configuration (Models, Defaults) ---
29
  TEXT_MODELS = {}
30
  UI_DEFAULT_TEXT_MODEL_KEY = None
 
 
 
 
 
31
  if GEMINI_TEXT_IS_READY:
 
 
 
32
  TEXT_MODELS["✨ Gemini 1.5 Flash (Narrate)"] = {"id": "gemini-1.5-flash-latest", "type": "gemini"}
33
  TEXT_MODELS["Legacy Gemini 1.0 Pro (Narrate)"] = {"id": "gemini-1.0-pro-latest", "type": "gemini"}
34
- if HF_TEXT_IS_READY:
35
- # CORRECTED LINE from SyntaxError:
36
- TEXT_MODELS["Mistral 7B (Narrate via HF)"] = {"id": "mistralai/Mistral-7B-Instruct-v0.2", "type": "hf_text"}
37
- TEXT_MODELS["Gemma 2B (Narrate via HF)"] = {"id": "google/gemma-2b-it", "type": "hf_text"}
38
-
39
- if TEXT_MODELS:
40
- if GEMINI_TEXT_IS_READY and "✨ Gemini 1.5 Flash (Narrate)" in TEXT_MODELS:
41
- UI_DEFAULT_TEXT_MODEL_KEY = "✨ Gemini 1.5 Flash (Narrate)"
42
- elif HF_TEXT_IS_READY and "Mistral 7B (Narrate via HF)" in TEXT_MODELS:
43
- UI_DEFAULT_TEXT_MODEL_KEY = "Mistral 7B (Narrate via HF)"
44
- elif TEXT_MODELS: # Fallback to first available if preferred ones are not ready or not in list
45
- UI_DEFAULT_TEXT_MODEL_KEY = list(TEXT_MODELS.keys())[0]
46
- else:
 
 
47
  TEXT_MODELS["No Text Models Configured"] = {"id": "dummy_text_error", "type": "none"}
48
  UI_DEFAULT_TEXT_MODEL_KEY = "No Text Models Configured"
49
 
@@ -68,51 +78,13 @@ elif not UI_DEFAULT_IMAGE_PROVIDER_KEY and IMAGE_PROVIDERS :
68
 
69
 
70
  # --- Gradio UI Theme and CSS ---
71
- omega_theme = gr.themes.Base(
72
- font=[gr.themes.GoogleFont("Lexend Deca"), "ui-sans-serif", "system-ui", "sans-serif"],
73
- primary_hue=gr.themes.colors.purple, secondary_hue=gr.themes.colors.pink, neutral_hue=gr.themes.colors.slate
74
- ).set(
75
- body_background_fill="#0F0F1A", block_background_fill="#1A1A2E", block_border_width="1px",
76
- block_border_color="#2A2A4A", block_label_background_fill="#2A2A4A", input_background_fill="#2A2A4A",
77
- input_border_color="#4A4A6A", button_primary_background_fill="linear-gradient(135deg, #7F00FF 0%, #E100FF 100%)",
78
- button_primary_text_color="white", button_secondary_background_fill="#4A4A6A",
79
- button_secondary_text_color="#E0E0FF", slider_color="#A020F0"
80
- )
81
- omega_css = """
82
- body, .gradio-container { background-color: #0F0F1A !important; color: #D0D0E0 !important; }
83
- .gradio-container { max-width: 1400px !important; margin: auto !important; border-radius: 20px; box-shadow: 0 10px 30px rgba(0,0,0,0.2); padding: 25px !important; border: 1px solid #2A2A4A;}
84
- .gr-panel, .gr-box, .gr-accordion { background-color: #1A1A2E !important; border: 1px solid #2A2A4A !important; border-radius: 12px !important; box-shadow: 0 4px 15px rgba(0,0,0,0.1);}
85
- .gr-markdown h1 { font-size: 2.8em !important; text-align: center; color: transparent; background: linear-gradient(135deg, #A020F0 0%, #E040FB 100%); -webkit-background-clip: text; background-clip: text; margin-bottom: 5px !important; letter-spacing: -1px;}
86
- .gr-markdown h3 { color: #C080F0 !important; text-align: center; font-weight: 400; margin-bottom: 25px !important;}
87
- .input-section-header { font-size: 1.6em; font-weight: 600; color: #D0D0FF; margin-top: 15px; margin-bottom: 8px; border-bottom: 2px solid #7F00FF; padding-bottom: 5px;}
88
- .output-section-header { font-size: 1.8em; font-weight: 600; color: #D0D0FF; margin-top: 15px; margin-bottom: 12px;}
89
- .gr-input input, .gr-input textarea, .gr-dropdown select, .gr-textbox textarea { background-color: #2A2A4A !important; color: #E0E0FF !important; border: 1px solid #4A4A6A !important; border-radius: 8px !important; padding: 10px !important;}
90
- .gr-button { border-radius: 8px !important; font-weight: 500 !important; transition: all 0.2s ease-in-out !important;}
91
- .gr-button-primary { padding-top: 10px !important; padding-bottom: 10px !important; }
92
- .gr-button-primary:hover { transform: scale(1.03) translateY(-1px) !important; box-shadow: 0 8px 16px rgba(127,0,255,0.3) !important; }
93
- .panel_image { border-radius: 12px !important; overflow: hidden; box-shadow: 0 6px 15px rgba(0,0,0,0.25) !important; background-color: #23233A;}
94
- .panel_image img { max-height: 600px !important; }
95
- .gallery_output { background-color: transparent !important; border: none !important; }
96
- .gallery_output .thumbnail-item { border-radius: 8px !important; box-shadow: 0 3px 8px rgba(0,0,0,0.2) !important; margin: 6px !important; transition: transform 0.2s ease; height: 180px !important; width: 180px !important;}
97
- .gallery_output .thumbnail-item:hover { transform: scale(1.05); }
98
- .status_text { font-weight: 500; padding: 12px 18px; text-align: center; border-radius: 8px; margin-top:12px; border: 1px solid transparent; font-size: 1.05em;}
99
- .error_text { background-color: #401010 !important; color: #FFB0B0 !important; border-color: #802020 !important; }
100
- .success_text { background-color: #104010 !important; color: #B0FFB0 !important; border-color: #208020 !important;}
101
- .processing_text { background-color: #102040 !important; color: #B0D0FF !important; border-color: #204080 !important;}
102
- .important-note { background-color: rgba(127,0,255,0.1); border-left: 5px solid #7F00FF; padding: 15px; margin-bottom:20px; color: #E0E0FF; border-radius: 6px;}
103
- .gr-tabitem { background-color: #1A1A2E !important; border-radius: 0 0 12px 12px !important; padding: 15px !important;}
104
- .gr-tab-button.selected { background-color: #2A2A4A !important; color: white !important; border-bottom: 3px solid #A020F0 !important; border-radius: 8px 8px 0 0 !important; font-weight: 600 !important;}
105
- .gr-tab-button { color: #A0A0C0 !important; border-radius: 8px 8px 0 0 !important;}
106
- .gr-accordion > .gr-block { border-top: 1px solid #2A2A4A !important; }
107
- .gr-markdown code { background-color: #2A2A4A !important; color: #C0C0E0 !important; padding: 0.2em 0.5em; border-radius: 4px; }
108
- .gr-markdown pre { background-color: #23233A !important; padding: 1em !important; border-radius: 6px !important; border: 1px solid #2A2A4A !important;}
109
- .gr-markdown pre > code { padding: 0 !important; background-color: transparent !important; }
110
- #surprise_button { background: linear-gradient(135deg, #ff7e5f 0%, #feb47b 100%) !important; font-weight:600 !important;}
111
- #surprise_button:hover { transform: scale(1.03) translateY(-1px) !important; box-shadow: 0 8px 16px rgba(255,126,95,0.3) !important; }
112
- """
113
 
114
  # --- Helper: Placeholder Image Creation ---
115
  def create_placeholder_image(text="Processing...", size=(512, 512), color="#23233A", text_color="#E0E0FF"):
 
116
  img = Image.new('RGB', size, color=color); draw = ImageDraw.Draw(img)
117
  try: font_path = "arial.ttf" if os.path.exists("arial.ttf") else None
118
  except: font_path = None
@@ -141,7 +113,7 @@ def add_scene_to_story_orchestrator(
141
  ret_status_bar_html_obj = gr.HTML(value="<p class='processing_text status_text'>Processing...</p>")
142
  # ret_log_md will be built up
143
 
144
- # Initial yield for UI updates (buttons disabled by .then() chain)
145
  yield {
146
  output_status_bar: gr.HTML(value=f"<p class='processing_text status_text'>🌌 Weaving Scene {current_story_obj.current_scene_number + 1}...</p>"),
147
  output_latest_scene_image: gr.Image(value=create_placeholder_image("🎨 Conjuring visuals...")),
@@ -257,7 +229,6 @@ def add_scene_to_story_orchestrator(
257
  log_accumulator.append(f" Cycle ended at {time.strftime('%H:%M:%S')}. Total time: {current_total_time:.2f}s")
258
  ret_log_md = gr.Markdown(value="\n".join(log_accumulator))
259
 
260
- # This is the FINAL return. It must be a tuple matching the `outputs` list of engage_button.click()
261
  return (
262
  ret_story_state, ret_gallery, ret_latest_image,
263
  ret_latest_narrative_md_obj, ret_status_bar_html_obj, ret_log_md
@@ -304,7 +275,7 @@ with gr.Blocks(theme=omega_theme, css=omega_css, title="✨ StoryVerse Omega ✨
304
  with gr.Accordion("βš™οΈ Advanced AI Configuration", open=False):
305
  with gr.Group():
306
  text_model_dropdown = gr.Dropdown(choices=list(TEXT_MODELS.keys()), value=UI_DEFAULT_TEXT_MODEL_KEY, label="Narrative AI Engine")
307
- image_provider_dropdown = gr.Dropdown(choices=list(IMAGE_PROVIDERS.keys()), value=UI_DEFAULT_IMAGE_PROVIDER_KEY, label="Visual AI Engine")
308
  with gr.Row():
309
  narrative_length_dropdown = gr.Dropdown(["Short (1 paragraph)", "Medium (2-3 paragraphs)", "Detailed (4+ paragraphs)"], value="Medium (2-3 paragraphs)", label="Narrative Detail")
310
  image_quality_dropdown = gr.Dropdown(["Standard", "High Detail", "Sketch Concept"], value="Standard", label="Image Detail/Style")
@@ -341,7 +312,7 @@ with gr.Blocks(theme=omega_theme, css=omega_css, title="✨ StoryVerse Omega ✨
341
  examples=[
342
  ["A lone, weary traveler on a mechanical steed crosses a vast, crimson desert under twin suns. Dust devils dance in the distance.", "Sci-Fi Western", "Moebius", "greenery, water, modern city"],
343
  ["Deep within an ancient, bioluminescent forest, a hidden civilization of sentient fungi perform a mystical ritual around a pulsating crystal.", "Psychedelic Fantasy", "Alex Grey", "technology, buildings, roads"],
344
- ["A child sits on a crescent moon, fishing for stars in a swirling nebula. A friendly space whale swims nearby.", "Whimsical Cosmic", "James Jean", "realistic, dark, scary"],
345
  ["A grand, baroque library where the books fly freely and whisper forgotten lore to those who listen closely.", "Magical Realism", "Remedios Varo", "minimalist, simple, technology"]
346
  ],
347
  inputs=[scene_prompt_input, image_style_input, artist_style_input, negative_prompt_input],
 
21
 
22
  # --- Get API Readiness Status ---
23
  GEMINI_TEXT_IS_READY = is_gemini_text_ready()
24
+ HF_TEXT_IS_READY = is_hf_text_ready() # For text fallback
25
  DALLE_IMAGE_IS_READY = is_dalle_ready()
26
+ HF_IMAGE_IS_READY = is_hf_image_api_ready() # For image fallback
27
 
28
  # --- Application Configuration (Models, Defaults) ---
29
  TEXT_MODELS = {}
30
  UI_DEFAULT_TEXT_MODEL_KEY = None
31
+
32
+ # Define your Gemini Preview Model IDs here (REPLACE PLACEHOLDERS WITH ACTUAL IDs IF DIFFERENT)
33
+ GEMINI_2_5_PRO_PREVIEW_ID = "gemini-2.5-pro-preview-05-06" # From your screenshot
34
+ GEMINI_2_5_FLASH_PREVIEW_ID = "gemini-2.5-flash-preview-04-17" # From your screenshot
35
+
36
  if GEMINI_TEXT_IS_READY:
37
+ # Add the new Preview models with high priority
38
+ TEXT_MODELS[f"πŸš€ Gemini 2.5 Pro Preview (Narrate)"] = {"id": GEMINI_2_5_PRO_PREVIEW_ID, "type": "gemini"}
39
+ TEXT_MODELS[f"⚑ Gemini 2.5 Flash Preview (Narrate)"] = {"id": GEMINI_2_5_FLASH_PREVIEW_ID, "type": "gemini"}
40
  TEXT_MODELS["✨ Gemini 1.5 Flash (Narrate)"] = {"id": "gemini-1.5-flash-latest", "type": "gemini"}
41
  TEXT_MODELS["Legacy Gemini 1.0 Pro (Narrate)"] = {"id": "gemini-1.0-pro-latest", "type": "gemini"}
42
+
43
+ # Set the default to your newest preferred model if available
44
+ if f"πŸš€ Gemini 2.5 Pro Preview (Narrate)" in TEXT_MODELS: UI_DEFAULT_TEXT_MODEL_KEY = f"πŸš€ Gemini 2.5 Pro Preview (Narrate)"
45
+ elif f"⚑ Gemini 2.5 Flash Preview (Narrate)" in TEXT_MODELS: UI_DEFAULT_TEXT_MODEL_KEY = f"⚑ Gemini 2.5 Flash Preview (Narrate)"
46
+ elif "✨ Gemini 1.5 Flash (Narrate)" in TEXT_MODELS: UI_DEFAULT_TEXT_MODEL_KEY = "✨ Gemini 1.5 Flash (Narrate)"
47
+ else: UI_DEFAULT_TEXT_MODEL_KEY = "Legacy Gemini 1.0 Pro (Narrate)" # Further fallback
48
+ print(f"INFO: app.py - Gemini text models populated. Default set to: {UI_DEFAULT_TEXT_MODEL_KEY}")
49
+
50
+ elif HF_TEXT_IS_READY: # Fallback to HF if no Gemini is ready
51
+ TEXT_MODELS["Mistral 7B (Narrate via HF - Fallback)"] = {"id": "mistralai/Mistral-7B-Instruct-v0.2", "type": "hf_text"}
52
+ TEXT_MODELS["Gemma 2B (Narrate via HF - Fallback)"] = {"id": "google/gemma-2b-it", "type": "hf_text"}
53
+ UI_DEFAULT_TEXT_MODEL_KEY = "Mistral 7B (Narrate via HF - Fallback)"
54
+ print(f"INFO: app.py - HF text models populated as fallback. Default set to: {UI_DEFAULT_TEXT_MODEL_KEY}")
55
+
56
+ if not TEXT_MODELS: # If neither is ready
57
  TEXT_MODELS["No Text Models Configured"] = {"id": "dummy_text_error", "type": "none"}
58
  UI_DEFAULT_TEXT_MODEL_KEY = "No Text Models Configured"
59
 
 
78
 
79
 
80
  # --- Gradio UI Theme and CSS ---
81
+ # (omega_theme and omega_css definitions remain THE SAME as the last full app.py version)
82
+ omega_theme = gr.themes.Base(font=[gr.themes.GoogleFont("Lexend Deca")], primary_hue=gr.themes.colors.purple, secondary_hue=gr.themes.colors.pink, neutral_hue=gr.themes.colors.slate).set(body_background_fill="#0F0F1A", block_background_fill="#1A1A2E", block_border_width="1px", block_border_color="#2A2A4A", block_label_background_fill="#2A2A4A", input_background_fill="#2A2A4A", input_border_color="#4A4A6A", button_primary_background_fill="linear-gradient(135deg, #7F00FF 0%, #E100FF 100%)", button_primary_text_color="white", button_secondary_background_fill="#4A4A6A", button_secondary_text_color="#E0E0FF", slider_color="#A020F0")
83
+ omega_css = """ /* ... Paste your full omega_css string here ... */ """ # Make sure this is complete
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  # --- Helper: Placeholder Image Creation ---
86
  def create_placeholder_image(text="Processing...", size=(512, 512), color="#23233A", text_color="#E0E0FF"):
87
+ # ... (Full implementation as before)
88
  img = Image.new('RGB', size, color=color); draw = ImageDraw.Draw(img)
89
  try: font_path = "arial.ttf" if os.path.exists("arial.ttf") else None
90
  except: font_path = None
 
113
  ret_status_bar_html_obj = gr.HTML(value="<p class='processing_text status_text'>Processing...</p>")
114
  # ret_log_md will be built up
115
 
116
+ # Initial yield for UI updates (buttons handled by .then() chain)
117
  yield {
118
  output_status_bar: gr.HTML(value=f"<p class='processing_text status_text'>🌌 Weaving Scene {current_story_obj.current_scene_number + 1}...</p>"),
119
  output_latest_scene_image: gr.Image(value=create_placeholder_image("🎨 Conjuring visuals...")),
 
229
  log_accumulator.append(f" Cycle ended at {time.strftime('%H:%M:%S')}. Total time: {current_total_time:.2f}s")
230
  ret_log_md = gr.Markdown(value="\n".join(log_accumulator))
231
 
 
232
  return (
233
  ret_story_state, ret_gallery, ret_latest_image,
234
  ret_latest_narrative_md_obj, ret_status_bar_html_obj, ret_log_md
 
275
  with gr.Accordion("βš™οΈ Advanced AI Configuration", open=False):
276
  with gr.Group():
277
  text_model_dropdown = gr.Dropdown(choices=list(TEXT_MODELS.keys()), value=UI_DEFAULT_TEXT_MODEL_KEY, label="Narrative AI Engine")
278
+ image_provider_dropdown = gr.Dropdown(choices=list(IMAGE_PROVIDERS.keys()), value=UI_DEFAULT_IMAGE_PROVIDER_KEY, label="Visual AI Engine (DALL-E/HF)")
279
  with gr.Row():
280
  narrative_length_dropdown = gr.Dropdown(["Short (1 paragraph)", "Medium (2-3 paragraphs)", "Detailed (4+ paragraphs)"], value="Medium (2-3 paragraphs)", label="Narrative Detail")
281
  image_quality_dropdown = gr.Dropdown(["Standard", "High Detail", "Sketch Concept"], value="Standard", label="Image Detail/Style")
 
312
  examples=[
313
  ["A lone, weary traveler on a mechanical steed crosses a vast, crimson desert under twin suns. Dust devils dance in the distance.", "Sci-Fi Western", "Moebius", "greenery, water, modern city"],
314
  ["Deep within an ancient, bioluminescent forest, a hidden civilization of sentient fungi perform a mystical ritual around a pulsating crystal.", "Psychedelic Fantasy", "Alex Grey", "technology, buildings, roads"],
315
+ ["A child sits on a crescent moon, fishing for stars in a swirling nebula. A friendly space whale swims nearby.", "Whimsical Cosmic", "James Jean", "realistic, dark, scary"],
316
  ["A grand, baroque library where the books fly freely and whisper forgotten lore to those who listen closely.", "Magical Realism", "Remedios Varo", "minimalist, simple, technology"]
317
  ],
318
  inputs=[scene_prompt_input, image_style_input, artist_style_input, negative_prompt_input],