Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import base64
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
hf_token = os.environ.get('hf_token')
|
9 |
+
API_URL = os.environ.get('api_url')
|
10 |
+
|
11 |
+
headers = {
|
12 |
+
"Accept" : "application/json",
|
13 |
+
"Authorization": f"Bearer {hf_token}",
|
14 |
+
"Content-Type": "application/json"
|
15 |
+
}
|
16 |
+
|
17 |
+
# helper to decode input image
|
18 |
+
def decode_base64_image(image_string):
|
19 |
+
base64_image = base64.b64decode(image_string)
|
20 |
+
buffer = BytesIO(base64_image)
|
21 |
+
image = Image.open(buffer)
|
22 |
+
return image
|
23 |
+
|
24 |
+
def predict(prompt, temperature, guidance_scale, num_inference_steps, seed):
|
25 |
+
payload = {
|
26 |
+
"inputs": prompt,
|
27 |
+
"temperature": temperature,
|
28 |
+
"guidance_scale": guidance_scale,
|
29 |
+
"num_inference_steps": num_inference_steps,
|
30 |
+
"seed": seed
|
31 |
+
}
|
32 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
33 |
+
output = response.json()
|
34 |
+
image = decode_base64_image(image_string=output)
|
35 |
+
return image
|
36 |
+
|
37 |
+
demo = gr.Interface(
|
38 |
+
predict,
|
39 |
+
inputs=[
|
40 |
+
gr.Textbox(label='Prompt'),
|
41 |
+
gr.Slider(0.1, 5.0, label='Temperature', step=0.1, value=1.0),
|
42 |
+
gr.Slider(0.1, 15.0, label='Guidance Scale', step=0.1, value=7.5),
|
43 |
+
gr.Slider(1, 100, label='Number of inference steps', step=1, value=50),
|
44 |
+
gr.Slider(1, 100, label='Seed', step=1, value=42),
|
45 |
+
],
|
46 |
+
outputs="image",
|
47 |
+
title="Android toy SD demo",
|
48 |
+
description="Generate images with android toy!! Just specify an android toy somewhere in the prompt or click on the example provided below. The first run could take up to 2 minutes if the model is sleeping, then approximately 10 seconds per one request",
|
49 |
+
examples=[["An android toy near Eiffel Tower", 1.0, 7.5, 50, 42],
|
50 |
+
["A blue android toy in snow near Eiffel Tower in winter", 1.0, 7.5, 50, 42],
|
51 |
+
["An android toy flying near Statue of Liberty", 1.0, 7.5, 50, 42]],
|
52 |
+
cache_examples=True
|
53 |
+
)
|
54 |
+
demo.launch()
|