Spaces:
Configuration error
Configuration error
Uploaded Text-to-Image.ipynb
Browse files- Text_to_Image.ipynb +121 -0
Text_to_Image.ipynb
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": [],
|
| 7 |
+
"gpuType": "T4"
|
| 8 |
+
},
|
| 9 |
+
"kernelspec": {
|
| 10 |
+
"name": "python3",
|
| 11 |
+
"display_name": "Python 3"
|
| 12 |
+
},
|
| 13 |
+
"language_info": {
|
| 14 |
+
"name": "python"
|
| 15 |
+
},
|
| 16 |
+
"accelerator": "GPU"
|
| 17 |
+
},
|
| 18 |
+
"cells": [
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"source": [
|
| 22 |
+
"import torch\n",
|
| 23 |
+
"from PIL import Image\n",
|
| 24 |
+
"from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline\n",
|
| 25 |
+
"from diffusers import StableDiffusionImg2ImgPipeline\n",
|
| 26 |
+
"from diffusers import EulerAncestralDiscreteScheduler\n",
|
| 27 |
+
"\n",
|
| 28 |
+
"#provide your Hugging Face Authentication token\n",
|
| 29 |
+
"#you can obtain your token from https://huggingface.co/settings/tokens\n",
|
| 30 |
+
"auth_token = input(\"Enter your Hugging Face token: \")\n",
|
| 31 |
+
"\n",
|
| 32 |
+
"### image generation ###\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"#using stable diffusion version 2.1 for image generation\n",
|
| 35 |
+
"modelid = \"stabilityai/stable-diffusion-2-1\"\n",
|
| 36 |
+
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 37 |
+
"\n",
|
| 38 |
+
"pipe = StableDiffusionPipeline.from_pretrained(\n",
|
| 39 |
+
" modelid,\n",
|
| 40 |
+
" revision=\"fp16\",\n",
|
| 41 |
+
" torch_dtype= torch.float16,\n",
|
| 42 |
+
" use_auth_token=auth_token,\n",
|
| 43 |
+
" low_cpu_mem_usage=True\n",
|
| 44 |
+
")\n",
|
| 45 |
+
"#using EulerAncestralDiscreteScheduler for sharper and detailed images\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)\n",
|
| 48 |
+
"\n",
|
| 49 |
+
"\n",
|
| 50 |
+
"### image modification ###\n",
|
| 51 |
+
"#using StableDiffusionImg2ImgPipeline for image to image generation\n",
|
| 52 |
+
"pipe.to(device)\n",
|
| 53 |
+
"\n",
|
| 54 |
+
"pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(\"stabilityai/stable-diffusion-2-1\", torch_dtype=torch.float16, low_cpu_mem_usage=True).to(device)\n",
|
| 55 |
+
"pipe_img2img.to(device)\n",
|
| 56 |
+
"\n"
|
| 57 |
+
],
|
| 58 |
+
"metadata": {
|
| 59 |
+
"id": "6IveiamACvSG"
|
| 60 |
+
},
|
| 61 |
+
"execution_count": null,
|
| 62 |
+
"outputs": []
|
| 63 |
+
},
|
| 64 |
+
{
|
| 65 |
+
"cell_type": "code",
|
| 66 |
+
"source": [
|
| 67 |
+
"import gradio as gr\n",
|
| 68 |
+
"# Function to generate image from text\n",
|
| 69 |
+
"def generate_image(prompt, guidance_scale):\n",
|
| 70 |
+
" global stored_image\n",
|
| 71 |
+
" image = pipe(prompt, guidance_scale=guidance_scale).images[0]\n",
|
| 72 |
+
" stored_image = image\n",
|
| 73 |
+
" return image\n",
|
| 74 |
+
"\n",
|
| 75 |
+
"# Function to modify image (Img2Img)\n",
|
| 76 |
+
"def modify_image(prompt, strength=0.5):\n",
|
| 77 |
+
" global stored_image\n",
|
| 78 |
+
" if stored_image is None:\n",
|
| 79 |
+
" return \"No generated image available. Generate an image first!\"\n",
|
| 80 |
+
"\n",
|
| 81 |
+
" stored_image = stored_image.convert(\"RGB\")\n",
|
| 82 |
+
" modified_image = pipe_img2img(prompt=prompt, image=stored_image, strength=strength, guidance_scale=8.5).images[0]\n",
|
| 83 |
+
" return modified_image\n",
|
| 84 |
+
"\n",
|
| 85 |
+
"# Gradio UI\n",
|
| 86 |
+
"with gr.Blocks() as demo:\n",
|
| 87 |
+
" gr.Markdown(\"# Stable Diffusion - Generate & Modify Images\")\n",
|
| 88 |
+
" with gr.Group():\n",
|
| 89 |
+
" with gr.Row():\n",
|
| 90 |
+
" prompt_input = gr.Textbox(label=\"Enter Prompt\", placeholder=\"A futuristic city at sunset\")\n",
|
| 91 |
+
" with gr.Row():\n",
|
| 92 |
+
" guidance_input = gr.Slider(1.0, 15.0, value=8.5, label=\"Guidance Scale (More guidance meansthe model follows the prompt very strictly but is less creative)\") # User can adjust guidance\n",
|
| 93 |
+
"\n",
|
| 94 |
+
" with gr.Row():\n",
|
| 95 |
+
" generate_btn = gr.Button(\"Generate\")\n",
|
| 96 |
+
" with gr.Row():\n",
|
| 97 |
+
" output_image = gr.Image(label=\"Generated Image\")\n",
|
| 98 |
+
"\n",
|
| 99 |
+
" generate_btn.click(generate_image, inputs=[prompt_input, guidance_input], outputs=output_image)\n",
|
| 100 |
+
"\n",
|
| 101 |
+
"\n",
|
| 102 |
+
" # Modify Image (After Generation)\n",
|
| 103 |
+
" with gr.Tab(\"Modify Image\"):\n",
|
| 104 |
+
" modify_prompt = gr.Textbox(label=\"Enter modification prompt\")\n",
|
| 105 |
+
" strength_slider = gr.Slider(0.1, 1.0, value=0.5, label=\"Strength (Higher = More Change)\")\n",
|
| 106 |
+
" modify_btn = gr.Button(\"Modify\")\n",
|
| 107 |
+
" modified_output = gr.Image(label=\"Modified Image\")\n",
|
| 108 |
+
"\n",
|
| 109 |
+
" modify_btn.click(modify_image, inputs=[modify_prompt, strength_slider], outputs=modified_output)\n",
|
| 110 |
+
"\n",
|
| 111 |
+
"# Launch Gradio App\n",
|
| 112 |
+
"demo.launch(share=True)\n"
|
| 113 |
+
],
|
| 114 |
+
"metadata": {
|
| 115 |
+
"id": "ZuoDNzKH29da"
|
| 116 |
+
},
|
| 117 |
+
"execution_count": null,
|
| 118 |
+
"outputs": []
|
| 119 |
+
}
|
| 120 |
+
]
|
| 121 |
+
}
|