Spaces:
Build error
Build error
Upload folder using huggingface_hub
Browse files- README.md +3 -9
- app.py +87 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji: 🚀
|
| 4 |
-
colorFrom: pink
|
| 5 |
-
colorTo: blue
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.42.0
|
| 8 |
app_file: app.py
|
| 9 |
-
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: stable_diffusion_image_gen
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app_file: app.py
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 4.41.0
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import io
|
| 4 |
+
import requests, json
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import base64
|
| 7 |
+
from dotenv import load_dotenv, find_dotenv
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
_ = load_dotenv(find_dotenv()) # read local .env file
|
| 11 |
+
hf_api_key = os.environ['HF_API_KEY']
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Text-to-image endpoint
|
| 15 |
+
def get_completion(inputs, parameters=None, ENDPOINT_URL=os.environ['HF_API_TTI_STABILITY_AI']):
|
| 16 |
+
headers = {
|
| 17 |
+
"Authorization": f"Bearer {hf_api_key}",
|
| 18 |
+
"Content-Type": "application/json"
|
| 19 |
+
}
|
| 20 |
+
data = {"inputs": inputs}
|
| 21 |
+
if parameters is not None:
|
| 22 |
+
data.update({"parameters": parameters})
|
| 23 |
+
|
| 24 |
+
response = requests.post(ENDPOINT_URL, headers=headers, data=json.dumps(data))
|
| 25 |
+
|
| 26 |
+
# Check the content type of the response
|
| 27 |
+
content_type = response.headers.get('Content-Type', '')
|
| 28 |
+
print(content_type)
|
| 29 |
+
if 'application/json' in content_type:
|
| 30 |
+
return json.loads(response.content.decode("utf-8"))
|
| 31 |
+
elif 'image/' in content_type:
|
| 32 |
+
return response.content # return raw image data
|
| 33 |
+
|
| 34 |
+
response.raise_for_status() # raise an error for unexpected content types
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
#A helper function to convert the PIL image to base64
|
| 38 |
+
# so you can send it to the API
|
| 39 |
+
def base64_to_pil(img_base64):
|
| 40 |
+
base64_decoded = base64.b64decode(img_base64)
|
| 41 |
+
byte_stream = io.BytesIO(base64_decoded)
|
| 42 |
+
pil_image = Image.open(byte_stream)
|
| 43 |
+
return pil_image
|
| 44 |
+
|
| 45 |
+
def generate(prompt, negative_prompt, steps, guidance, width, height):
|
| 46 |
+
params = {
|
| 47 |
+
"negative_prompt": negative_prompt,
|
| 48 |
+
"num_inference_steps": steps,
|
| 49 |
+
"guidance_scale": guidance,
|
| 50 |
+
"width": width,
|
| 51 |
+
"height": height
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
output = get_completion(prompt,params)
|
| 55 |
+
|
| 56 |
+
# Check if the output is an image (bytes) or JSON (dict)
|
| 57 |
+
if isinstance(output, dict):
|
| 58 |
+
raise ValueError("Expected an image but received JSON: {}".format(output))
|
| 59 |
+
|
| 60 |
+
# If output is raw image data, convert it to a PIL image
|
| 61 |
+
result_image = Image.open(io.BytesIO(output))
|
| 62 |
+
return result_image
|
| 63 |
+
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
+
gr.Markdown("# Image Generation with stable-diffusion-xl-base-1.0")
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column(scale=4):
|
| 68 |
+
prompt = gr.Textbox(label="Your prompt") #Give prompt some real estate
|
| 69 |
+
with gr.Column(scale=1, min_width=50):
|
| 70 |
+
btn = gr.Button("Submit") #Submit button side by side!
|
| 71 |
+
with gr.Accordion("Advanced options", open=False): #Let's hide the advanced options!
|
| 72 |
+
negative_prompt = gr.Textbox(label="Negative prompt")
|
| 73 |
+
with gr.Row():
|
| 74 |
+
with gr.Column():
|
| 75 |
+
steps = gr.Slider(label="Inference Steps", minimum=1, maximum=100, value=25,
|
| 76 |
+
info="In many steps will the denoiser denoise the image?")
|
| 77 |
+
guidance = gr.Slider(label="Guidance Scale", minimum=1, maximum=20, value=7,
|
| 78 |
+
info="Controls how much the text prompt influences the result")
|
| 79 |
+
with gr.Column():
|
| 80 |
+
width = gr.Slider(label="Width", minimum=64, maximum=1024, step=64, value=512)
|
| 81 |
+
height = gr.Slider(label="Height", minimum=64, maximum=1024, step=64, value=512)
|
| 82 |
+
output = gr.Image(label="Result") #Move the output up too
|
| 83 |
+
|
| 84 |
+
btn.click(fn=generate, inputs=[prompt,negative_prompt,steps,guidance,width,height], outputs=[output])
|
| 85 |
+
|
| 86 |
+
gr.close_all()
|
| 87 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pillow
|
| 3 |
+
python-dotenv
|