Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,66 +2,212 @@
|
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
import time
|
5 |
-
|
|
|
|
|
|
|
6 |
|
7 |
# --- Core Logic Imports ---
|
8 |
from core.llm_services import initialize_text_llms, is_gemini_text_ready, is_hf_text_ready, generate_text_gemini, generate_text_hf
|
9 |
-
# MODIFIED
|
10 |
from core.image_services import initialize_image_llms, is_dalle_ready, is_hf_image_api_ready, generate_image_dalle, generate_image_hf_model, ImageGenResponse
|
11 |
from core.story_engine import Story, Scene
|
12 |
-
# ... (other core imports: prompts, utils)
|
13 |
from prompts.narrative_prompts import get_narrative_system_prompt, format_narrative_user_prompt
|
14 |
from prompts.image_style_prompts import STYLE_PRESETS, COMMON_NEGATIVE_PROMPTS, format_image_generation_prompt
|
15 |
from core.utils import basic_text_cleanup
|
16 |
|
17 |
-
|
18 |
# --- Initialize Services ---
|
19 |
initialize_text_llms()
|
20 |
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() # For
|
27 |
|
28 |
# --- Application Configuration (Models, Defaults) ---
|
29 |
TEXT_MODELS = {}
|
30 |
UI_DEFAULT_TEXT_MODEL_KEY = None
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
TEXT_MODELS["No Text Models Configured"] = {"id": "dummy_text_error", "type": "none"}
|
42 |
UI_DEFAULT_TEXT_MODEL_KEY = "No Text Models Configured"
|
43 |
|
44 |
|
45 |
-
IMAGE_PROVIDERS = {}
|
46 |
UI_DEFAULT_IMAGE_PROVIDER_KEY = None
|
47 |
-
|
48 |
-
|
49 |
-
IMAGE_PROVIDERS["πΌοΈ OpenAI DALL-E
|
|
|
50 |
UI_DEFAULT_IMAGE_PROVIDER_KEY = "πΌοΈ OpenAI DALL-E 3"
|
51 |
-
elif HF_IMAGE_IS_READY: # Fallback to HF for images
|
52 |
-
IMAGE_PROVIDERS["π‘ HF - Stable Diffusion XL Base (Fallback)"] = "hf_sdxl_base"
|
53 |
-
IMAGE_PROVIDERS["π HF - OpenJourney (Fallback)"] = "hf_openjourney"
|
54 |
-
UI_DEFAULT_IMAGE_PROVIDER_KEY = "π‘ HF - Stable Diffusion XL Base (Fallback)"
|
55 |
|
56 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
IMAGE_PROVIDERS["No Image Providers Configured"] = "none"
|
58 |
UI_DEFAULT_IMAGE_PROVIDER_KEY = "No Image Providers Configured"
|
59 |
|
60 |
|
61 |
-
#
|
|
|
62 |
omega_theme = gr.themes.Base(font=[gr.themes.GoogleFont("Lexend Deca")], primary_hue=gr.themes.colors.purple).set(body_background_fill="#0F0F1A", block_background_fill="#1A1A2E", slider_color="#A020F0")
|
63 |
-
omega_css = "
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
img = Image.new('RGB', size, color=color); draw = ImageDraw.Draw(img); #... (full implementation)
|
66 |
try: font_path = "arial.ttf" if os.path.exists("arial.ttf") else None
|
67 |
except: font_path = None
|
@@ -71,43 +217,75 @@ def create_placeholder_image(text="Processing...", size=(512, 512), color="#2323
|
|
71 |
else: tw, th = draw.textsize(text, font=font)
|
72 |
draw.text(((size[0]-tw)/2, (size[1]-th)/2), text, font=font, fill=text_color); return img
|
73 |
|
74 |
-
|
75 |
# --- StoryVerse Weaver Orchestrator (MODIFIED image generation part) ---
|
76 |
def add_scene_to_story_orchestrator(
|
77 |
current_story_obj: Story, scene_prompt_text: str, image_style_dropdown: str, artist_style_text: str,
|
78 |
-
negative_prompt_text: str, text_model_key: str, image_provider_key: str,
|
79 |
narrative_length: str, image_quality: str,
|
80 |
progress=gr.Progress(track_tqdm=True)
|
81 |
):
|
82 |
start_time = time.time()
|
83 |
if not current_story_obj: current_story_obj = Story()
|
84 |
-
log_accumulator = [f"**π Scene {current_story_obj.current_scene_number + 1} - {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
# ... (Initialize ret_... placeholders as before) ...
|
86 |
ret_story_state, ret_gallery, ret_latest_image, ret_latest_narrative_md_obj, ret_status_bar_html_obj, ret_log_md = \
|
87 |
current_story_obj, current_story_obj.get_all_scenes_for_gallery_display(), None, gr.Markdown("Processing..."), gr.HTML("<p>Processing...</p>"), gr.Markdown("\n".join(log_accumulator))
|
88 |
|
89 |
-
# Initial yield
|
90 |
yield {
|
91 |
output_status_bar: gr.HTML(value=f"<p class='processing_text status_text'>π Weaving Scene {current_story_obj.current_scene_number + 1}...</p>"),
|
92 |
-
# ... (other initial yields)
|
93 |
output_latest_scene_image: gr.Image(value=create_placeholder_image("π¨ Conjuring visuals...")),
|
94 |
output_latest_scene_narrative: gr.Markdown(value=" Musing narrative..."),
|
95 |
output_interaction_log_markdown: gr.Markdown(value="\n".join(log_accumulator))
|
96 |
}
|
97 |
-
# Note: Button disabling/enabling is handled by the .then() chain in UI definition
|
98 |
|
99 |
try:
|
100 |
if not scene_prompt_text.strip(): raise ValueError("Scene prompt cannot be empty!")
|
101 |
|
102 |
-
# --- 1. Generate Narrative Text (
|
103 |
progress(0.1, desc="βοΈ Crafting narrative...")
|
104 |
-
# ... (Full narrative generation logic from previous app.py
|
105 |
-
# ... (This part should be copied from your last working version) ...
|
106 |
narrative_text_generated = "Simulated Narrative." # Placeholder
|
107 |
text_model_info = TEXT_MODELS.get(text_model_key)
|
108 |
if text_model_info and text_model_info["type"] != "none":
|
109 |
system_p = get_narrative_system_prompt("default"); prev_narrative = current_story_obj.get_last_scene_narrative(); user_p = format_narrative_user_prompt(scene_prompt_text, prev_narrative)
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
text_response = None
|
112 |
if text_model_info["type"] == "gemini": text_response = generate_text_gemini(user_p, model_id=text_model_info["id"], system_prompt=system_p, max_tokens=768 if narrative_length.startswith("Detailed") else 400)
|
113 |
elif text_model_info["type"] == "hf_text": text_response = generate_text_hf(user_p, model_id=text_model_info["id"], system_prompt=system_p, max_tokens=768 if narrative_length.startswith("Detailed") else 400)
|
@@ -120,44 +298,99 @@ def add_scene_to_story_orchestrator(
|
|
120 |
yield { output_latest_scene_narrative: ret_latest_narrative_md_obj, output_interaction_log_markdown: gr.Markdown(value="\n".join(log_accumulator)) }
|
121 |
|
122 |
|
123 |
-
# --- 2. Generate Image (NOW USING DALL-E
|
124 |
progress(0.5, desc="π¨ Conjuring visuals...")
|
125 |
image_generated_pil = None
|
126 |
image_generation_error_message = None
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
image_content_prompt_for_gen = narrative_text_generated if narrative_text_generated and "Error" not in narrative_text_generated else scene_prompt_text
|
130 |
-
quality_keyword = "detailed, high quality, " if image_quality == "High Detail" else ""
|
131 |
full_image_prompt = format_image_generation_prompt(quality_keyword + image_content_prompt_for_gen[:350], image_style_dropdown, artist_style_text)
|
132 |
-
log_accumulator.append(f" Image: Attempting with provider key '{image_provider_key}' (maps to type '{
|
133 |
|
134 |
-
if
|
135 |
image_response = None
|
136 |
-
if
|
137 |
if DALLE_IMAGE_IS_READY:
|
138 |
image_response = generate_image_dalle(full_image_prompt, model="dall-e-3", quality="hd" if image_quality=="High Detail" else "standard")
|
139 |
-
else: image_generation_error_message = "**Image Error:** DALL-E 3 selected but API not ready."
|
140 |
-
elif
|
141 |
if DALLE_IMAGE_IS_READY:
|
142 |
-
image_response = generate_image_dalle(full_image_prompt, model="dall-e-2", size="1024x1024")
|
143 |
else: image_generation_error_message = "**Image Error:** DALL-E 2 selected but API not ready."
|
144 |
-
# Fallback to HF models
|
145 |
-
elif
|
146 |
if HF_IMAGE_IS_READY:
|
147 |
hf_model_id_to_call = "stabilityai/stable-diffusion-xl-base-1.0" # Default HF
|
148 |
img_width, img_height = 768, 768
|
149 |
-
if
|
150 |
-
elif
|
151 |
image_response = generate_image_hf_model(full_image_prompt, model_id=hf_model_id_to_call, negative_prompt=negative_prompt_text or COMMON_NEGATIVE_PROMPTS, width=img_width, height=img_height)
|
152 |
-
else: image_generation_error_message = "**Image Error:** HF Image Model selected but API not ready
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
if image_response and image_response.success: image_generated_pil = image_response.image; log_accumulator.append(f" Image: Success from {image_response.provider} (Model: {image_response.model_id_used}).")
|
156 |
elif image_response: image_generation_error_message = f"**Image Error ({image_response.provider} - {image_response.model_id_used}):** {image_response.error}"; log_accumulator.append(f" Image: FAILED - {image_response.error}")
|
157 |
elif not image_generation_error_message: image_generation_error_message = f"**Image Error:** No response/unknown issue with {image_provider_key}."; log_accumulator.append(f" Image: FAILED - No response object.")
|
158 |
|
159 |
-
if not image_generated_pil and not image_generation_error_message:
|
160 |
-
image_generation_error_message = "**Image Error:** No valid image provider configured or selected."
|
161 |
log_accumulator.append(f" Image: FAILED - {image_generation_error_message}")
|
162 |
|
163 |
ret_latest_image = image_generated_pil if image_generated_pil else create_placeholder_image("Image Gen Failed", color="#401010")
|
@@ -176,22 +409,11 @@ def add_scene_to_story_orchestrator(
|
|
176 |
ret_status_bar_html_obj = gr.HTML(value=status_html_str_temp)
|
177 |
progress(1.0, desc="Scene Complete!")
|
178 |
|
179 |
-
|
180 |
except ValueError as ve: # ... (Error handling as before) ...
|
181 |
log_accumulator.append(f"\n**INPUT ERROR:** {ve}"); ret_status_bar_html_obj = gr.HTML(f"<p class='error_text'>ERROR: {ve}</p>"); ret_latest_narrative_md_obj = gr.Markdown(f"## Error\n{ve}")
|
182 |
except Exception as e: # ... (Error handling as before) ...
|
183 |
log_accumulator.append(f"\n**RUNTIME ERROR:** {e}\n{traceback.format_exc()}"); ret_status_bar_html_obj = gr.HTML(f"<p class='error_text'>UNEXPECTED ERROR: {e}</p>"); ret_latest_narrative_md_obj = gr.Markdown(f"## Unexpected Error\n{e}")
|
184 |
-
|
185 |
-
current_total_time = time.time() - start_time
|
186 |
-
log_accumulator.append(f" Cycle ended. Total time: {current_total_time:.2f}s")
|
187 |
-
ret_log_md = gr.Markdown(value="\n".join(log_accumulator))
|
188 |
-
|
189 |
-
return (ret_story_state, ret_gallery, ret_latest_image, ret_latest_narrative_md_obj, ret_status_bar_html_obj, ret_log_md)
|
190 |
-
|
191 |
-
# --- clear_story_state_ui_wrapper, surprise_me_func, disable_buttons_for_processing, enable_buttons_after_processing ---
|
192 |
-
# (These functions remain IDENTICAL to the ones in the last full app.py that fixed the ValueError)
|
193 |
-
def clear_story_state_ui_wrapper(): new_story=Story(); ph_img=create_placeholder_image("Blank..."); return(new_story,[(ph_img,"New...")],None,gr.Markdown("## Cleared"),gr.HTML("<p>Cleared.</p>"),"Log Cleared","")
|
194 |
-
def surprise_me_func(): themes = ["Cosmic Horror", "Solarpunk"]; actions = ["unearths artifact", "negotiates"]; settings = ["on rogue planet", "in tree city"]; prompt = f"Protagonist {random.choice(actions)} {random.choice(settings)}. Theme: {random.choice(themes)}."; style = random.choice(list(STYLE_PRESETS.keys())); artist = random.choice(["Giger", "Moebius", ""]*2); return prompt, style, artist
|
195 |
def disable_buttons_for_processing(): return gr.Button(interactive=False), gr.Button(interactive=False)
|
196 |
def enable_buttons_after_processing(): return gr.Button(interactive=True), gr.Button(interactive=True)
|
197 |
|
@@ -199,9 +421,9 @@ def enable_buttons_after_processing(): return gr.Button(interactive=True), gr.Bu
|
|
199 |
# --- Gradio UI Definition ---
|
200 |
with gr.Blocks(theme=omega_theme, css=omega_css, title="β¨ StoryVerse Omega β¨") as story_weaver_demo:
|
201 |
story_state_output = gr.State(Story())
|
202 |
-
# ... (Full UI layout from the
|
203 |
# ... (This includes defining all component variables like scene_prompt_input, output_gallery, engage_button etc. IN THE LAYOUT)
|
204 |
-
#
|
205 |
gr.Markdown("<div align='center'><h1>β¨ StoryVerse Omega β¨</h1>\n<h3>Craft Immersive Multimodal Worlds with AI</h3></div>")
|
206 |
gr.HTML("<div class='important-note'><strong>Welcome, Worldsmith!</strong> ... API keys (<code>STORYVERSE_...</code>) ...</div>")
|
207 |
with gr.Accordion("π§ AI Services Status & Info", open=False):
|
@@ -225,12 +447,66 @@ with gr.Blocks(theme=omega_theme, css=omega_css, title="β¨ StoryVerse Omega β¨
|
|
225 |
with gr.Accordion("βοΈ Advanced AI Configuration", open=False):
|
226 |
with gr.Group():
|
227 |
text_model_dropdown = gr.Dropdown(choices=list(TEXT_MODELS.keys()), value=UI_DEFAULT_TEXT_MODEL_KEY, label="Narrative AI Engine")
|
228 |
-
image_provider_dropdown = gr.Dropdown(choices=list(IMAGE_PROVIDERS.keys()), value=UI_DEFAULT_IMAGE_PROVIDER_KEY, label="Visual AI Engine") #
|
229 |
with gr.Row():
|
230 |
narrative_length_dropdown = gr.Dropdown(["Short", "Medium", "Detailed"], value="Medium", label="Narrative Detail")
|
231 |
image_quality_dropdown = gr.Dropdown(["Standard", "High Detail", "Sketch"], value="Standard", label="Image Detail")
|
232 |
with gr.Row(elem_classes=["compact-row"], equal_height=True):
|
233 |
-
engage_button = gr.Button("π Weave Scene!", variant="primary", scale=3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
surprise_button = gr.Button("π² Surprise!", variant="secondary", scale=1, icon="π")
|
235 |
clear_story_button = gr.Button("ποΈ New", variant="stop", scale=1, icon="β»οΈ")
|
236 |
output_status_bar = gr.HTML(value="<p class='processing_text status_text'>Ready to weave!</p>")
|
@@ -255,7 +531,7 @@ with gr.Blocks(theme=omega_theme, css=omega_css, title="β¨ StoryVerse Omega β¨
|
|
255 |
if __name__ == "__main__":
|
256 |
print("="*80); print("β¨ StoryVerse Omega (DALL-E/Gemini Focus) Launching... β¨")
|
257 |
print(f" Gemini Text Ready: {GEMINI_TEXT_IS_READY}"); print(f" HF Text Ready: {HF_TEXT_IS_READY}")
|
258 |
-
print(f" DALL-E Image Ready: {DALLE_IMAGE_IS_READY}"); print(f" HF Image Ready: {HF_IMAGE_IS_READY}")
|
259 |
if not (GEMINI_TEXT_IS_READY or HF_TEXT_IS_READY) or not (DALLE_IMAGE_IS_READY or HF_IMAGE_IS_READY): print(" π΄ WARNING: Not all services configured.")
|
260 |
print(f" Default Text Model: {UI_DEFAULT_TEXT_MODEL_KEY}"); print(f" Default Image Provider: {UI_DEFAULT_IMAGE_PROVIDER_KEY}")
|
261 |
print("="*80)
|
|
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
import time
|
5 |
+
import json
|
6 |
+
from PIL import Image, ImageDraw, ImageFont
|
7 |
+
import random
|
8 |
+
import traceback
|
9 |
|
10 |
# --- Core Logic Imports ---
|
11 |
from core.llm_services import initialize_text_llms, is_gemini_text_ready, is_hf_text_ready, generate_text_gemini, generate_text_hf
|
12 |
+
# MODIFIED Import for image_services to reflect DALL-E priority
|
13 |
from core.image_services import initialize_image_llms, is_dalle_ready, is_hf_image_api_ready, generate_image_dalle, generate_image_hf_model, ImageGenResponse
|
14 |
from core.story_engine import Story, Scene
|
|
|
15 |
from prompts.narrative_prompts import get_narrative_system_prompt, format_narrative_user_prompt
|
16 |
from prompts.image_style_prompts import STYLE_PRESETS, COMMON_NEGATIVE_PROMPTS, format_image_generation_prompt
|
17 |
from core.utils import basic_text_cleanup
|
18 |
|
|
|
19 |
# --- Initialize Services ---
|
20 |
initialize_text_llms()
|
21 |
initialize_image_llms()
|
22 |
|
23 |
# --- Get API Readiness Status ---
|
24 |
GEMINI_TEXT_IS_READY = is_gemini_text_ready()
|
25 |
+
HF_TEXT_IS_READY = is_hf_text_ready()
|
26 |
+
DALLE_IMAGE_IS_READY = is_dalle_ready() # For DALL-E
|
27 |
+
HF_IMAGE_IS_READY = is_hf_image_api_ready() # For HF Image fallback
|
28 |
|
29 |
# --- Application Configuration (Models, Defaults) ---
|
30 |
TEXT_MODELS = {}
|
31 |
UI_DEFAULT_TEXT_MODEL_KEY = None
|
32 |
+
# ... (TEXT_MODELS and UI_DEFAULT_TEXT_MODEL_KEY population as before,Great prioritizing Gemini then HF) ...
|
33 |
+
if GEMINI_TEXT_IS_READY:
|
34 |
+
TEXT_MODELS["β¨ Gemini! Now that `core/image_services.py` is updated to prioritize D 1.5 Flash (Narrate)"] = {"id": "gemini-1.5-flash-latest", "type": "gemini"}
|
35 |
+
TEXT_MODELS["Legacy Gemini 1.0 Pro (ALL-E and use Hugging Face models as a fallback, we need to ensure `app.py` correctly reflects these changes inNarrate)"] = {"id": "gemini-1.0-pro-latest", "type": "gemini"}
|
36 |
+
if HF_TEXT_IS_READY:
|
37 |
+
TEXT_MODELS["Mistral 7 its configuration and logic.
|
38 |
+
|
39 |
+
Here's the **full `app.py`** rewritten to align with the updated `image_services.py`.
|
40 |
+
|
41 |
+
**Key changes in this `app.py`:**
|
42 |
+
|
43 |
+
1. **APIB (Narrate via HF)"] = {"id": "mistralai/Mistral-7B-Instruct-v0.2", "type": "hf_text"}
|
44 |
+
TEXT_MODELS["Gemma 2B (Narrate via HF)"] = {"id": "google/gemma-2b-it", Readiness Checks:** Imports and uses `is_dalle_ready()` and `is_hf_image_api_ready()` from `image_services.py`.
|
45 |
+
2. **`IMAGE_PROVIDERS` Configuration:** Prioritizes DALL-E options if `DALLE_IMAGE_IS_READY` is true, then falls back to HF image models "type": "hf_text"}
|
46 |
+
if TEXT_MODELS:
|
47 |
+
if GEMINI_TEXT_IS_READY and "β¨ Gemini 1.5 Flash (Narrate)" in TEXT_MODELS: UI_DEFAULT_TEXT_MODEL_KEY = "β¨ Gemini 1.5 Flash (Narrate)"
|
48 |
+
elif HF_TEXT_IS_READY and "Mistral 7B (Narrate via HF)" in TEXT_MODELS: UI_DEFAULT_TEXT_MODEL_KEY = "Mistral 7B (Narrate via HF)"
|
49 |
+
else: UI if `HF_IMAGE_IS_READY` is true.
|
50 |
+
3. **Orchestrator Logic (`add_scene_to_story_orchestrator`):**
|
51 |
+
* Correctly identifies the selected image provider type (DALL-E or specific HF model).
|
52 |
+
* Calls `generate_image_dalle()`_DEFAULT_TEXT_MODEL_KEY = list(TEXT_MODELS.keys())[0]
|
53 |
+
else:
|
54 |
+
TEXT_MODELS["No Text Models Configured"] = {"id": "dummy_text_error", or `generate_image_hf_model()` accordingly.
|
55 |
+
4. **UI Labels and Info:** Updated to reflect the DALL-E and HF image options.
|
56 |
+
|
57 |
+
```python
|
58 |
+
# storyverse_weaver/app.py
|
59 |
+
import "type": "none"}
|
60 |
+
UI_DEFAULT_TEXT_MODEL_KEY = "No Text Models Configured"
|
61 |
+
|
62 |
+
|
63 |
+
IMAGE_PROVIDERS = {} # Reset and rebuild
|
64 |
+
UI_DEFAULT_IMAGE_PROVIDER_KEY = None
|
65 |
+
|
66 |
+
if DALLE_IMAGE_IS_READY: # Prioritize DALL-E
|
67 |
+
IMAGE_PROVIDERS["πΌοΈ OpenAI gradio as gr
|
68 |
+
import os
|
69 |
+
import time
|
70 |
+
import json
|
71 |
+
from PIL import Image, ImageDraw, ImageFont
|
72 |
+
import random
|
73 |
+
import traceback
|
74 |
+
|
75 |
+
# --- Core Logic Imports ---
|
76 |
+
from core.llm_services import initialize_text_llms, is_gemini_text_ready, is_hf_text_ready, generate_text_gemini, generate_text_hf
|
77 |
+
# Updated import from image_services
|
78 |
+
from core.image DALL-E 3"] = "dalle_3"
|
79 |
+
IMAGE_PROVIDERS["πΌοΈ OpenAI DALL-E 2 (Legacy)"] = "dalle_2"
|
80 |
+
UI_DEFAULT_IMAGE_PROVIDER_KEY = "πΌοΈ OpenAI DALL-E 3"
|
81 |
+
elif HF_IMAGE_IS_READY: # Fallback to HF if DALL-E not ready
|
82 |
+
IMAGE_PROVIDERS["π‘ HF - Stable Diffusion XL Base (Fallback)"] = "hf_sdxl_base"
|
83 |
+
IMAGE_PROVIDERS["π HF - Open_services import initialize_image_llms, is_dalle_ready, is_hf_image_api_ready, generate_image_dalle, generate_image_hf_model, ImageGenResponse
|
84 |
+
from core.story_engine import Story, Scene
|
85 |
+
from prompts.narrative_prompts import get_narrative_system_prompt, format_narrative_user_prompt
|
86 |
+
from prompts.image_style_prompts import STYLE_PRESETS, COMMON_NEGATIVE_PROMPTS, format_image_generation_prompt
|
87 |
+
from core.utils import basic_text_cleanup
|
88 |
+
|
89 |
+
# --- Initialize Services ---
|
90 |
+
initialize_text_llms()
|
91 |
+
initialize_imageJourney (Fallback)"] = "hf_openjourney"
|
92 |
+
IMAGE_PROVIDERS["π HF - Stable Diffusion v1.5 (Fallback)"] = "hf_sd_1_5"
|
93 |
+
UI_DEFAULT_IMAGE_PROVIDER_KEY = "π‘ HF - Stable Diffusion XL Base (Fallback)"
|
94 |
+
|
95 |
+
if not IMAGE_PROVIDERS: # If neither is ready
|
96 |
+
IMAGE_PROVIDERS["No Image Providers Configured"] = "none"
|
97 |
+
_llms()
|
98 |
+
|
99 |
+
# --- Get API Readiness Status ---
|
100 |
+
GEMINI_TEXT_IS_READY = is_gemini_text_ready()
|
101 |
+
HF_TEXT_IS_READY = is_hf_text_ready()
|
102 |
+
DALLE_IMAGE_IS_READY = is_dalle_ready() # For DALL-E
|
103 |
+
HF_IMAGE_IS_READY = is_hf_image_api_ready() # For HF image models
|
104 |
+
|
105 |
+
# --- Application Configuration (Models, Defaults) ---
|
106 |
+
TEXT_MODELS = {}
|
107 |
+
UI_DEFAULT_TEXTUI_DEFAULT_IMAGE_PROVIDER_KEY = "No Image Providers Configured"
|
108 |
+
elif not UI_DEFAULT_IMAGE_PROVIDER_KEY and IMAGE_PROVIDERS : # Should not happen if logic above is correct
|
109 |
+
UI_DEFAULT_IMAGE_PROVIDER_KEY = list(IMAGE_PROVIDERS.keys())[0]
|
110 |
+
|
111 |
+
|
112 |
+
# --- Gradio UI Theme and CSS ---
|
113 |
+
# (omega_theme and omega_css definitions remain THE SAME as the last full app.py version)
|
114 |
+
omega_theme = gr.themes.Base(font=[gr.themes.GoogleFont("Lexend Deca_MODEL_KEY = None
|
115 |
+
# ... (TEXT_MODELS and UI_DEFAULT_TEXT_MODEL_KEY population logic remains the same as previous full app.py)
|
116 |
+
if GEMINI_TEXT_IS_READY:
|
117 |
TEXT_MODELS["β¨ Gemini 1.5 Flash (Narrate)"] = {"id": "gemini-1.5-flash-latest", "type": "gemini"}
|
118 |
TEXT_MODELS["Legacy Gemini 1.0 Pro (Narrate)"] = {"id": "gemini-1.0-pro-latest", "type": "gemini"}
|
119 |
+
if HF_TEXT_IS_READY:
|
120 |
+
TEXT_MODELS["Mistral 7B (Narrate via HF)"] = {"id": "mistralai/Mistral-7")], primary_hue=gr.themes.colors.purple).set(body_background_fill="#0F0F1A", block_background_fill="#1A1A2E", slider_color="#A020F0")
|
121 |
+
omega_css = "body, .gradio-container { background-color: #0F0F1A !important; color: #D0D0E0 !important; } /* Paste your full omega_css here */"
|
122 |
+
|
123 |
+
|
124 |
+
# --- Helper: Placeholder Image Creation ---
|
125 |
+
def create_placeholder_image(text="Processing...", size=(512, 512), color="#23233A", text_color="#E0E0FF"):
|
126 |
+
# ... (Full implementation as before)
|
127 |
+
img = Image.new('RGB', size,B-Instruct-v0.2", "type": "hf_text"}
|
128 |
+
TEXT_MODELS["Gemma 2B (Narrate via HF)"] = {"id": "google/gemma-2b-it", "type": "hf_text"}
|
129 |
+
if TEXT_MODELS:
|
130 |
+
if GEMINI_TEXT_IS_READY and "β¨ Gemini 1.5 Flash (Narrate)" in TEXT_MODELS: UI_DEFAULT_TEXT_MODEL_KEY = "β¨ Gemini 1.5 Flash (Narrate)"
|
131 |
+
elif HF_TEXT_IS_READY and "Mistral 7B (Narrate via HF)" in TEXT_MODELS: UI_DEFAULT_TEXT_MODEL_KEY = "Mistral 7B (Narrate via HF)"
|
132 |
+
else: UI_DEFAULT_TEXT_MODEL_KEY = list(TEXT_MODELS.keys())[0 color=color); draw = ImageDraw.Draw(img)
|
133 |
+
try: font_path = "arial.ttf" if os.path.exists("arial.ttf") else None
|
134 |
+
except: font_path = None
|
135 |
+
try: font = ImageFont.truetype(font_path, 40) if font_path else ImageFont.load_default()
|
136 |
+
except IOError: font = ImageFont.load_default()
|
137 |
+
if hasattr(draw, 'textbbox'): bbox = draw.textbbox((0,0), text, font=font); tw, th = bbox[2]-bbox[0], bbox[3]-bbox[1]
|
138 |
+
else: tw, th = draw.textsize(text, font=font)
|
139 |
+
draw.text(((size[0]-tw)/2, (size[1]-th)/2), text, font=font, fill=text_color); return img
|
140 |
+
|
141 |
+
# --- StoryVerse Weaver Orchestrator (MODIFIED image generation part) ---
|
142 |
+
def add_scene_to_story_orchestrator(
|
143 |
+
current_story_obj: Story, scene_prompt_text: str, image_style_dropdown: str, artist_style_text: str,
|
144 |
+
negative_]
|
145 |
+
else:
|
146 |
TEXT_MODELS["No Text Models Configured"] = {"id": "dummy_text_error", "type": "none"}
|
147 |
UI_DEFAULT_TEXT_MODEL_KEY = "No Text Models Configured"
|
148 |
|
149 |
|
150 |
+
IMAGE_PROVIDERS = {} # Rebuild this based on new priorities
|
151 |
UI_DEFAULT_IMAGE_PROVIDER_KEY = None
|
152 |
+
|
153 |
+
if DALLE_IMAGE_IS_READY:
|
154 |
+
IMAGE_PROVIDERS["πΌοΈ OpenAI DALL-E 3"] = "dalle_3" # This key will map to model="dall-e-3"
|
155 |
+
IMAGE_PROVIDERS["πΌοΈ OpenAI DALL-E 2 (Legacy)"] = "dalle_2"
|
156 |
UI_DEFAULT_IMAGE_PROVIDER_KEY = "πΌοΈ OpenAI DALL-E 3"
|
|
|
|
|
|
|
|
|
157 |
|
158 |
+
if HF_IMAGE_IS_READY:
|
159 |
+
IMAGE_PROVIDERS["π‘ HF - Stable Diffusion XL Base"] = "hf_sdxl_base"
|
160 |
+
IMAGE_PROVIDERS["π HF - OpenJourney (Midjourney-like)"] = "hf_openjourney"
|
161 |
+
IMAGE_PROVIDERSprompt_text: str, text_model_key: str, image_provider_key: str,
|
162 |
+
narrative_length: str, image_quality: str,
|
163 |
+
progress=gr.Progress(track_tqdm=True)
|
164 |
+
):
|
165 |
+
start_time = time.time()
|
166 |
+
if not current_story_obj: current_story_obj = Story()
|
167 |
+
log_accumulator = [f"**π Scene {current_story_obj.current_scene_number + 1} - {time.strftime('%H:%M:%S')}**"]
|
168 |
+
# ... (Initialize ret_... placeholders as before) ...
|
169 |
+
ret_story_state, ret_gallery, ret_latest_image, ret_latest_narrative_md_obj, ret_status_bar_html_obj, ret_log_md = \
|
170 |
+
current_story_obj, current_story_obj.get_all_scenes_for_gallery_display(), None, gr.Markdown("Processing..."), gr.HTML("<p>Processing...</p>"), gr.Markdown("\n".join(log_accumulator))
|
171 |
+
|
172 |
+
# Initial yield (buttons handled by .then() chain)
|
173 |
+
yield {
|
174 |
+
output_status_bar: gr.HTML(["π HF - Stable Diffusion v1.5"] = "hf_sd_1_5"
|
175 |
+
if not UI_DEFAULT_IMAGE_PROVIDER_KEY: # If DALL-E wasn't ready, default to HF
|
176 |
+
UI_DEFAULT_IMAGE_PROVIDER_KEY = "π‘ HF - Stable Diffusion XL Base"
|
177 |
+
|
178 |
+
if not IMAGE_PROVIDERS: # If neither DALL-E nor HF images are ready
|
179 |
IMAGE_PROVIDERS["No Image Providers Configured"] = "none"
|
180 |
UI_DEFAULT_IMAGE_PROVIDER_KEY = "No Image Providers Configured"
|
181 |
|
182 |
|
183 |
+
# --- Gradio UI Theme and CSS ---
|
184 |
+
# (omega_theme and omega_css definitions remain the same as the last full app.py)
|
185 |
omega_theme = gr.themes.Base(font=[gr.themes.GoogleFont("Lexend Deca")], primary_hue=gr.themes.colors.purple).set(body_background_fill="#0F0F1A", block_background_fill="#1A1A2E", slider_color="#A020F0")
|
186 |
+
omega_css = """ /* ... Paste your full omega_css string here ... */ """
|
187 |
+
|
188 |
+
# --- Helper: Placeholder Image Creation ---
|
189 |
+
def create_placeholder_image(text="Processing...", size=(512, 512), color="#23233A", text_color="#value=f"<p class='processing_text status_text'>π Weaving Scene {current_story_obj.current_scene_number + 1}...</p>"),
|
190 |
+
output_latest_scene_image: gr.Image(value=create_placeholder_image("π¨ Conjuring visuals...")),
|
191 |
+
output_latest_scene_narrative: gr.Markdown(value=" Musing narrative..."),
|
192 |
+
output_interaction_log_markdown: gr.Markdown(value="\n".join(log_accumulator))
|
193 |
+
}
|
194 |
+
|
195 |
+
try:
|
196 |
+
if not scene_prompt_text.strip(): raise ValueError("Scene prompt cannot be empty!")
|
197 |
+
|
198 |
+
# --- 1. Generate Narrative Text (No change here, uses Gemini or HF) ---
|
199 |
+
progress(0.1, desc="βοΈ Crafting narrative...")
|
200 |
+
# ... (Full narrative generation logic - PASTE FROM PREVIOUS WORKING VERSION) ...
|
201 |
+
# Example:
|
202 |
+
narrative_text_generated = "Simulated Narrative: " + scene_prompt_text[:30] # Placeholder
|
203 |
+
text_model_info = TEXT_MODELS.get(text_model_key) # Get full model info
|
204 |
+
if text_model_info and text_model_info["type"] != "none":
|
205 |
+
# ... call generate_text_gemini or generate_text_hf ...
|
206 |
+
log_accumulator.append(f" Narrative: Using {text_model_key} (simulated).")
|
207 |
+
else:
|
208 |
+
narrative_text_generated = "**Narrative Error:** Text model not available."
|
209 |
+
E0E0FF"):
|
210 |
+
# ... (Full implementation as before) ...
|
211 |
img = Image.new('RGB', size, color=color); draw = ImageDraw.Draw(img); #... (full implementation)
|
212 |
try: font_path = "arial.ttf" if os.path.exists("arial.ttf") else None
|
213 |
except: font_path = None
|
|
|
217 |
else: tw, th = draw.textsize(text, font=font)
|
218 |
draw.text(((size[0]-tw)/2, (size[1]-th)/2), text, font=font, fill=text_color); return img
|
219 |
|
|
|
220 |
# --- StoryVerse Weaver Orchestrator (MODIFIED image generation part) ---
|
221 |
def add_scene_to_story_orchestrator(
|
222 |
current_story_obj: Story, scene_prompt_text: str, image_style_dropdown: str, artist_style_text: str,
|
223 |
+
negative_prompt_text: str, text_model_key: str, image_provider_key: str,
|
224 |
narrative_length: str, image_quality: str,
|
225 |
progress=gr.Progress(track_tqdm=True)
|
226 |
):
|
227 |
start_time = time.time()
|
228 |
if not current_story_obj: current_story_obj = Story()
|
229 |
+
log_accumulator = [f"**π Scene {current_story_obj.current_scene_number + 1} - {log_accumulator.append(f" Narrative: FAILED - Model '{text_model_key}' not available.")
|
230 |
+
ret_latest_narrative_str_content = f"## Scene Idea: {scene_prompt_text}\n\n{narrative_text_generated}"
|
231 |
+
ret_latest_narrative_md_obj = gr.Markdown(value=ret_latest_narrative_str_content)
|
232 |
+
yield { output_latest_scene_narrative: ret_latest_narrative_md_obj,
|
233 |
+
output_interaction_log_markdown: gr.Markdown(value="\n".join(log_accumulator)) }
|
234 |
+
|
235 |
+
|
236 |
+
# --- 2. Generate Image (NOW PRIORITIZING DALL-E, THEN HF) ---
|
237 |
+
progress(0.5, desc="π¨ Conjuring visuals...")
|
238 |
+
image_generated_pil = None
|
239 |
+
image_generation_error_message = None
|
240 |
+
selected_image_provider_actual_type = IMAGE_PROVIDERS.get(image_provider_key) # e.g., "dalle_3", "hf_sdxl_base"
|
241 |
+
|
242 |
+
image_content_prompt_for_gen = narrative_text_generated if narrative_text_generated and "Error" not in narrative_text_generated else scene_prompt_text
|
243 |
+
quality_keyword = "detailed, high quality, cinematic lighting, " if image_quality == "High Detail" else ""
|
244 |
+
full_image_prompt = format_image_generation_prompt(quality_keyword + image_content_prompt_for_gen[:350], image_style_dropdown, artist_style_text)
|
245 |
+
log_accumulator.append(f" Image: Attempting with provider key '{image_provider_key}' (maps to type '{selected_image_provider_actual_type}'). Style: {image_style_dropdown}.")
|
246 |
+
|
247 |
+
if selected_image_provider_actual_type and selected_image_provider_actualtime.strftime('%H:%M:%S')}**"]
|
248 |
# ... (Initialize ret_... placeholders as before) ...
|
249 |
ret_story_state, ret_gallery, ret_latest_image, ret_latest_narrative_md_obj, ret_status_bar_html_obj, ret_log_md = \
|
250 |
current_story_obj, current_story_obj.get_all_scenes_for_gallery_display(), None, gr.Markdown("Processing..."), gr.HTML("<p>Processing...</p>"), gr.Markdown("\n".join(log_accumulator))
|
251 |
|
252 |
+
# Initial yield for UI updates (buttons handled by .then() chain)
|
253 |
yield {
|
254 |
output_status_bar: gr.HTML(value=f"<p class='processing_text status_text'>π Weaving Scene {current_story_obj.current_scene_number + 1}...</p>"),
|
|
|
255 |
output_latest_scene_image: gr.Image(value=create_placeholder_image("π¨ Conjuring visuals...")),
|
256 |
output_latest_scene_narrative: gr.Markdown(value=" Musing narrative..."),
|
257 |
output_interaction_log_markdown: gr.Markdown(value="\n".join(log_accumulator))
|
258 |
}
|
|
|
259 |
|
260 |
try:
|
261 |
if not scene_prompt_text.strip(): raise ValueError("Scene prompt cannot be empty!")
|
262 |
|
263 |
+
# --- 1. Generate Narrative Text (Gemini or HF fallback) ---
|
264 |
progress(0.1, desc="βοΈ Crafting narrative...")
|
265 |
+
# ... (Full narrative generation logic from previous app.py) ...
|
266 |
+
# ... (This part should be copied from your last working version, it already handles Gemini/HF choice) ...
|
267 |
narrative_text_generated = "Simulated Narrative." # Placeholder
|
268 |
text_model_info = TEXT_MODELS.get(text_model_key)
|
269 |
if text_model_info and text_model_info["type"] != "none":
|
270 |
system_p = get_narrative_system_prompt("default"); prev_narrative = current_story_obj.get_last_scene_narrative(); user_p = format_narrative_user_prompt(scene_prompt_text, prev_narrative)
|
271 |
+
log_accumulator_type != "none":
|
272 |
+
image_response = None
|
273 |
+
if selected_image_provider_actual_type.startswith("dalle_"): # Catches "dalle_3", "dalle_2"
|
274 |
+
if DALLE_IMAGE_IS_READY:
|
275 |
+
dalle_model_version = "dall-e-3" if selected_image_provider_actual_type == "dalle_3" else "dall-e-2"
|
276 |
+
dalle_size = "1024x1024" # DALL-E 3 supports more, DALL-E 2 has fixed
|
277 |
+
if dalle_model_version == "dall-e-3" and image_quality == "High Detail": dalle_quality = "hd"
|
278 |
+
else: dalle_quality = "standard"
|
279 |
+
image_response = generate_image_dalle(full_image_prompt, model=dalle_model_version, size=dalle_size, quality=dalle_quality)
|
280 |
+
else:
|
281 |
+
image_generation_error_message = "**Image Error:** DALL-E selected but API not ready (check STORYVERSE_OPENAI_API_KEY)."
|
282 |
+
elif selected_image_provider_actual_type.startswith("hf_"):
|
283 |
+
if HF_IMAGE_IS_READY:
|
284 |
+
hf_model_id_to_call = "stabilityai/stable-diffusion-xl-base-1.0" # Default HF
|
285 |
+
img_width, img_height = 768, 768
|
286 |
+
if selected_image_provider_actual_type == "hf_openjourney": hf_model_id_to_call = "prompthero/openjourney"; img_width,img_height = 512,512
|
287 |
+
elif selected_image_provider_actual_type == "hf_sd_1_5": hf_model_id_to_call = "runwayml/stable-diffusion-v1-5"; img_width,img_height = 512,512
|
288 |
+
image_response = generate_image_hf_model(full_image_prompt, model_id=hf_model_id_to_call, negative_prompt=negative_prompt_text or COMMON_NEGATIVE_PROMPTS, width=img_width, height=img.append(f" Narrative: Using {text_model_key} ({text_model_info['id']}).")
|
289 |
text_response = None
|
290 |
if text_model_info["type"] == "gemini": text_response = generate_text_gemini(user_p, model_id=text_model_info["id"], system_prompt=system_p, max_tokens=768 if narrative_length.startswith("Detailed") else 400)
|
291 |
elif text_model_info["type"] == "hf_text": text_response = generate_text_hf(user_p, model_id=text_model_info["id"], system_prompt=system_p, max_tokens=768 if narrative_length.startswith("Detailed") else 400)
|
|
|
298 |
yield { output_latest_scene_narrative: ret_latest_narrative_md_obj, output_interaction_log_markdown: gr.Markdown(value="\n".join(log_accumulator)) }
|
299 |
|
300 |
|
301 |
+
# --- 2. Generate Image (NOW USING DALL-E primary, HF fallback) ---
|
302 |
progress(0.5, desc="π¨ Conjuring visuals...")
|
303 |
image_generated_pil = None
|
304 |
image_generation_error_message = None
|
305 |
+
# `image_provider_key` is the UI display string, e.g., "πΌοΈ OpenAI DALL-E 3"
|
306 |
+
_height)
|
307 |
+
else:
|
308 |
+
image_generation_error_message = "**Image Error:** HF Image Model selected but API not ready (check STORYVERSE_HF_TOKEN)."
|
309 |
+
else:
|
310 |
+
image_generation_error_message = f"**Image Error:** Provider type '{selected_image_provider_actual_type}' is not handled."
|
311 |
+
|
312 |
+
if image_response and image_response.success:
|
313 |
+
image_generated_pil = image_response.image
|
314 |
+
log_accumulator.append(f" Image: Success from {image_response.provider} (Model: {image_response.model_id_used}).")
|
315 |
+
elif image_response:
|
316 |
+
image_generation_error_message = f"**Image Error ({image_response.provider} - {image_response.model_id_used}):** {image_response.error}"
|
317 |
+
log_accumulator.append(f" Image: FAILED - {image_response.error}")
|
318 |
+
elif not image_generation_error_message: # If no response and no specific error set yet
|
319 |
+
image_generation_error_message = f"**Image Error:** No response/unknown issue with {image_provider_key}."
|
320 |
+
|
321 |
+
if not image_generated_pil and not image_generation_error_message: # If no provider matched or was ready
|
322 |
+
image_generation_error_message = "**Image Error:** No valid image provider configured or selected for this request."
|
323 |
+
log_accumulator.append(f" Image: FAILED - {image_generation_error_message}")
|
324 |
+
|
325 |
+
ret_latest_image = image_generated_pil if image_generated_pil else create_placeholder_image("Image Gen Failed", color="#401010")
|
326 |
+
yield { output_latest_scene_image: gr.Image(value=ret_latest_image),
|
327 |
+
output_interaction_log_markdown: gr.Markdown(value="\n".join(log_accumulator)) }
|
328 |
+
|
329 |
+
# --- 3. Add Scene to Story Object & 4. Prepare Final Return Values ---
|
330 |
+
# ... (This part remains largely the same as the previous full app.py) ...
|
331 |
+
final_scene_error=None; # ... (set based on narrative/image errors) ...
|
332 |
+
if image_generation_error_message and "**Narrative Error**" in narrative_text_generated : final_scene_error = f"{narrative_text_generated}\n{image_generation_error_message}"
|
333 |
+
elif "**Narrative Error**" in narrative_text_generated: final_scene_error = narrative_text_generated
|
334 |
+
elif image_generation_error_message: final_scene_error = image_generation_error_message
|
335 |
+
current_story_obj.add_scene_from_elements(user_prompt=scene_prompt_text # `selected_image_provider_type` is the internal type, e.g., "dalle_3"
|
336 |
+
selected_image_provider_type = IMAGE_PROVIDERS.get(image_provider_key)
|
337 |
|
338 |
image_content_prompt_for_gen = narrative_text_generated if narrative_text_generated and "Error" not in narrative_text_generated else scene_prompt_text
|
339 |
+
quality_keyword = "detailed, high quality, vivid colors, " if image_quality == "High Detail" else ""
|
340 |
full_image_prompt = format_image_generation_prompt(quality_keyword + image_content_prompt_for_gen[:350], image_style_dropdown, artist_style_text)
|
341 |
+
log_accumulator.append(f" Image: Attempting with provider key '{image_provider_key}' (maps to type '{selected_image_provider_type}'). Style: {image_style_dropdown}.")
|
342 |
|
343 |
+
if selected_image_provider_type and selected_image_provider_type != "none":
|
344 |
image_response = None
|
345 |
+
if selected_image_provider_type == "dalle_3":
|
346 |
if DALLE_IMAGE_IS_READY:
|
347 |
image_response = generate_image_dalle(full_image_prompt, model="dall-e-3", quality="hd" if image_quality=="High Detail" else "standard")
|
348 |
+
else: image_generation_error_message = "**Image Error:** DALL-E 3 selected but API not ready (check STORYVERSE_OPENAI_API_KEY)."
|
349 |
+
elif selected_image_provider_type == "dalle_2":
|
350 |
if DALLE_IMAGE_IS_READY:
|
351 |
+
image_response = generate_image_dalle(full_image_prompt, model="dall-e-2", size="1024x1024")
|
352 |
else: image_generation_error_message = "**Image Error:** DALL-E 2 selected but API not ready."
|
353 |
+
# Fallback to HF models
|
354 |
+
elif selected_image_provider_type.startswith("hf_"):
|
355 |
if HF_IMAGE_IS_READY:
|
356 |
hf_model_id_to_call = "stabilityai/stable-diffusion-xl-base-1.0" # Default HF
|
357 |
img_width, img_height = 768, 768
|
358 |
+
if selected_image_provider_type == "hf_openjourney": hf_model_id_to_call = "prompthero/openjourney"; img_width,img_height = 512,512
|
359 |
+
elif selected_image_provider_type == "hf_sd_1_5": hf_model_id_to_call = "runwayml/stable-diffusion-v1-5"; img_width,img_height = 512,512
|
360 |
image_response = generate_image_hf_model(full_image_prompt, model_id=hf_model_id_to_call, negative_prompt=negative_prompt_text or COMMON_NEGATIVE_PROMPTS, width=img_width, height=img_height)
|
361 |
+
else: image_generation_error_message = "**Image Error:** HF Image Model selected but API not ready (check STORY, narrative_text=narrative_text_generated, image=image_generated_pil, image_style_prompt=f"{image_style_dropdown} by {artist_style_text}", image_provider=image_provider_key, error_message=final_scene_error)
|
362 |
+
ret_story_state = current_story_obj; log_accumulator.append(f" Scene {current_story_obj.current_scene_number} processed.")
|
363 |
+
ret_gallery = current_story_obj.get_all_scenes_for_gallery_display()
|
364 |
+
_ , latest_narr_for_display_final_str_temp = current_story_obj.get_latest_scene_details_for_display()
|
365 |
+
ret_latest_narrative_md_obj = gr.Markdown(value=latest_narr_for_display_final_str_temp)
|
366 |
+
status_html_str_temp = f"<p class='error_text'>Scene added with errors.</p>" if final_scene_error else f"<p class='success_text'>π Scene woven!</p>"
|
367 |
+
ret_status_bar_html_obj = gr.HTML(value=status_html_str_temp)
|
368 |
+
progress(1.0, desc="Scene Complete!")
|
369 |
+
|
370 |
+
|
371 |
+
except ValueError as ve: # ... (Error handling as before) ...
|
372 |
+
log_accumulator.append(f"\n**INPUT ERROR:** {ve}"); ret_status_bar_html_obj = gr.HTML(f"<p class='error_text'>ERROR: {ve}</p>"); ret_latest_narrative_md_obj = gr.Markdown(f"## Error\n{ve}")
|
373 |
+
except Exception as e: # ... (Error handling as before) ...
|
374 |
+
log_accumulator.append(f"\n**RUNTIME ERROR:** {e}\n{traceback.format_exc()}"); ret_status_bar_html_obj = gr.HTML(f"<p class='error_text'>UNEXPECTED ERROR: {e}</p>"); ret_latest_narrative_md_obj = gr.Markdown(f"## Unexpected Error\n{e}")
|
375 |
+
|
376 |
+
current_total_time = time.time() - start_time
|
377 |
+
log_accumulator.append(f" Cycle ended. Total time: {current_total_time:.2f}s")
|
378 |
+
ret_log_md = gr.Markdown(value="\n".join(log_accumulator))
|
379 |
+
|
380 |
+
return (ret_story_state, ret_gallery, ret_latest_image, ret_latest_narrative_md_obj, ret_status_bar_html_obj, ret_log_md)
|
381 |
+
|
382 |
+
# --- clear_story_state_ui_wrapper, surprise_me_func, disable_buttons_for_processing, enable_buttons_after_processing ---
|
383 |
+
# (These functions remain IDENTICAL to the ones in the last full app.py that fixed the ValueError)
|
384 |
+
def clear_story_state_ui_wrapper(): new_story=Story(); ph_img=create_placeholder_image("Blank..."); return(new_story,[(ph_img,"New...")],None,gr.Markdown("## Cleared"),gr.HTML("<p>Cleared.</p>"),"Log Cleared","")
|
385 |
+
def surprise_me_func(): themes = ["Cosmic Horror", "Solarpunk Utopia"]; actions = ["unearths an artifact", "negotiates"]; settings = ["on a rogue planet", "in a city in a tree"]; prompt = f"A protagonist {random.choice(actions)} {random.choice(settings)}. Theme: {random.choice(themes)}."; style = random.choice(list(STYLE_PRESETS.keys())); artist = random.choice(["H.R. Giger", "Moebius",VERSE_HF_TOKEN)."
|
386 |
+
else: image_generation_error_message = f"**Image Error:** Provider type '{selected_image_provider_type}' not handled."
|
387 |
|
388 |
if image_response and image_response.success: image_generated_pil = image_response.image; log_accumulator.append(f" Image: Success from {image_response.provider} (Model: {image_response.model_id_used}).")
|
389 |
elif image_response: image_generation_error_message = f"**Image Error ({image_response.provider} - {image_response.model_id_used}):** {image_response.error}"; log_accumulator.append(f" Image: FAILED - {image_response.error}")
|
390 |
elif not image_generation_error_message: image_generation_error_message = f"**Image Error:** No response/unknown issue with {image_provider_key}."; log_accumulator.append(f" Image: FAILED - No response object.")
|
391 |
|
392 |
+
if not image_generated_pil and not image_generation_error_message:
|
393 |
+
image_generation_error_message = "**Image Error:** No valid image provider configured or selected for the chosen option."
|
394 |
log_accumulator.append(f" Image: FAILED - {image_generation_error_message}")
|
395 |
|
396 |
ret_latest_image = image_generated_pil if image_generated_pil else create_placeholder_image("Image Gen Failed", color="#401010")
|
|
|
409 |
ret_status_bar_html_obj = gr.HTML(value=status_html_str_temp)
|
410 |
progress(1.0, desc="Scene Complete!")
|
411 |
|
|
|
412 |
except ValueError as ve: # ... (Error handling as before) ...
|
413 |
log_accumulator.append(f"\n**INPUT ERROR:** {ve}"); ret_status_bar_html_obj = gr.HTML(f"<p class='error_text'>ERROR: {ve}</p>"); ret_latest_narrative_md_obj = gr.Markdown(f"## Error\n{ve}")
|
414 |
except Exception as e: # ... (Error handling as before) ...
|
415 |
log_accumulator.append(f"\n**RUNTIME ERROR:** {e}\n{traceback.format_exc()}"); ret_status_bar_html_obj = gr.HTML(f"<p class='error_text'>UNEXPECTED ERROR: {e}</p>"); ret_latest_narrative_md_obj = gr.Markdown(f"## Unexpected Error\n{e}")
|
416 |
+
""]*2); return prompt, style, artist
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
417 |
def disable_buttons_for_processing(): return gr.Button(interactive=False), gr.Button(interactive=False)
|
418 |
def enable_buttons_after_processing(): return gr.Button(interactive=True), gr.Button(interactive=True)
|
419 |
|
|
|
421 |
# --- Gradio UI Definition ---
|
422 |
with gr.Blocks(theme=omega_theme, css=omega_css, title="β¨ StoryVerse Omega β¨") as story_weaver_demo:
|
423 |
story_state_output = gr.State(Story())
|
424 |
+
# ... (Full UI layout from the "app.py in full with update" response where NameError for create_placeholder was fixed)
|
425 |
# ... (This includes defining all component variables like scene_prompt_input, output_gallery, engage_button etc. IN THE LAYOUT)
|
426 |
+
# Key change: Update the image_provider_dropdown choices and default value.
|
427 |
gr.Markdown("<div align='center'><h1>β¨ StoryVerse Omega β¨</h1>\n<h3>Craft Immersive Multimodal Worlds with AI</h3></div>")
|
428 |
gr.HTML("<div class='important-note'><strong>Welcome, Worldsmith!</strong> ... API keys (<code>STORYVERSE_...</code>) ...</div>")
|
429 |
with gr.Accordion("π§ AI Services Status & Info", open=False):
|
|
|
447 |
with gr.Accordion("βοΈ Advanced AI Configuration", open=False):
|
448 |
with gr.Group():
|
449 |
text_model_dropdown = gr.Dropdown(choices=list(TEXT_MODELS.keys()), value=UI_DEFAULT_TEXT_MODEL_KEY, label="Narrative AI Engine")
|
450 |
+
image_provider_dropdown = gr.Dropdown(choices=list(IMAGE_PROVIDERS.keys()), value=UI_DEFAULT_IMAGE_PROVIDER_KEY, label="Visual AI Engine (DALL-E/HF)") # UPDATED LABEL
|
451 |
with gr.Row():
|
452 |
narrative_length_dropdown = gr.Dropdown(["Short", "Medium", "Detailed"], value="Medium", label="Narrative Detail")
|
453 |
image_quality_dropdown = gr.Dropdown(["Standard", "High Detail", "Sketch"], value="Standard", label="Image Detail")
|
454 |
with gr.Row(elem_classes=["compact-row"], equal_height=True):
|
455 |
+
engage_button = gr.Button("π Weave Scene!", variant="primary", scale=3
|
456 |
+
current_total_time = time.time() - start_time
|
457 |
+
log_accumulator.append(f" Cycle ended. Total time: {current_total_time:.2f}s")
|
458 |
+
ret_log_md = gr.Markdown(value="\n".join(log_accumulator))
|
459 |
+
|
460 |
+
return (ret_story_state, ret_gallery, ret_latest_image, ret_latest_narrative_md_obj, ret_status_bar_html_obj, ret_log_md)
|
461 |
+
|
462 |
+
# --- clear_story_state_ui_wrapper, surprise_me_func, disable_buttons_for_processing, enable_buttons_after_processing ---
|
463 |
+
# (These functions remain IDENTICAL to the ones in the last full app.py)
|
464 |
+
def clear_story_state_ui_wrapper(): new_story=Story(); ph_img=create_placeholder_image("Blank..."); return(new_story,[(ph_img,"New...")],None,gr.Markdown("## Cleared"),gr.HTML("<p>Cleared.</p>"),"Log Cleared","")
|
465 |
+
def surprise_me_func(): themes = ["Cosmic Horror", "Solarpunk Utopia"]; actions = ["unearths an artifact", "negotiates"]; settings = ["on a rogue planet", "in a city in a tree"]; prompt = f"A protagonist {random.choice(actions)} {random.choice(settings)}. Theme: {random.choice(themes)}."; style = random.choice(list(STYLE_PRESETS.keys())); artist = random.choice(["H.R. Giger", "Moebius", ""]*2); return prompt, style, artist
|
466 |
+
def disable_buttons_for_processing(): return gr.Button(interactive=False), gr.Button(interactive=False)
|
467 |
+
def enable_buttons_after_processing(): return gr.Button(interactive=True), gr.Button(interactive=True)
|
468 |
+
|
469 |
+
# --- Gradio UI Definition ---
|
470 |
+
with gr.Blocks(theme=omega_theme, css=omega_css, title="β¨ StoryVerse Omega β¨") as story_weaver_demo:
|
471 |
+
# Define Python variables for UI components
|
472 |
+
story_state_output = gr.State(Story())
|
473 |
+
scene_prompt_input = gr.Textbox()
|
474 |
+
image_style_input = gr.Dropdown()
|
475 |
+
artist_style_input = gr.Textbox()
|
476 |
+
negative_prompt_input = gr.Textbox()
|
477 |
+
text_model_dropdown = gr.Dropdown()
|
478 |
+
image_provider_dropdown = gr.Dropdown()
|
479 |
+
narrative_length_dropdown = gr.Dropdown()
|
480 |
+
image_quality_dropdown = gr.Dropdown()
|
481 |
+
output_gallery = gr.Gallery()
|
482 |
+
output_latest_scene_image = gr.Image()
|
483 |
+
output_latest_scene_narrative = gr.Markdown()
|
484 |
+
output_status_bar = gr.HTML()
|
485 |
+
output_interaction_log_markdown = gr.Markdown()
|
486 |
+
engage_button = gr.Button()
|
487 |
+
surprise_button = gr.Button()
|
488 |
+
clear_story_button = gr.Button()
|
489 |
+
|
490 |
+
# Layout UI
|
491 |
+
gr.Markdown("<div align='center'><h1>β¨ StoryVerse Omega β¨</h1>\n<h3>Craft Immersive Multimodal Worlds with AI</h3></div>")
|
492 |
+
gr.HTML("<div class='important-note'><strong>Welcome, Worldsmith!</strong> ... API keys ...</div>")
|
493 |
+
|
494 |
+
with gr.Accordion("π§ AI Services Status & Info", open=False): # Updated status check
|
495 |
+
status_text_list = []; text_llm_ok = (GEMINI_TEXT_IS_READY or HF_TEXT_IS_READY); image_gen_ok = (DALLE_IMAGE_IS_READY or HF_IMAGE_IS_READY)
|
496 |
+
if not text_llm_ok and not image_gen_ok: status_text_list.append("<p style='color:#FCA5A5;font-weight:bold;'>β οΈ CRITICAL: NO AI SERVICES CONFIGURED.</p>")
|
497 |
+
else:
|
498 |
+
if text_llm_ok: status_text_list.append("<p style='color:#A7F3D0;'>β
Text Generation Ready.</p>")
|
499 |
+
else: status_text_list.append("<p style='color:#FCD34D;'>β οΈ Text Generation NOT Ready.</p>")
|
500 |
+
if image_gen_ok: status_text_list.append("<p style='color:#A7F3D0;'>β
Image Generation Ready.</p>")
|
501 |
+
else: status_text_list.append("<p style='color:#FCD34D;'>β οΈ Image Generation NOT Ready.</p>")
|
502 |
+
gr.HTML("".join(status_text_list))
|
503 |
+
|
504 |
+
with gr.Row(equal_height=False, variant="panel"):
|
505 |
+
with gr.Column(scale=7, min_width=450):
|
506 |
+
gr.Markdown("### π‘ **Craft Your Scene**", elem_classes="input-section-header")
|
507 |
+
with gr.Group(): scene_prompt_input = gr.Textbox(lines=7, label="Scene Vision:", placeholder="e.g., Amidst swirling cosmic dust...")
|
508 |
+
with gr.Row(elem_classes=["compact-row"]):
|
509 |
+
with gr.Column(scale=2): image_style_input = gr.Dropdown(choices=["Default (Cinematic Realism)"] + sorted(list(STYLE_PRESETS.keys())), value="Default (Cinematic Realism)", label="Visual Style", allow_custom_, icon="β¨")
|
510 |
surprise_button = gr.Button("π² Surprise!", variant="secondary", scale=1, icon="π")
|
511 |
clear_story_button = gr.Button("ποΈ New", variant="stop", scale=1, icon="β»οΈ")
|
512 |
output_status_bar = gr.HTML(value="<p class='processing_text status_text'>Ready to weave!</p>")
|
|
|
531 |
if __name__ == "__main__":
|
532 |
print("="*80); print("β¨ StoryVerse Omega (DALL-E/Gemini Focus) Launching... β¨")
|
533 |
print(f" Gemini Text Ready: {GEMINI_TEXT_IS_READY}"); print(f" HF Text Ready: {HF_TEXT_IS_READY}")
|
534 |
+
print(f" DALL-E Image Ready: {DALLE_IMAGE_IS_READY}"); print(f" HF Image API Ready: {HF_IMAGE_IS_READY}")
|
535 |
if not (GEMINI_TEXT_IS_READY or HF_TEXT_IS_READY) or not (DALLE_IMAGE_IS_READY or HF_IMAGE_IS_READY): print(" π΄ WARNING: Not all services configured.")
|
536 |
print(f" Default Text Model: {UI_DEFAULT_TEXT_MODEL_KEY}"); print(f" Default Image Provider: {UI_DEFAULT_IMAGE_PROVIDER_KEY}")
|
537 |
print("="*80)
|