Spaces:
Runtime error
Runtime error
Updated app.py _pycache
Browse files- __pycache__/live_preview_helpers.cpython-310.pyc +0 -0
- app.py +43 -40
__pycache__/live_preview_helpers.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/live_preview_helpers.cpython-310.pyc and b/__pycache__/live_preview_helpers.cpython-310.pyc differ
|
|
|
app.py
CHANGED
|
@@ -6,15 +6,17 @@ from live_preview_helpers import (
|
|
| 6 |
flux_pipe_call_that_returns_an_iterable_of_images as flux_iter,
|
| 7 |
)
|
| 8 |
|
| 9 |
-
# Device selection
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# PUBLIC Stable Diffusion pipeline setup
|
| 13 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 14 |
-
"runwayml/stable-diffusion-v1-5", torch_dtype=
|
| 15 |
).to(device)
|
| 16 |
good_vae = AutoencoderKL.from_pretrained(
|
| 17 |
-
"runwayml/stable-diffusion-v1-5", subfolder="vae", torch_dtype=
|
| 18 |
).to(device)
|
| 19 |
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_iter.__get__(pipe)
|
| 20 |
|
|
@@ -23,6 +25,7 @@ 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:
|
|
@@ -33,22 +36,29 @@ def first_live_space(space_ids):
|
|
| 33 |
return c
|
| 34 |
except Exception as e:
|
| 35 |
print(f"[warn] {sid} unusable → {e}")
|
| 36 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
)
|
|
@@ -73,17 +83,10 @@ def generate_character(world_desc, persona_desc, progress=gr.Progress(track_tqdm
|
|
| 73 |
)
|
| 74 |
try:
|
| 75 |
return json.loads(raw)
|
| 76 |
-
except
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
persona_description=persona_desc,
|
| 80 |
-
world_description=world_desc,
|
| 81 |
-
),
|
| 82 |
-
max_tokens=1024,
|
| 83 |
-
)
|
| 84 |
-
return json.loads(raw)
|
| 85 |
|
| 86 |
-
# Chaining (connected characters)
|
| 87 |
CHAIN_PROMPT_TEMPLATE = """
|
| 88 |
You are crafting an interconnected character ensemble for a shared world.
|
| 89 |
|
|
@@ -122,15 +125,11 @@ def generate_connected_characters(world_desc, persona_desc, progress=gr.Progress
|
|
| 122 |
)
|
| 123 |
try:
|
| 124 |
return json.loads(raw)
|
| 125 |
-
except
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
),
|
| 131 |
-
max_tokens=2048
|
| 132 |
-
)
|
| 133 |
-
return json.loads(raw)
|
| 134 |
|
| 135 |
# Gradio UI
|
| 136 |
DESCRIPTION = """
|
|
@@ -161,18 +160,22 @@ with gr.Blocks(title="Connected Character Chain Generator", theme="Nymbo/Nymbo_T
|
|
| 161 |
json_out = gr.JSON(label="Character Description")
|
| 162 |
chained_out = gr.JSON(label="Connected Characters (Protagonist, Ally, Nemesis)")
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
btn_generate.click(
|
| 173 |
generate_character, [world_tb, persona_tb], [json_out]
|
| 174 |
).then(
|
| 175 |
-
|
| 176 |
)
|
| 177 |
btn_chain.click(
|
| 178 |
generate_connected_characters, [world_tb, persona_tb], [chained_out]
|
|
|
|
| 6 |
flux_pipe_call_that_returns_an_iterable_of_images as flux_iter,
|
| 7 |
)
|
| 8 |
|
| 9 |
+
# Device and dtype selection
|
| 10 |
+
USE_CUDA = torch.cuda.is_available()
|
| 11 |
+
DTYPE = torch.float16 if USE_CUDA else torch.float32
|
| 12 |
+
device = torch.device("cuda" if USE_CUDA else "cpu")
|
| 13 |
|
| 14 |
# PUBLIC Stable Diffusion pipeline setup
|
| 15 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 16 |
+
"runwayml/stable-diffusion-v1-5", torch_dtype=DTYPE
|
| 17 |
).to(device)
|
| 18 |
good_vae = AutoencoderKL.from_pretrained(
|
| 19 |
+
"runwayml/stable-diffusion-v1-5", subfolder="vae", torch_dtype=DTYPE
|
| 20 |
).to(device)
|
| 21 |
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_iter.__get__(pipe)
|
| 22 |
|
|
|
|
| 25 |
"https://huggingfaceh4-zephyr-chat.hf.space",
|
| 26 |
"huggingface-projects/gemma-2-9b-it",
|
| 27 |
]
|
| 28 |
+
|
| 29 |
def first_live_space(space_ids):
|
| 30 |
for sid in space_ids:
|
| 31 |
try:
|
|
|
|
| 36 |
return c
|
| 37 |
except Exception as e:
|
| 38 |
print(f"[warn] {sid} unusable → {e}")
|
| 39 |
+
print("[warn] No live chat Space found; falling back to local responses.")
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
llm_client = first_live_space(LLM_SPACES)
|
| 43 |
CHAT_API = "/chat"
|
| 44 |
+
|
| 45 |
def call_llm(prompt, max_tokens=256, temperature=0.6, top_p=0.9):
|
| 46 |
+
if llm_client is not None:
|
| 47 |
+
try:
|
| 48 |
+
return llm_client.predict(
|
| 49 |
+
prompt, max_tokens, temperature, top_p, api_name=CHAT_API
|
| 50 |
+
).strip()
|
| 51 |
+
except Exception as exc:
|
| 52 |
+
print(f"[error] LLM failure → {exc}")
|
| 53 |
+
# Local fallback if no LLM API available
|
| 54 |
+
print("[warn] Returning local fallback response.")
|
| 55 |
+
return "No LLM API available. Please enter your own text."
|
| 56 |
|
| 57 |
# Datasets and prompt templates
|
| 58 |
ds = load_dataset("MohamedRashad/FinePersonas-Lite", split="train")
|
| 59 |
def random_persona():
|
| 60 |
return ds[random.randint(0, len(ds) - 1)]["persona"]
|
| 61 |
+
|
| 62 |
WORLD_PROMPT = (
|
| 63 |
"Invent a short, unique and vivid world description. Respond with the description only."
|
| 64 |
)
|
|
|
|
| 83 |
)
|
| 84 |
try:
|
| 85 |
return json.loads(raw)
|
| 86 |
+
except Exception:
|
| 87 |
+
# Fallback for user input/manual override
|
| 88 |
+
return {"name": "Unnamed", "appearance": "Manual entry required."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
|
|
|
| 90 |
CHAIN_PROMPT_TEMPLATE = """
|
| 91 |
You are crafting an interconnected character ensemble for a shared world.
|
| 92 |
|
|
|
|
| 125 |
)
|
| 126 |
try:
|
| 127 |
return json.loads(raw)
|
| 128 |
+
except Exception:
|
| 129 |
+
# Fallback for user input/manual override
|
| 130 |
+
return [{"name": "Unnamed Protagonist", "role": "protagonist"},
|
| 131 |
+
{"name": "Unnamed Ally", "role": "ally"},
|
| 132 |
+
{"name": "Unnamed Nemesis", "role": "nemesis"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
# Gradio UI
|
| 135 |
DESCRIPTION = """
|
|
|
|
| 160 |
json_out = gr.JSON(label="Character Description")
|
| 161 |
chained_out = gr.JSON(label="Connected Characters (Protagonist, Ally, Nemesis)")
|
| 162 |
|
| 163 |
+
def sd_image_from_character(character):
|
| 164 |
+
# Use appearance or fallback if needed
|
| 165 |
+
prompt = character.get("appearance", "A unique portrait, digital art, fantasy character, 4k")
|
| 166 |
+
return next(pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
| 167 |
+
prompt=prompt,
|
| 168 |
+
guidance_scale=7.5,
|
| 169 |
+
num_inference_steps=25,
|
| 170 |
+
width=512,
|
| 171 |
+
height=512,
|
| 172 |
+
output_type="pil"
|
| 173 |
+
))
|
| 174 |
+
|
| 175 |
btn_generate.click(
|
| 176 |
generate_character, [world_tb, persona_tb], [json_out]
|
| 177 |
).then(
|
| 178 |
+
sd_image_from_character, [json_out], [img_out]
|
| 179 |
)
|
| 180 |
btn_chain.click(
|
| 181 |
generate_connected_characters, [world_tb, persona_tb], [chained_out]
|