Update app.py
Browse files
app.py
CHANGED
|
@@ -15,13 +15,26 @@ SYSTEM_PROMPT = (
|
|
| 15 |
"strictly maintaining pack lore and narrative continuity."
|
| 16 |
)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def write_chapter(prompt, history, temp):
|
| 19 |
if not prompt or not prompt.strip():
|
| 20 |
return "Please enter a scene prompt or chapter instruction."
|
|
|
|
| 21 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 22 |
-
for
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
messages.append({"role": "user", "content": prompt})
|
| 26 |
|
| 27 |
try:
|
|
@@ -31,30 +44,36 @@ def write_chapter(prompt, history, temp):
|
|
| 31 |
max_tokens=2048,
|
| 32 |
temperature=temp,
|
| 33 |
)
|
| 34 |
-
return response.choices.message.content
|
| 35 |
except Exception as e:
|
| 36 |
return f"⚠️ Notice: {str(e)}"
|
| 37 |
|
| 38 |
def generate_scene_art(scene_prompt):
|
| 39 |
if not scene_prompt or not scene_prompt.strip():
|
| 40 |
return None
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def generate_book_trailer(video_prompt):
|
| 48 |
if not video_prompt or not video_prompt.strip():
|
| 49 |
return None
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
with gr.Blocks(title="Knowledge AI Studio", analytics_enabled=False) as demo:
|
| 60 |
gr.Markdown("# 📖 Knowledge AI Studio")
|
|
|
|
| 15 |
"strictly maintaining pack lore and narrative continuity."
|
| 16 |
)
|
| 17 |
|
| 18 |
+
def extract_text(content):
|
| 19 |
+
"""Safely extracts plain text from Gradio 6 structured content blocks or strings."""
|
| 20 |
+
if isinstance(content, str):
|
| 21 |
+
return content
|
| 22 |
+
if isinstance(content, list):
|
| 23 |
+
return " ".join(
|
| 24 |
+
block.get("text", "") for block in content if isinstance(block, dict) and block.get("type") == "text"
|
| 25 |
+
)
|
| 26 |
+
return str(content)
|
| 27 |
+
|
| 28 |
def write_chapter(prompt, history, temp):
|
| 29 |
if not prompt or not prompt.strip():
|
| 30 |
return "Please enter a scene prompt or chapter instruction."
|
| 31 |
+
|
| 32 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 33 |
+
for msg in history:
|
| 34 |
+
if isinstance(msg, dict) and "role" in msg and "content" in msg:
|
| 35 |
+
text = extract_text(msg["content"])
|
| 36 |
+
if text:
|
| 37 |
+
messages.append({"role": msg["role"], "content": text})
|
| 38 |
messages.append({"role": "user", "content": prompt})
|
| 39 |
|
| 40 |
try:
|
|
|
|
| 44 |
max_tokens=2048,
|
| 45 |
temperature=temp,
|
| 46 |
)
|
| 47 |
+
return response.choices[0].message.content
|
| 48 |
except Exception as e:
|
| 49 |
return f"⚠️ Notice: {str(e)}"
|
| 50 |
|
| 51 |
def generate_scene_art(scene_prompt):
|
| 52 |
if not scene_prompt or not scene_prompt.strip():
|
| 53 |
return None
|
| 54 |
+
try:
|
| 55 |
+
image = client.text_to_image(
|
| 56 |
+
prompt=f"Nobel literary book cover illustration, werewolf lore, cinematic natural lighting, clean painterly style, {scene_prompt}",
|
| 57 |
+
model="black-forest-labs/FLUX.1-schnell",
|
| 58 |
+
)
|
| 59 |
+
return image
|
| 60 |
+
except Exception as e:
|
| 61 |
+
raise gr.Error(f"Artwork generation notice: {str(e)}")
|
| 62 |
|
| 63 |
def generate_book_trailer(video_prompt):
|
| 64 |
if not video_prompt or not video_prompt.strip():
|
| 65 |
return None
|
| 66 |
+
try:
|
| 67 |
+
video_bytes = client.text_to_video(
|
| 68 |
+
prompt=f"Cinematic literary novel book trailer shot, luminous atmosphere, {video_prompt}",
|
| 69 |
+
model="Wan-AI/Wan2.1-T2V-14B",
|
| 70 |
+
)
|
| 71 |
+
output_path = "trailer_clip.mp4"
|
| 72 |
+
with open(output_path, "wb") as f:
|
| 73 |
+
f.write(video_bytes)
|
| 74 |
+
return output_path
|
| 75 |
+
except Exception as e:
|
| 76 |
+
raise gr.Error(f"Video generation notice: {str(e)}")
|
| 77 |
|
| 78 |
with gr.Blocks(title="Knowledge AI Studio", analytics_enabled=False) as demo:
|
| 79 |
gr.Markdown("# 📖 Knowledge AI Studio")
|