File size: 2,094 Bytes
aa13efa
ec3730d
73c6f92
 
 
 
 
 
 
 
 
 
eefa462
ec3730d
aa13efa
 
73c6f92
f25fd61
 
 
 
 
 
 
73c6f92
aa13efa
73c6f92
aa13efa
 
f25fd61
 
 
 
18976e3
 
73c6f92
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import spaces

import gradio as gr
import torch
import numpy as np
import torch
from pytorch_lightning import seed_everything
from model_lib.utils import parse_args
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"

title = "MoMA"
description = "This model has to run on GPU. Please find our project page at https://moma-adapter.github.io/."
device = torch.device('cuda')
seed_everything(0)
args = parse_args()

model = None

@spaces.GPU
def load_model(model):
    from model_lib.modules import MoMA_main_modal
    model = MoMA_main_modal(args).to(device, dtype=torch.float16)
    return model

@spaces.GPU
def inference(rgb, subject, prompt, strength, seed):
    seed = int(seed) if seed else 0
    seed = seed if not seed == 0 else np.random.randint(0,1000)
    
    if model is None:
        model = load_model(model)
    
    generated_image = model.generate_images(rgb, subject, prompt, strength=strength, seed=seed)
    return generated_image

gr.Interface(
    inference,
    [gr.Image(type="pil", label="Input RGB"),
     gr.Textbox(lines=1, label="subject"),
     gr.Textbox(lines=1, label="Prompt"),
     gr.Slider(minimum=0.2, maximum=1.2, step=0.1,label="Strength. Recommend: 1.0 for context editing; 0.4 for texture editing",value=1.0),
     gr.Textbox(lines=1, label="Seed. Use 0 for a random seed")],
    gr.Image(type="pil", label="Output"),
    title=title,
    description=description,
    examples=[["example_images/newImages/3.jpg",'car','A car in autumn with falling leaves.',1.0,"6"],["example_images/newImages/3.jpg",'car','A wooden sculpture of a car on a table.',0.4,"4"],["example_images/newImages/2.jpg",'car','A car on a city road with green trees and buildings.',1.0,"4"],["example_images/newImages/03.jpg",'cat','A cat at the Grand Canyon.',1.0,"2"],["example_images/newImages/02.jpg",'dog','A dog in a spring garden with flowers.',1.0,"6"],["example_images/newImages/1.jpeg",'bird','A bird in spring with flowers.',1.0,"1"],["example_images/newImages/17.jpg",'robot','A robot in autumn mountain and lake.',1,"5"]],
    allow_flagging='never'
).launch(debug=False)