Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,40 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
model_repo_id = "stabilityai/sdxl-turbo"
|
| 8 |
-
|
| 9 |
-
# 'export=True' converts the model to OpenVINO format on-the-fly
|
| 10 |
-
# If you have already exported it, point model_id to your local folder
|
| 11 |
-
print("Loading OpenVINO model (this may take a minute on first run)...")
|
| 12 |
-
pipe = OVStableDiffusionXLPipeline.from_pretrained(
|
| 13 |
-
model_repo_id,
|
| 14 |
-
export=True,
|
| 15 |
-
compile=True # Pre-compiles for CPU speed
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
# Speed optimization: Static Reshaping
|
| 19 |
-
# This tells the CPU exactly what size to expect so it can optimize the math paths
|
| 20 |
-
pipe.reshape(batch_size=1, height=512, width=512, num_images_per_prompt=1)
|
| 21 |
|
| 22 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Standard CPU generation
|
| 27 |
-
# Note: Width/Height must match the .reshape() values above for max speed
|
| 28 |
-
image = pipe(
|
| 29 |
prompt=prompt,
|
| 30 |
negative_prompt=negative_prompt,
|
| 31 |
guidance_scale=guidance_scale,
|
| 32 |
num_inference_steps=num_inference_steps,
|
| 33 |
-
width=
|
| 34 |
-
height=
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
with gr.Row():
|
| 44 |
-
prompt = gr.Text(label="Prompt", placeholder="A fast CPU cat")
|
| 45 |
-
run_btn = gr.Button("Run")
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
seed = gr.Number(value=42, label="Seed")
|
| 53 |
-
|
| 54 |
-
run_btn.click(infer, [prompt, gr.State(""), seed, gr.State(False), gr.State(512), gr.State(512), gr.State(0.0), steps], [result, seed])
|
| 55 |
-
|
| 56 |
-
if __name__ == "__main__":
|
| 57 |
-
demo.launch()
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import io
|
| 3 |
+
import base64
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
# ... (rest of your SDXL-Turbo setup) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 9 |
+
# 1. Generate the raw image
|
| 10 |
+
result = pipe(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
prompt=prompt,
|
| 12 |
negative_prompt=negative_prompt,
|
| 13 |
guidance_scale=guidance_scale,
|
| 14 |
num_inference_steps=num_inference_steps,
|
| 15 |
+
width=width,
|
| 16 |
+
height=height,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
raw_image = result.images[0]
|
| 20 |
|
| 21 |
+
# 2. Save image to a "buffer" (in memory, not on disk)
|
| 22 |
+
img_byte_arr = io.BytesIO()
|
| 23 |
+
raw_image.save(img_byte_arr, format='PNG')
|
| 24 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 25 |
|
| 26 |
+
# 3. Upload to ImgBB (Free API)
|
| 27 |
+
# Get a free key from https://api.imgbb.com/
|
| 28 |
+
IMGBB_API_KEY = "YOUR_IMGBB_API_KEY_HERE"
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
response = requests.post(
|
| 31 |
+
"https://api.imgbb.com/1/upload",
|
| 32 |
+
data={
|
| 33 |
+
"key": IMGBB_API_KEY,
|
| 34 |
+
"image": base64.b64encode(img_byte_arr),
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
|
| 38 |
+
# 4. Return the URL instead of the raw image
|
| 39 |
+
upload_data = response.json()
|
| 40 |
+
return upload_data["data"]["url"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|