DarwinAnim8or commited on
Commit
312c9c4
1 Parent(s): 31a0491

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ from openvino.runtime import Core
4
+ from optimum.intel import OVStableDiffusionPipeline
5
+ from PIL import Image as PImg
6
+ import numpy as np
7
+
8
+ model_id = "NoCrypt/SomethingV2_2"
9
+ core = Core()
10
+
11
+ # Check available devices
12
+ devices = core.available_devices
13
+ print("Available devices:", devices)
14
+
15
+ # Use 'GPU' if available, otherwise fall back to 'CPU'
16
+ device = "GPU" if "GPU" in devices else "CPU"
17
+ print(f"Using device: {device}")
18
+
19
+ ov_pipe_bf16 = OVStableDiffusionPipeline.from_pretrained(
20
+ model_id,
21
+ export=True,
22
+ device=device # Specify the device here
23
+ )
24
+
25
+ # The compile step is not needed as it's handled internally
26
+ def generate_image(prompt, num_inference_steps):
27
+ # Generate an image from the prompt using the pipeline
28
+ start = time.time()
29
+ output = ov_pipe_bf16(prompt, num_inference_steps=num_inference_steps, output_type="np")
30
+ end = time.time()
31
+ print("Inference time: ", end - start)
32
+
33
+ # Convert the image to PIL Image object
34
+ image_data = output.images[0]
35
+ image_data = (image_data * 255).clip(0, 255).astype(np.uint8)
36
+ image = PImg.fromarray(image_data)
37
+
38
+ # Calculate the target size based on the scaling factor
39
+ target_resolution = 1.2
40
+ width = int(image.width * target_resolution)
41
+ height = int(image.height * target_resolution)
42
+ target_size = (width, height)
43
+
44
+ # Upscale the image to the target resolution
45
+ upscaled_image = image.resize(target_size, resample=PImg.BICUBIC)
46
+ return upscaled_image
47
+
48
+
49
+ examples = [
50
+ ["masterpiece, best quality, 1girl, blonde, colorful, clouds, outdoors, falling leaves, smiling, whimsical"],
51
+ ["masterpiece, best quality, landscape"],
52
+ ["masterpiece, best quality, 1girl, aqua eyes, baseball cap, blonde hair, looking at viewer, shirt, short hair, simple background, solo, upper body, yellow shirt"]
53
+ ]
54
+
55
+ iface = gr.Interface(
56
+ fn=generate_image,
57
+ inputs=[
58
+ gr.Textbox(label="Enter a prompt"),
59
+ gr.Slider(minimum=1, maximum=20, value=8, step=1, label="Number of inference steps")
60
+ ],
61
+ outputs=gr.Image(label="Generated image"),
62
+ title="OpenVINO Anime Diffusion",
63
+ description="A gradio app that generates an image from a text prompt using the stable diffusion pipeline using the OpenVINO library for speed!",
64
+ examples=examples,
65
+ )
66
+
67
+ iface.launch()