Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
sys.path.append("./")
|
4 |
+
|
5 |
+
import torch
|
6 |
+
from torchvision import transforms
|
7 |
+
from src.transformer import Transformer2DModel
|
8 |
+
from src.pipeline import Pipeline
|
9 |
+
from src.scheduler import Scheduler
|
10 |
+
from transformers import (
|
11 |
+
CLIPTextModelWithProjection,
|
12 |
+
CLIPTokenizer,
|
13 |
+
)
|
14 |
+
from diffusers import VQModel
|
15 |
+
import gradio as gr
|
16 |
+
|
17 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
18 |
+
|
19 |
+
def load_model():
|
20 |
+
model_path = "MeissonFlow/Meissonic"
|
21 |
+
model = Transformer2DModel.from_pretrained(model_path, subfolder="transformer")
|
22 |
+
vq_model = VQModel.from_pretrained(model_path, subfolder="vqvae")
|
23 |
+
text_encoder = CLIPTextModelWithProjection.from_pretrained(model_path, subfolder="text_encoder")
|
24 |
+
tokenizer = CLIPTokenizer.from_pretrained(model_path, subfolder="tokenizer")
|
25 |
+
scheduler = Scheduler.from_pretrained(model_path, subfolder="scheduler")
|
26 |
+
pipe = Pipeline(vq_model, tokenizer=tokenizer, text_encoder=text_encoder, transformer=model, scheduler=scheduler)
|
27 |
+
return pipe.to(device)
|
28 |
+
|
29 |
+
pipe = load_model()
|
30 |
+
|
31 |
+
def generate_image(prompt, negative_prompt, resolution, steps, cfg):
|
32 |
+
image = pipe(
|
33 |
+
prompt=prompt,
|
34 |
+
negative_prompt=negative_prompt,
|
35 |
+
height=resolution,
|
36 |
+
width=resolution,
|
37 |
+
guidance_scale=cfg,
|
38 |
+
num_inference_steps=steps
|
39 |
+
).images[0]
|
40 |
+
|
41 |
+
return image
|
42 |
+
|
43 |
+
# Default negative prompt
|
44 |
+
default_negative_prompt = "worst quality, normal quality, low quality, low res, blurry, distortion, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch, duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, bad anatomy, bad proportions, bad quality, deformed, disconnected limbs, out of frame, out of focus, dehydrated, disfigured, extra arms, extra limbs, extra hands, fused fingers, gross proportions, long neck, jpeg, malformed limbs, mutated, mutated hands, mutated limbs, missing arms, missing fingers, picture frame, poorly drawn hands, poorly drawn face, collage, pixel, pixelated, grainy, color aberration, amputee, autograph, bad illustration, beyond the borders, blank background, body out of frame, boring background, branding, cut off, dismembered, disproportioned, distorted, draft, duplicated features, extra fingers, extra legs, fault, flaw, grains, hazy, identifying mark, improper scale, incorrect physiology, incorrect ratio, indistinct, kitsch, low resolution"
|
45 |
+
|
46 |
+
# Gradio interface
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=generate_image,
|
49 |
+
inputs=[
|
50 |
+
gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
|
51 |
+
gr.Textbox(label="Negative Prompt", value=default_negative_prompt),
|
52 |
+
gr.Slider(512, 1024, 1024, step=64, label="Resolution"),
|
53 |
+
gr.Slider(1, 100, 48, step=1, label="Number of Steps"),
|
54 |
+
gr.Slider(1, 20, 9, step=0.5, label="CFG Scale")
|
55 |
+
],
|
56 |
+
outputs=gr.Image(type="pil"),
|
57 |
+
title="Meissonic Image Generator",
|
58 |
+
description="Generate images using the Meissonic model. Enter a prompt and adjust parameters to create your image.",
|
59 |
+
)
|
60 |
+
|
61 |
+
iface.launch()
|