Spaces:
Paused
Paused
all app and sentencepiecesentencepiece
Browse files- README.md +1 -1
- app.py +0 -123
- requirements.txt +1 -0
README.md
CHANGED
|
@@ -6,7 +6,7 @@ colorTo: purple
|
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.23.3
|
| 8 |
python_version: 3.10
|
| 9 |
-
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
short_description: 'Unofficial HiDream-ai Spaces '
|
| 12 |
---
|
|
|
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.23.3
|
| 8 |
python_version: 3.10
|
| 9 |
+
app_file: app-dev.py
|
| 10 |
pinned: false
|
| 11 |
short_description: 'Unofficial HiDream-ai Spaces '
|
| 12 |
---
|
app.py
DELETED
|
@@ -1,123 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import PIL
|
| 3 |
-
import spaces
|
| 4 |
-
import torch
|
| 5 |
-
from hi_diffusers import HiDreamImagePipeline, HiDreamImageTransformer2DModel
|
| 6 |
-
from hi_diffusers.schedulers.flash_flow_match import (
|
| 7 |
-
FlashFlowMatchEulerDiscreteScheduler,
|
| 8 |
-
)
|
| 9 |
-
from transformers import AutoTokenizer, LlamaForCausalLM
|
| 10 |
-
|
| 11 |
-
# Constants
|
| 12 |
-
MODEL_PREFIX: str = "HiDream-ai"
|
| 13 |
-
LLAMA_MODEL_NAME: str = "meta-llama/Meta-Llama-3.1-8B-Instruct"
|
| 14 |
-
MODEL_PATH = "HiDream-ai/HiDream-I1-Dev"
|
| 15 |
-
MODEL_CONFIGS = {
|
| 16 |
-
"guidance_scale": 0.0,
|
| 17 |
-
"num_inference_steps": 28,
|
| 18 |
-
"shift": 6.0,
|
| 19 |
-
"scheduler": FlashFlowMatchEulerDiscreteScheduler,
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Supported image sizes
|
| 24 |
-
RESOLUTION_OPTIONS: list[str] = [
|
| 25 |
-
"1024 x 1024 (Square)",
|
| 26 |
-
"768 x 1360 (Portrait)",
|
| 27 |
-
"1360 x 768 (Landscape)",
|
| 28 |
-
"880 x 1168 (Portrait)",
|
| 29 |
-
"1168 x 880 (Landscape)",
|
| 30 |
-
"1248 x 832 (Landscape)",
|
| 31 |
-
"832 x 1248 (Portrait)",
|
| 32 |
-
]
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
tokenizer = AutoTokenizer.from_pretrained(LLAMA_MODEL_NAME, use_fast=False)
|
| 36 |
-
text_encoder = LlamaForCausalLM.from_pretrained(
|
| 37 |
-
LLAMA_MODEL_NAME,
|
| 38 |
-
output_hidden_states=True,
|
| 39 |
-
output_attentions=True,
|
| 40 |
-
torch_dtype=torch.bfloat16,
|
| 41 |
-
).to("cuda")
|
| 42 |
-
|
| 43 |
-
transformer = HiDreamImageTransformer2DModel.from_pretrained(
|
| 44 |
-
MODEL_PATH,
|
| 45 |
-
subfolder="transformer",
|
| 46 |
-
torch_dtype=torch.bfloat16,
|
| 47 |
-
).to("cuda")
|
| 48 |
-
|
| 49 |
-
scheduler = MODEL_CONFIGS["scheduler"](
|
| 50 |
-
num_train_timesteps=1000,
|
| 51 |
-
shift=MODEL_CONFIGS["shift"],
|
| 52 |
-
use_dynamic_shifting=False,
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
pipe = HiDreamImagePipeline.from_pretrained(
|
| 56 |
-
MODEL_PATH,
|
| 57 |
-
scheduler=scheduler,
|
| 58 |
-
tokenizer_4=tokenizer,
|
| 59 |
-
text_encoder_4=text_encoder,
|
| 60 |
-
torch_dtype=torch.bfloat16,
|
| 61 |
-
).to("cuda", torch.bfloat16)
|
| 62 |
-
|
| 63 |
-
pipe.transformer = transformer
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
@spaces.GPU(duration=90)
|
| 67 |
-
def generate_image(
|
| 68 |
-
prompt: str,
|
| 69 |
-
resolution: str,
|
| 70 |
-
seed: int,
|
| 71 |
-
) -> tuple[PIL.Image.Image, int]:
|
| 72 |
-
if seed == -1:
|
| 73 |
-
seed = torch.randint(0, 1_000_000, (1,)).item()
|
| 74 |
-
|
| 75 |
-
height, width = tuple(map(int, resolution.replace(" ", "").split("x")))
|
| 76 |
-
generator = torch.Generator("cuda").manual_seed(seed)
|
| 77 |
-
|
| 78 |
-
image = pipe(
|
| 79 |
-
prompt=prompt,
|
| 80 |
-
height=height,
|
| 81 |
-
width=width,
|
| 82 |
-
guidance_scale=MODEL_CONFIGS["guidance_scale"],
|
| 83 |
-
num_inference_steps=MODEL_CONFIGS["num_inference_steps"],
|
| 84 |
-
generator=generator,
|
| 85 |
-
).images[0]
|
| 86 |
-
|
| 87 |
-
torch.cuda.empty_cache()
|
| 88 |
-
return image, seed
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
# Gradio UI
|
| 92 |
-
with gr.Blocks(title="HiDream Image Generator") as demo:
|
| 93 |
-
gr.Markdown("## 🌈 HiDream Image Generator")
|
| 94 |
-
|
| 95 |
-
with gr.Row():
|
| 96 |
-
with gr.Column():
|
| 97 |
-
prompt = gr.Textbox(
|
| 98 |
-
label="Prompt",
|
| 99 |
-
placeholder="e.g. A futuristic city with floating cars at sunset",
|
| 100 |
-
lines=3,
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
resolution = gr.Radio(
|
| 104 |
-
choices=RESOLUTION_OPTIONS,
|
| 105 |
-
value=RESOLUTION_OPTIONS[0],
|
| 106 |
-
label="Resolution",
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
seed = gr.Number(label="Seed (-1 for random)", value=-1, precision=0)
|
| 110 |
-
generate_btn = gr.Button("Generate Image", variant="primary")
|
| 111 |
-
seed_used = gr.Number(label="Seed Used", interactive=False)
|
| 112 |
-
|
| 113 |
-
with gr.Column():
|
| 114 |
-
output_image = gr.Image(label="Generated Image", type="pil")
|
| 115 |
-
|
| 116 |
-
generate_btn.click(
|
| 117 |
-
fn=generate_image,
|
| 118 |
-
inputs=[prompt, resolution, seed],
|
| 119 |
-
outputs=[output_image, seed_used],
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
if __name__ == "__main__":
|
| 123 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -9,4 +9,5 @@ https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.0.post2/flas
|
|
| 9 |
einops
|
| 10 |
gradio
|
| 11 |
spaces
|
|
|
|
| 12 |
|
|
|
|
| 9 |
einops
|
| 10 |
gradio
|
| 11 |
spaces
|
| 12 |
+
sentencepiece
|
| 13 |
|