File size: 3,061 Bytes
8725491
 
dce67cf
81b31fb
 
 
 
 
 
e6696c2
81b31fb
 
 
 
744924d
c0e3387
9f5126a
0afedf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import spaces
import gradio as gr
import torch
#import transformers
#from transformers import AutoTokenizer
#from transformers import pipeline
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file

base = "stabilityai/stable-diffusion-xl-base-1.0"
repo = "ByteDance/SDXL-Lightning"
ckpt = "sdxl_lightning_4step_unet.safetensors" # Use the correct ckpt for your step setting!
# Load model.
pipe_box=[]

@spaces.GPU()
def main():
    def init():
        device="cuda:0"
        #unet = UNet2DConditionModel.from_config(base, subfolder="unet").to(device, torch.float16)
        #unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device))
        #pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to(device)
        pipe = StableDiffusionXLPipeline.from_pretrained(base, torch_dtype=torch.float16, variant="fp16").to(device)
        # Ensure sampler uses "trailing" timesteps.
        pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
        pipe_box.append(pipe)
    #init()
    @spaces.GPU()
    def run():
        init()
        pipe=pipe_box[0]
        # Ensure using the same inference steps as the loaded model and CFG set to 0.
        return pipe("A cat", num_inference_steps=4, guidance_scale=0).images[0].save("output.png")
    
    
    
    
    '''
    tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b")
    
    model = transformers.AutoModelForCausalLM.from_pretrained(
      'mosaicml/mpt-7b-instruct',
      trust_remote_code=True
    )
    
    
    pipe = pipeline('text-generation', model=model, tokenizer=tokenizer, device='cuda:0')
    
    INSTRUCTION_KEY = "### Instruction:"
    RESPONSE_KEY = "### Response:"
    INTRO_BLURB = "Below is an instruction that describes a task. Write a response that appropriately completes the request."
    PROMPT_FOR_GENERATION_FORMAT = """{intro}
    {instruction_key}
    {instruction}
    {response_key}
    """.format(
        intro=INTRO_BLURB,
        instruction_key=INSTRUCTION_KEY,
        instruction="{instruction}",
        response_key=RESPONSE_KEY,
    )
    
    example = "James decides to run 3 sprints 3 times a week. He runs 60 meters each sprint. How many total meters does he run a week? Explain before answering."
    fmt_ex = PROMPT_FOR_GENERATION_FORMAT.format(instruction=example)
    
    
    
    
    @spaces.GPU
    def run():
        with torch.autocast('cuda', dtype=torch.bfloat16):
            return(
                pipe('Here is a recipe for vegan banana bread:\n',
                    max_new_tokens=100,
                    do_sample=True,
                    use_cache=True))
    '''
    
    
    with gr.Blocks() as app:
        btn = gr.Button()
        #outp=gr.Textbox()
        outp=gr.Image()
        btn.click(run,None,outp)
    app.launch()
    
if __name__ == "__main__":
    main()