Spaces:
Running
Running
Surya Narayana
commited on
Update text_to_image.py
Browse files- text_to_image.py +9 -80
text_to_image.py
CHANGED
@@ -8,14 +8,14 @@ Original file is located at
|
|
8 |
"""
|
9 |
|
10 |
# Commented out IPython magic to ensure Python compatibility.
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
|
20 |
from diffusers import StableDiffusionPipeline
|
21 |
import gc
|
@@ -599,44 +599,6 @@ ov_pipe = OVStableDiffusionPipeline(
|
|
599 |
scheduler=lms
|
600 |
)
|
601 |
|
602 |
-
"""### Text-to-Image generation
|
603 |
-
[back to top ⬆️](#Table-of-contents:)
|
604 |
-
|
605 |
-
Now, you can define a text prompt for image generation and run inference pipeline.
|
606 |
-
Optionally, you can also change the random generator seed for latent state initialization and number of steps.
|
607 |
-
|
608 |
-
> **Note**: Consider increasing `steps` to get more precise results. A suggested value is `50`, but it will take longer time to process.
|
609 |
-
"""
|
610 |
-
|
611 |
-
import ipywidgets as widgets
|
612 |
-
sample_text = ('cyberpunk cityscape like Tokyo New York with tall buildings at dusk golden hour cinematic lighting, epic composition. '
|
613 |
-
'A golden daylight, hyper-realistic environment. '
|
614 |
-
'Hyper and intricate detail, photo-realistic. '
|
615 |
-
'Cinematic and volumetric light. '
|
616 |
-
'Epic concept art. '
|
617 |
-
'Octane render and Unreal Engine, trending on artstation')
|
618 |
-
text_prompt = widgets.Text(value=sample_text, description='your text')
|
619 |
-
num_steps = widgets.IntSlider(min=1, max=50, value=20, description='steps:')
|
620 |
-
seed = widgets.IntSlider(min=0, max=10000000, description='seed: ', value=42)
|
621 |
-
widgets.VBox([text_prompt, seed, num_steps])
|
622 |
-
|
623 |
-
print('Pipeline settings')
|
624 |
-
print(f'Input text: {text_prompt.value}')
|
625 |
-
print(f'Seed: {seed.value}')
|
626 |
-
print(f'Number of steps: {num_steps.value}')
|
627 |
-
|
628 |
-
result = ov_pipe(text_prompt.value, num_inference_steps=num_steps.value, seed=seed.value)
|
629 |
-
|
630 |
-
"""Finally, let us save generation results.
|
631 |
-
The pipeline returns several results: `sample` contains final generated image, `iterations` contains list of intermediate results for each step.
|
632 |
-
"""
|
633 |
-
|
634 |
-
final_image = result['sample'][0]
|
635 |
-
if result['iterations']:
|
636 |
-
all_frames = result['iterations']
|
637 |
-
img = next(iter(all_frames))
|
638 |
-
img.save(fp='result.gif', format='GIF', append_images=iter(all_frames), save_all=True, duration=len(all_frames) * 5, loop=0)
|
639 |
-
final_image.save('result.png')
|
640 |
|
641 |
"""Now is show time!"""
|
642 |
|
@@ -645,37 +607,4 @@ import ipywidgets as widgets
|
|
645 |
text = '\n\t'.join(text_prompt.value.split('.'))
|
646 |
print("Input text:")
|
647 |
print("\t" + text)
|
648 |
-
display(final_image)
|
649 |
-
|
650 |
-
"""Nice. As you can see, the picture has quite a high definition 🔥."""
|
651 |
-
|
652 |
-
import gradio as gr
|
653 |
-
|
654 |
-
def generate_from_text(text, seed, num_steps, _=gr.Progress(track_tqdm=True)):
|
655 |
-
result = ov_pipe(text, num_inference_steps=num_steps, seed=seed)
|
656 |
-
return result["sample"][0]
|
657 |
-
|
658 |
-
with gr.Blocks() as demo:
|
659 |
-
with gr.Tab("Text-to-Image generation"):
|
660 |
-
with gr.Row():
|
661 |
-
with gr.Column():
|
662 |
-
text_input = gr.Textbox(lines=3, label="Text")
|
663 |
-
seed_input = gr.Slider(0, 10000000, value=42, label="Seed")
|
664 |
-
steps_input = gr.Slider(1, 50, value=20, step=1, label="Steps")
|
665 |
-
out = gr.Image(label="Result", type="pil")
|
666 |
-
btn = gr.Button()
|
667 |
-
btn.click(generate_from_text, [text_input, seed_input, steps_input], out)
|
668 |
-
|
669 |
-
# Remove the "Image-to-Image generation" tab and its content
|
670 |
-
|
671 |
-
try:
|
672 |
-
demo.launch(debug=True)
|
673 |
-
except Exception:
|
674 |
-
demo.launch(share=True, debug=True)
|
675 |
-
|
676 |
-
!ls # List files in the current directory
|
677 |
-
|
678 |
-
!echo "Hello, World!" # Print a message
|
679 |
-
|
680 |
-
!gradio deploy
|
681 |
-
|
|
|
8 |
"""
|
9 |
|
10 |
# Commented out IPython magic to ensure Python compatibility.
|
11 |
+
%pip install -q "openvino>=2023.1.0"
|
12 |
+
%pip install -q --extra-index-url https://download.pytorch.org/whl/cpu "diffusers[torch]>=0.9.0"
|
13 |
+
%pip install -q "huggingface-hub>=0.9.1"
|
14 |
+
%pip install -q gradio
|
15 |
+
%pip install -q transformers
|
16 |
+
%pip install kaleido cohere openai tiktoken
|
17 |
+
%pip install typing-extensions==3.10.0.2
|
18 |
+
%pip install diffusers transformers
|
19 |
|
20 |
from diffusers import StableDiffusionPipeline
|
21 |
import gc
|
|
|
599 |
scheduler=lms
|
600 |
)
|
601 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
602 |
|
603 |
"""Now is show time!"""
|
604 |
|
|
|
607 |
text = '\n\t'.join(text_prompt.value.split('.'))
|
608 |
print("Input text:")
|
609 |
print("\t" + text)
|
610 |
+
display(final_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|