File size: 3,039 Bytes
04ff8fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab78c01
04ff8fc
 
 
ab78c01
 
 
 
 
 
 
 
04ff8fc
ab78c01
 
04ff8fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import sys
sys.path.append("./")

import torch
from torchvision import transforms
from src.transformer import Transformer2DModel
from src.pipeline import Pipeline
from src.scheduler import Scheduler
from transformers import (
    CLIPTextModelWithProjection,
    CLIPTokenizer,
)
from diffusers import VQModel
import gradio as gr
import spaces 

device = 'cuda' if torch.cuda.is_available() else 'cpu'

model_path = "MeissonFlow/Meissonic"
model = Transformer2DModel.from_pretrained(model_path, subfolder="transformer")
vq_model = VQModel.from_pretrained(model_path, subfolder="vqvae")
text_encoder = CLIPTextModelWithProjection.from_pretrained(model_path, subfolder="text_encoder")
tokenizer = CLIPTokenizer.from_pretrained(model_path, subfolder="tokenizer")
scheduler = Scheduler.from_pretrained(model_path, subfolder="scheduler")
pipe = Pipeline(vq_model, tokenizer=tokenizer, text_encoder=text_encoder, transformer=model, scheduler=scheduler)
pipe.to(device)

@spaces.GPU
def generate_image(prompt, negative_prompt, resolution, steps, cfg, progress=gr.Progress(track_tqdm=True)):
    image = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        height=resolution,
        width=resolution,
        guidance_scale=cfg,
        num_inference_steps=steps
    ).images[0]
    
    return image

# Default negative prompt
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"

# Gradio interface
iface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
        gr.Textbox(label="Negative Prompt", value=default_negative_prompt),
        gr.Slider(512, 1024, 1024, step=64, label="Resolution"),
        gr.Slider(1, 100, 48, step=1, label="Number of Steps"),
        gr.Slider(1, 20, 9, step=0.5, label="CFG Scale")
    ],
    outputs=gr.Image(type="pil"),
    title="Meissonic Image Generator",
    description="Generate images using the Meissonic model. Enter a prompt and adjust parameters to create your image.",
)

iface.launch()