File size: 1,308 Bytes
d426a18
 
 
f184c8b
d426a18
 
 
 
 
 
839b20a
d426a18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356ec78
d426a18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356ec78
d426a18
 
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
import json
import os
import requests
import gradio as gr

def generate_image(text_prompt, init_image):
    engine_id = "stable-diffusion-v1-5"
    api_host = os.getenv('API_HOST', 'https://api.stability.ai')
    url = f"{api_host}/v1alpha/generation/{engine_id}/image-to-image"

    api_key = os.getenv('stability_ai')
    if api_key is None:
        raise Exception("Missing Stability API key.")

    options = json.dumps(
        {
            "text_prompts": [
                {
                    "text": text_prompt,
                    "weight": 2
                }
            ],
        }
    )

    headers = {
        'accept': 'image/png',
        'Authorization': api_key,
    }

    files = {
        'init_image': init_image.read(),
        'options': (None, options),
    }

    response = requests.post(url, headers=headers, files=files)

    if response.status_code != 200:
        raise Exception("Non-200 response: " + str(response.text))

    with open("output.png", "wb") as f:
        f.write(response.content)

    return "output.png"


# Gradio


inputs = [
    gr.inputs.Textbox(label="Text"),
    gr.inputs.Image(label="Image")
]

outputs = gr.outputs.Image(type='numpy', label="Generated Image")

gr.Interface(fn=generate_image, inputs=inputs, outputs=outputs).launch(debug=True)