File size: 2,219 Bytes
e8f02a0
 
 
 
 
 
 
db793ec
 
e8f02a0
 
ebf6f2c
 
 
 
 
 
 
db793ec
 
e8f02a0
 
 
 
 
 
 
 
 
 
 
db793ec
 
e8f02a0
 
 
 
db793ec
 
 
e8f02a0
 
 
 
 
 
 
 
 
 
 
 
 
 
db793ec
 
e8f02a0
 
 
 
 
 
 
 
 
 
 
 
db793ec
e8f02a0
 
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
# -*- coding: utf-8 -*-
# @时间       : 2024/5/15 18:03
# @作者       : caishilong
# @文件名      : main.py
# @项目名      : ai-platform
# @Software   : PyCharm
import io
import gradio as gr
import numpy as np
import requests
from PIL import Image
from dotenv import load_dotenv
import os
load_dotenv()
SECRET_TOKEN = os.getenv("SECRET_TOKEN")
account = os.getenv('ACCOUNT')
API_BASE_URL = f"https://api.cloudflare.com/client/v4/accounts/{account}/ai/run/"
headers = {"Authorization": f"Bearer {SECRET_TOKEN}"}


def run(model, inputs):
    input = inputs
    response = requests.post(f"{API_BASE_URL}{model}", headers=headers, json=input)
    # 返回的是"content-type": "image/png"
    try:
        print(response.json())
    except:
        pass
    image = Image.open(io.BytesIO(response.content))
    image_array = np.array(image)
    return image_array


# output = run("@cf/stabilityai/stable-diffusion-xl-base-1.0", inputs)
#
# with open("output.png", "wb") as f:
#     f.write(output)



def fake_diffusion(model, prompt, guidance, image: np.ndarray):
    # image = Image.fromarray(image)
    #
    # image = np.array(image)
    # print(image)
    # plt.plot(121)
    # plt.imshow(image)
    # plt.show()
    data = {"prompt": prompt,
            "guidance": guidance,
            # "image": image.tolist()
            }
    output = run(model, data)
    yield output


demo = gr.Interface(fake_diffusion,
                    [
                        gr.Dropdown(["@cf/stabilityai/stable-diffusion-xl-base-1.0",
                                     "@cf/lykon/dreamshaper-8-lcm",
                                     '@cf/bytedance/stable-diffusion-xl-lightning',
                                     '@cf/runwayml/stable-diffusion-v1-5-img2img'],
                                    label="Model", value='@cf/bytedance/stable-diffusion-xl-lightning'),
                        gr.Textbox(label='prompt', value='human playing with a dog'),
                        gr.Slider(1, 20, 20, label='guidance'),
                        # gr.Image()
                    ],
                    outputs="image")

if __name__ == "__main__":
    demo.launch(server_name='0.0.0.0',auth= ("admin", r'qwe[]\123'),)