Spaces:
Runtime error
Runtime error
updated app.py requirements.txt and live_preview_helpers
Browse files- app.py +25 -18
- live_preview_helpers.py +14 -20
- requirements.txt +10 -6
app.py
CHANGED
|
@@ -1,26 +1,29 @@
|
|
| 1 |
from datasets import load_dataset
|
| 2 |
import gradio as gr, json, os, random, torch, spaces
|
| 3 |
-
from diffusers import
|
| 4 |
from gradio_client import Client
|
| 5 |
from live_preview_helpers import (
|
| 6 |
flux_pipe_call_that_returns_an_iterable_of_images as flux_iter,
|
| 7 |
)
|
| 8 |
|
|
|
|
| 9 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
).to(device)
|
| 13 |
good_vae = AutoencoderKL.from_pretrained(
|
| 14 |
-
"
|
| 15 |
).to(device)
|
| 16 |
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_iter.__get__(pipe)
|
| 17 |
|
|
|
|
| 18 |
LLM_SPACES = [
|
| 19 |
"https://huggingfaceh4-zephyr-chat.hf.space",
|
| 20 |
"huggingface-projects/gemma-2-9b-it",
|
| 21 |
]
|
| 22 |
-
|
| 23 |
-
def first_live_space(space_ids: list[str]) -> Client:
|
| 24 |
for sid in space_ids:
|
| 25 |
try:
|
| 26 |
print(f"[info] probing {sid}")
|
|
@@ -31,11 +34,9 @@ def first_live_space(space_ids: list[str]) -> Client:
|
|
| 31 |
except Exception as e:
|
| 32 |
print(f"[warn] {sid} unusable → {e}")
|
| 33 |
raise RuntimeError("No live chat Space found!")
|
| 34 |
-
|
| 35 |
llm_client = first_live_space(LLM_SPACES)
|
| 36 |
-
CHAT_API
|
| 37 |
-
|
| 38 |
-
def call_llm(prompt: str, max_tokens: int = 256, temperature: float = 0.6, top_p: float = 0.9) -> str:
|
| 39 |
try:
|
| 40 |
return llm_client.predict(
|
| 41 |
prompt, max_tokens, temperature, top_p, api_name=CHAT_API
|
|
@@ -46,16 +47,14 @@ def call_llm(prompt: str, max_tokens: int = 256, temperature: float = 0.6, top_p
|
|
| 46 |
|
| 47 |
# Datasets and prompt templates
|
| 48 |
ds = load_dataset("MohamedRashad/FinePersonas-Lite", split="train")
|
| 49 |
-
def random_persona()
|
| 50 |
return ds[random.randint(0, len(ds) - 1)]["persona"]
|
| 51 |
-
|
| 52 |
WORLD_PROMPT = (
|
| 53 |
"Invent a short, unique and vivid world description. Respond with the description only."
|
| 54 |
)
|
| 55 |
-
def random_world()
|
| 56 |
return call_llm(WORLD_PROMPT, max_tokens=120)
|
| 57 |
|
| 58 |
-
# Standard single character prompt (optional)
|
| 59 |
PROMPT_TEMPLATE = """Generate a character with this persona description:
|
| 60 |
{persona_description}
|
| 61 |
In a world with this description:
|
|
@@ -64,7 +63,7 @@ Write the character in JSON with keys:
|
|
| 64 |
name, background, appearance, personality, skills_and_abilities, goals, conflicts, backstory, current_situation, spoken_lines (list of strings).
|
| 65 |
Respond with JSON only (no markdown)."""
|
| 66 |
|
| 67 |
-
def generate_character(world_desc
|
| 68 |
raw = call_llm(
|
| 69 |
PROMPT_TEMPLATE.format(
|
| 70 |
persona_description=persona_desc,
|
|
@@ -113,7 +112,7 @@ Each character must include:
|
|
| 113 |
Respond with pure JSON array.
|
| 114 |
"""
|
| 115 |
|
| 116 |
-
def generate_connected_characters(world_desc
|
| 117 |
raw = call_llm(
|
| 118 |
CHAIN_PROMPT_TEMPLATE.format(
|
| 119 |
world_description=world_desc,
|
|
@@ -136,7 +135,7 @@ def generate_connected_characters(world_desc: str, persona_desc: str, progress=g
|
|
| 136 |
# Gradio UI
|
| 137 |
DESCRIPTION = """
|
| 138 |
* Generates a trio of connected character sheets for a world + persona.
|
| 139 |
-
* Images via **
|
| 140 |
* Personas sampled from **FinePersonas-Lite**.
|
| 141 |
Tip → Shuffle the world then persona for rapid inspiration.
|
| 142 |
"""
|
|
@@ -162,10 +161,18 @@ with gr.Blocks(title="Connected Character Chain Generator", theme="Nymbo/Nymbo_T
|
|
| 162 |
json_out = gr.JSON(label="Character Description")
|
| 163 |
chained_out = gr.JSON(label="Connected Characters (Protagonist, Ally, Nemesis)")
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
btn_generate.click(
|
| 166 |
generate_character, [world_tb, persona_tb], [json_out]
|
| 167 |
).then(
|
| 168 |
-
|
| 169 |
)
|
| 170 |
btn_chain.click(
|
| 171 |
generate_connected_characters, [world_tb, persona_tb], [chained_out]
|
|
|
|
| 1 |
from datasets import load_dataset
|
| 2 |
import gradio as gr, json, os, random, torch, spaces
|
| 3 |
+
from diffusers import StableDiffusionPipeline, AutoencoderKL
|
| 4 |
from gradio_client import Client
|
| 5 |
from live_preview_helpers import (
|
| 6 |
flux_pipe_call_that_returns_an_iterable_of_images as flux_iter,
|
| 7 |
)
|
| 8 |
|
| 9 |
+
# Device selection
|
| 10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
# PUBLIC Stable Diffusion pipeline setup
|
| 13 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 14 |
+
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
|
| 15 |
).to(device)
|
| 16 |
good_vae = AutoencoderKL.from_pretrained(
|
| 17 |
+
"runwayml/stable-diffusion-v1-5", subfolder="vae", torch_dtype=torch.float16
|
| 18 |
).to(device)
|
| 19 |
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_iter.__get__(pipe)
|
| 20 |
|
| 21 |
+
# LLM client config (Zephyr or Gemma fallback)
|
| 22 |
LLM_SPACES = [
|
| 23 |
"https://huggingfaceh4-zephyr-chat.hf.space",
|
| 24 |
"huggingface-projects/gemma-2-9b-it",
|
| 25 |
]
|
| 26 |
+
def first_live_space(space_ids):
|
|
|
|
| 27 |
for sid in space_ids:
|
| 28 |
try:
|
| 29 |
print(f"[info] probing {sid}")
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
print(f"[warn] {sid} unusable → {e}")
|
| 36 |
raise RuntimeError("No live chat Space found!")
|
|
|
|
| 37 |
llm_client = first_live_space(LLM_SPACES)
|
| 38 |
+
CHAT_API = "/chat"
|
| 39 |
+
def call_llm(prompt, max_tokens=256, temperature=0.6, top_p=0.9):
|
|
|
|
| 40 |
try:
|
| 41 |
return llm_client.predict(
|
| 42 |
prompt, max_tokens, temperature, top_p, api_name=CHAT_API
|
|
|
|
| 47 |
|
| 48 |
# Datasets and prompt templates
|
| 49 |
ds = load_dataset("MohamedRashad/FinePersonas-Lite", split="train")
|
| 50 |
+
def random_persona():
|
| 51 |
return ds[random.randint(0, len(ds) - 1)]["persona"]
|
|
|
|
| 52 |
WORLD_PROMPT = (
|
| 53 |
"Invent a short, unique and vivid world description. Respond with the description only."
|
| 54 |
)
|
| 55 |
+
def random_world():
|
| 56 |
return call_llm(WORLD_PROMPT, max_tokens=120)
|
| 57 |
|
|
|
|
| 58 |
PROMPT_TEMPLATE = """Generate a character with this persona description:
|
| 59 |
{persona_description}
|
| 60 |
In a world with this description:
|
|
|
|
| 63 |
name, background, appearance, personality, skills_and_abilities, goals, conflicts, backstory, current_situation, spoken_lines (list of strings).
|
| 64 |
Respond with JSON only (no markdown)."""
|
| 65 |
|
| 66 |
+
def generate_character(world_desc, persona_desc, progress=gr.Progress(track_tqdm=True)):
|
| 67 |
raw = call_llm(
|
| 68 |
PROMPT_TEMPLATE.format(
|
| 69 |
persona_description=persona_desc,
|
|
|
|
| 112 |
Respond with pure JSON array.
|
| 113 |
"""
|
| 114 |
|
| 115 |
+
def generate_connected_characters(world_desc, persona_desc, progress=gr.Progress(track_tqdm=True)):
|
| 116 |
raw = call_llm(
|
| 117 |
CHAIN_PROMPT_TEMPLATE.format(
|
| 118 |
world_description=world_desc,
|
|
|
|
| 135 |
# Gradio UI
|
| 136 |
DESCRIPTION = """
|
| 137 |
* Generates a trio of connected character sheets for a world + persona.
|
| 138 |
+
* Images via **Stable Diffusion**; story text via Zephyr-chat or Gemma fallback.
|
| 139 |
* Personas sampled from **FinePersonas-Lite**.
|
| 140 |
Tip → Shuffle the world then persona for rapid inspiration.
|
| 141 |
"""
|
|
|
|
| 161 |
json_out = gr.JSON(label="Character Description")
|
| 162 |
chained_out = gr.JSON(label="Connected Characters (Protagonist, Ally, Nemesis)")
|
| 163 |
|
| 164 |
+
jls_extract_var = lambda character: next(pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
| 165 |
+
prompt=character["appearance"],
|
| 166 |
+
guidance_scale=7.5,
|
| 167 |
+
num_inference_steps=25,
|
| 168 |
+
width=512,
|
| 169 |
+
height=512,
|
| 170 |
+
output_type="pil"
|
| 171 |
+
))
|
| 172 |
btn_generate.click(
|
| 173 |
generate_character, [world_tb, persona_tb], [json_out]
|
| 174 |
).then(
|
| 175 |
+
jls_extract_var, [json_out], [img_out]
|
| 176 |
)
|
| 177 |
btn_chain.click(
|
| 178 |
generate_connected_characters, [world_tb, persona_tb], [chained_out]
|
live_preview_helpers.py
CHANGED
|
@@ -3,26 +3,23 @@
|
|
| 3 |
import torch
|
| 4 |
from typing import Iterator
|
| 5 |
|
|
|
|
|
|
|
| 6 |
def flux_pipe_call_that_returns_an_iterable_of_images(
|
| 7 |
self,
|
| 8 |
-
prompt
|
| 9 |
-
guidance_scale
|
| 10 |
-
num_inference_steps
|
| 11 |
-
width
|
| 12 |
-
height
|
| 13 |
generator=None,
|
| 14 |
-
output_type
|
| 15 |
-
good_vae=None
|
| 16 |
-
)
|
| 17 |
"""
|
| 18 |
-
|
| 19 |
"""
|
| 20 |
-
|
| 21 |
-
pipe = self # usually the FLUX pipeline instance
|
| 22 |
-
|
| 23 |
-
if generator is None:
|
| 24 |
-
generator = torch.Generator(device="cpu").manual_seed(0)
|
| 25 |
-
images = pipe(
|
| 26 |
prompt=prompt,
|
| 27 |
guidance_scale=guidance_scale,
|
| 28 |
num_inference_steps=num_inference_steps,
|
|
@@ -30,8 +27,5 @@ def flux_pipe_call_that_returns_an_iterable_of_images(
|
|
| 30 |
height=height,
|
| 31 |
generator=generator,
|
| 32 |
output_type=output_type,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
for img in images:
|
| 37 |
-
yield img
|
|
|
|
| 3 |
import torch
|
| 4 |
from typing import Iterator
|
| 5 |
|
| 6 |
+
# live_preview_helpers.py
|
| 7 |
+
|
| 8 |
def flux_pipe_call_that_returns_an_iterable_of_images(
|
| 9 |
self,
|
| 10 |
+
prompt,
|
| 11 |
+
guidance_scale=7.5,
|
| 12 |
+
num_inference_steps=20,
|
| 13 |
+
width=512,
|
| 14 |
+
height=512,
|
| 15 |
generator=None,
|
| 16 |
+
output_type="pil",
|
| 17 |
+
good_vae=None
|
| 18 |
+
):
|
| 19 |
"""
|
| 20 |
+
For StableDiffusion: yields a single image for now.
|
| 21 |
"""
|
| 22 |
+
image = self(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
prompt=prompt,
|
| 24 |
guidance_scale=guidance_scale,
|
| 25 |
num_inference_steps=num_inference_steps,
|
|
|
|
| 27 |
height=height,
|
| 28 |
generator=generator,
|
| 29 |
output_type=output_type,
|
| 30 |
+
).images[0]
|
| 31 |
+
yield image
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
datasets
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.21.0
|
| 2 |
+
gradio_client>=0.4.0
|
| 3 |
+
transformers>=4.39.0
|
| 4 |
+
datasets>=2.18.0
|
| 5 |
+
huggingface_hub>=0.23.0
|
| 6 |
+
accelerate>=0.27.0
|
| 7 |
+
diffusers>=0.27.0
|
| 8 |
+
torch>=2.1.0
|
| 9 |
+
Pillow>=10.0.0
|
| 10 |
+
spaces>=0.26.0
|