Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,106 +8,96 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
8 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
9 |
MAX_SEED = 2**32 - 1
|
10 |
|
11 |
-
# --- Model lists ---
|
12 |
image_models = {
|
13 |
-
"Stable Diffusion 1.5": "runwayml/stable-diffusion-v1-5",
|
14 |
"Stable Diffusion 2.1": "stabilityai/stable-diffusion-2-1",
|
15 |
-
"
|
16 |
"Playground v2": "playgroundai/playground-v2-1024px-aesthetic",
|
17 |
-
"
|
18 |
"PixArt": "PixArt-alpha/PixArt-LCM-XL-2-1024-MS",
|
|
|
19 |
"BLIP Diffusion": "Salesforce/blipdiffusion",
|
20 |
-
"
|
21 |
-
"
|
22 |
-
"OpenJourney": "prompthero/openjourney"
|
23 |
-
}
|
24 |
-
|
25 |
-
video_models = {
|
26 |
-
"AnimateDiff": "animate-diff/animate-diff",
|
27 |
-
"CogVideoX-5b": "THUDM/CogVideoX-5b",
|
28 |
-
"HunyuanVideo": "tencent/HunyuanVideo",
|
29 |
-
"LTX-Video": "Lightricks/LTX-Video",
|
30 |
-
"ModelScope T2V": "damo-vilab/modelscope-text-to-video-synthesis",
|
31 |
-
"VideoCrafter": "videocrafter/videocrafter",
|
32 |
-
"Mochi-1": "mochi/mochi-1",
|
33 |
-
"Allegro": "allegro/allegro",
|
34 |
-
"OpenSora": "LanguageBind/Open-Sora-Plan-v1.2.0",
|
35 |
-
"Zer0Scope": "zero-scope/zero-scope"
|
36 |
}
|
37 |
|
38 |
text_models = {
|
39 |
-
"GPT-2": "gpt2",
|
40 |
"GPT-Neo 1.3B": "EleutherAI/gpt-neo-1.3B",
|
41 |
-
"GPT-J 6B": "EleutherAI/gpt-j-6B",
|
42 |
"BLOOM 1.1B": "bigscience/bloom-1b1",
|
|
|
43 |
"Falcon 7B": "tiiuae/falcon-7b",
|
44 |
-
"MPT 7B": "mosaicml/mpt-7b",
|
45 |
-
"LLaMA 2 7B": "meta-llama/Llama-2-7b-hf",
|
46 |
-
"BTLM 3B": "cerebras/btlm-3b-8k-base",
|
47 |
"XGen 7B": "Salesforce/xgen-7b-8k-base",
|
48 |
-
"
|
|
|
|
|
|
|
49 |
}
|
50 |
|
51 |
-
#
|
52 |
image_pipes = {}
|
53 |
text_pipes = {}
|
54 |
|
55 |
-
|
56 |
-
def generate_image(prompt, model_name, seed, randomize_seed):
|
57 |
if randomize_seed:
|
58 |
seed = random.randint(0, MAX_SEED)
|
59 |
generator = torch.manual_seed(seed)
|
60 |
|
|
|
61 |
if model_name not in image_pipes:
|
62 |
image_pipes[model_name] = DiffusionPipeline.from_pretrained(
|
63 |
image_models[model_name],
|
64 |
torch_dtype=torch_dtype
|
65 |
).to(device)
|
66 |
-
|
67 |
pipe = image_pipes[model_name]
|
68 |
-
image = pipe(prompt=prompt, generator=generator, num_inference_steps=25, width=512, height=512).images[0]
|
69 |
-
return image, seed
|
70 |
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
if model_name not in text_pipes:
|
73 |
text_pipes[model_name] = pipeline("text-generation", model=text_models[model_name], device=0 if device == "cuda" else -1)
|
74 |
pipe = text_pipes[model_name]
|
75 |
-
output = pipe(prompt, max_length=100, do_sample=True)[0]['generated_text']
|
76 |
-
return output
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
81 |
|
82 |
-
#
|
83 |
with gr.Blocks() as demo:
|
84 |
-
gr.Markdown("#
|
85 |
|
86 |
with gr.Tabs():
|
87 |
-
#
|
88 |
-
with gr.Tab("πΌοΈ Image"):
|
89 |
img_prompt = gr.Textbox(label="Prompt")
|
90 |
-
img_model = gr.Dropdown(choices=list(image_models.keys()), value="Stable Diffusion 1.5", label="
|
91 |
img_seed = gr.Slider(0, MAX_SEED, value=42, label="Seed")
|
92 |
img_rand = gr.Checkbox(label="Randomize seed", value=True)
|
93 |
img_btn = gr.Button("Generate Image")
|
94 |
img_out = gr.Image()
|
95 |
img_btn.click(fn=generate_image, inputs=[img_prompt, img_model, img_seed, img_rand], outputs=[img_out, img_seed])
|
96 |
|
97 |
-
#
|
98 |
-
with gr.Tab("
|
99 |
-
vid_prompt = gr.Textbox(label="Prompt")
|
100 |
-
vid_model = gr.Dropdown(choices=list(video_models.keys()), value="AnimateDiff", label="Select Video Model")
|
101 |
-
vid_btn = gr.Button("Generate Video")
|
102 |
-
vid_out = gr.Textbox(label="Result (Placeholder)")
|
103 |
-
vid_btn.click(fn=generate_video, inputs=[vid_prompt, vid_model], outputs=vid_out)
|
104 |
-
|
105 |
-
# Tab 3: Text Generation
|
106 |
-
with gr.Tab("π Text"):
|
107 |
txt_prompt = gr.Textbox(label="Prompt")
|
108 |
-
txt_model = gr.Dropdown(choices=list(text_models.keys()), value="GPT-2", label="
|
109 |
txt_btn = gr.Button("Generate Text")
|
110 |
-
txt_out = gr.Textbox(label="
|
111 |
txt_btn.click(fn=generate_text, inputs=[txt_prompt, txt_model], outputs=txt_out)
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
demo.launch(show_error=True)
|
|
|
8 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
9 |
MAX_SEED = 2**32 - 1
|
10 |
|
11 |
+
# --- Model lists ordered by size (light to heavy) ---
|
12 |
image_models = {
|
13 |
+
"Stable Diffusion 1.5 (light)": "runwayml/stable-diffusion-v1-5",
|
14 |
"Stable Diffusion 2.1": "stabilityai/stable-diffusion-2-1",
|
15 |
+
"Dreamlike 2.0": "dreamlike-art/dreamlike-photoreal-2.0",
|
16 |
"Playground v2": "playgroundai/playground-v2-1024px-aesthetic",
|
17 |
+
"Muse 512": "amused/muse-512-finetuned",
|
18 |
"PixArt": "PixArt-alpha/PixArt-LCM-XL-2-1024-MS",
|
19 |
+
"Kandinsky 3": "kandinsky-community/kandinsky-3",
|
20 |
"BLIP Diffusion": "Salesforce/blipdiffusion",
|
21 |
+
"SDXL Base 1.0 (heavy)": "stabilityai/stable-diffusion-xl-base-1.0",
|
22 |
+
"OpenJourney (heavy)": "prompthero/openjourney"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
}
|
24 |
|
25 |
text_models = {
|
26 |
+
"GPT-2 (light)": "gpt2",
|
27 |
"GPT-Neo 1.3B": "EleutherAI/gpt-neo-1.3B",
|
|
|
28 |
"BLOOM 1.1B": "bigscience/bloom-1b1",
|
29 |
+
"GPT-J 6B": "EleutherAI/gpt-j-6B",
|
30 |
"Falcon 7B": "tiiuae/falcon-7b",
|
|
|
|
|
|
|
31 |
"XGen 7B": "Salesforce/xgen-7b-8k-base",
|
32 |
+
"BTLM 3B": "cerebras/btlm-3b-8k-base",
|
33 |
+
"MPT 7B": "mosaicml/mpt-7b",
|
34 |
+
"StableLM 2": "stabilityai/stablelm-2-1_6b",
|
35 |
+
"LLaMA 2 7B (heavy)": "meta-llama/Llama-2-7b-hf"
|
36 |
}
|
37 |
|
38 |
+
# Cache
|
39 |
image_pipes = {}
|
40 |
text_pipes = {}
|
41 |
|
42 |
+
def generate_image(prompt, model_name, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
|
|
|
43 |
if randomize_seed:
|
44 |
seed = random.randint(0, MAX_SEED)
|
45 |
generator = torch.manual_seed(seed)
|
46 |
|
47 |
+
progress(0, desc="Loading model...")
|
48 |
if model_name not in image_pipes:
|
49 |
image_pipes[model_name] = DiffusionPipeline.from_pretrained(
|
50 |
image_models[model_name],
|
51 |
torch_dtype=torch_dtype
|
52 |
).to(device)
|
|
|
53 |
pipe = image_pipes[model_name]
|
|
|
|
|
54 |
|
55 |
+
progress(25, desc="Running inference (step 1/3)...")
|
56 |
+
result = pipe(prompt=prompt, generator=generator, num_inference_steps=30, width=512, height=512)
|
57 |
+
|
58 |
+
progress(100, desc="Done.")
|
59 |
+
return result.images[0], seed
|
60 |
+
|
61 |
+
def generate_text(prompt, model_name, progress=gr.Progress(track_tqdm=True)):
|
62 |
+
progress(0, desc="Loading model...")
|
63 |
if model_name not in text_pipes:
|
64 |
text_pipes[model_name] = pipeline("text-generation", model=text_models[model_name], device=0 if device == "cuda" else -1)
|
65 |
pipe = text_pipes[model_name]
|
|
|
|
|
66 |
|
67 |
+
progress(50, desc="Generating text...")
|
68 |
+
result = pipe(prompt, max_length=100, do_sample=True)[0]['generated_text']
|
69 |
+
progress(100, desc="Done.")
|
70 |
+
return result
|
71 |
|
72 |
+
# Gradio Interface
|
73 |
with gr.Blocks() as demo:
|
74 |
+
gr.Markdown("# π§ Multi-Model AI Playground with Progress")
|
75 |
|
76 |
with gr.Tabs():
|
77 |
+
# πΌοΈ Image Gen Tab
|
78 |
+
with gr.Tab("πΌοΈ Image Generation"):
|
79 |
img_prompt = gr.Textbox(label="Prompt")
|
80 |
+
img_model = gr.Dropdown(choices=list(image_models.keys()), value="Stable Diffusion 1.5 (light)", label="Image Model")
|
81 |
img_seed = gr.Slider(0, MAX_SEED, value=42, label="Seed")
|
82 |
img_rand = gr.Checkbox(label="Randomize seed", value=True)
|
83 |
img_btn = gr.Button("Generate Image")
|
84 |
img_out = gr.Image()
|
85 |
img_btn.click(fn=generate_image, inputs=[img_prompt, img_model, img_seed, img_rand], outputs=[img_out, img_seed])
|
86 |
|
87 |
+
# π Text Gen Tab
|
88 |
+
with gr.Tab("π Text Generation"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
txt_prompt = gr.Textbox(label="Prompt")
|
90 |
+
txt_model = gr.Dropdown(choices=list(text_models.keys()), value="GPT-2 (light)", label="Text Model")
|
91 |
txt_btn = gr.Button("Generate Text")
|
92 |
+
txt_out = gr.Textbox(label="Output Text")
|
93 |
txt_btn.click(fn=generate_text, inputs=[txt_prompt, txt_model], outputs=txt_out)
|
94 |
|
95 |
+
# π₯ Video Gen Tab (placeholder)
|
96 |
+
with gr.Tab("π₯ Video Generation (Placeholder)"):
|
97 |
+
gr.Markdown("β οΈ Video generation is placeholder only. Models require special setup.")
|
98 |
+
vid_prompt = gr.Textbox(label="Prompt")
|
99 |
+
vid_btn = gr.Button("Pretend to Generate")
|
100 |
+
vid_out = gr.Textbox(label="Result")
|
101 |
+
vid_btn.click(lambda x: f"Fake video output for: {x}", inputs=[vid_prompt], outputs=[vid_out])
|
102 |
+
|
103 |
demo.launch(show_error=True)
|