Breadinlondon commited on
Commit
db0edb9
·
verified ·
1 Parent(s): 80e390b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -46
app.py CHANGED
@@ -1,57 +1,40 @@
1
- import gradio as gr
2
- import numpy as np
3
- import random
4
- from optimum.intel import OVStableDiffusionXLPipeline
5
 
6
- # Load the model specifically for CPU optimization (OpenVINO)
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
- if randomize_seed:
24
- seed = random.randint(0, 2**32 - 1)
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=512, # Locked to 512 for CPU stability
34
- height=512,
35
- latents=None
36
- ).images[0]
 
37
 
38
- return image, seed
 
 
 
39
 
40
- # UI Setup (Simplified for CPU Speed)
41
- with gr.Blocks() as demo:
42
- gr.Markdown("# ❄️ CPU-Optimized SDXL Turbo (OpenVINO)")
43
- with gr.Row():
44
- prompt = gr.Text(label="Prompt", placeholder="A fast CPU cat")
45
- run_btn = gr.Button("Run")
46
 
47
- result = gr.Image()
 
 
 
 
 
 
48
 
49
- # Hide complex settings to prevent CPU crashes
50
- with gr.Accordion("Settings", open=False):
51
- steps = gr.Slider(1, 4, value=1, label="Steps (Keep at 1 for CPU)")
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"]