Spaces:
Runtime error
Runtime error
first commit, magicboard on hf
Browse filesFiles changed (7)
- README.md+32-13
- locales/base.potAdded+56
- locales/it/LC_MESSAGES/base.moAdded
- locales/it/LC_MESSAGES/base.poAdded+56
- main.pyAdded+64
- requirements.txtAdded+5
- utils.pyAdded+25
12345678910111213---title: MagicBoardemoji: 🐨colorFrom: redcolorTo: greensdk: streamlitsdk_version: 1.10.0app_file: app.pypinned: falselicense: mit---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference1234567891011121314151617181920212223242526272829303132# Magic BoardMagic Board is a PoC using stable diffusion to generate out-of-mind images starting from a sketch.Thank to streamlit-drawable-canvas package it is super simple create the perfect environment to play with stable diffusion, starting form our quick draw.Select the number of steps and the strength to generate the final image.I like minimalism, so for this experiments I try to use a low-code approach. Also lowest time as possible ;)## Some technicalitiesThe Stable Diffusion model is the latest provided by the diffusers library.If you want to use it, to avoid local checkpoint, just get an access token from hugging face, follow [these](https://huggingface.co/CompVis/stable-diffusion-v1-4) instruction to get access.Then copy and paste the Token in [config.py](./config.py).Install the requirements and you should be able to run:```streamlit run main.py```This version has also the multi-language support, at the moment just English and Italian.The final result is :<p style="text-align:center;"><img src="./result.png" width="200"/></p>NOTE:This implementation use a `torch.float16` due to low VRAM availability. Strongly suggest to set it `torch.float32` to speed up the computation. [Here](./utils.py)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556# SOME DESCRIPTIVE TITLE.# Copyright (C) YEAR ORGANIZATION# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.#msgid ""msgstr """Project-Id-Version: PACKAGE VERSION\n""POT-Creation-Date: 2022-09-06 00:56+0200\n""PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n""Last-Translator: FULL NAME <EMAIL@ADDRESS>\n""Language-Team: LANGUAGE <LL@li.org>\n""MIME-Version: 1.0\n""Content-Type: text/plain; charset=UTF-8\n""Content-Transfer-Encoding: 8bit\n""Generated-By: pygettext.py 1.5\n"
#: main.py:10msgid "Choose your language"msgstr ""
#: main.py:21msgid "Drawing tool:"msgstr ""
#: main.py:24msgid "Stroke width: "msgstr ""
#: main.py:25msgid "Stroke color hex: "msgstr ""
#: main.py:26msgid "Background color hex: "msgstr ""
#: main.py:27msgid "Background image:"msgstr ""
#: main.py:28msgid "Number of Steps: "msgstr ""
#: main.py:29msgid "Strength value: "msgstr ""
#: main.py:50msgid "Prompt to generate your cool image"msgstr ""
#: main.py:51msgid "write you text here to improve the image with your favourite style"msgstr ""
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556# SOME DESCRIPTIVE TITLE.# Copyright (C) YEAR ORGANIZATION# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.#msgid ""msgstr """Project-Id-Version: PACKAGE VERSION\n""POT-Creation-Date: 2022-09-06 00:56+0200\n""PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n""Last-Translator: FULL NAME <EMAIL@ADDRESS>\n""Language-Team: LANGUAGE <LL@li.org>\n""MIME-Version: 1.0\n""Content-Type: text/plain; charset=UTF-8\n""Content-Transfer-Encoding: 8bit\n""Generated-By: pygettext.py 1.5\n"
#: main.py:10msgid "Choose your language"msgstr "Seleziona la lingua"
#: main.py:21msgid "Drawing tool:"msgstr "Strumento da disegno:"
#: main.py:24msgid "Stroke width: "msgstr "Spessore tratto:"
#: main.py:25msgid "Stroke color hex: "msgstr "Colore tratto:"
#: main.py:26msgid "Background color hex: "msgstr "Colore background:"
#: main.py:27msgid "Background image:"msgstr "Carica un'immagine:"
#: main.py:28msgid "Number of Steps: "msgstr "Numero di iterazioni:"
#: main.py:29msgid "Strength value: "msgstr "Attinenza all'originale:"
#: main.py:50msgid "Prompt to generate your cool image"msgstr "Testo di input per iniziare la magia"
#: main.py:51msgid "write you text here to improve the image with your favourite style"msgstr "Scrivi qui le indicazioni che trasformeranno la tua immagine"
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364from PIL import Imagefrom utils import pipe_imageimport streamlit as stfrom streamlit_drawable_canvas import st_canvasimport gettextimport numpy as np
_ = gettext.gettext
language = st.selectbox(_('Choose your language'), ['en', 'it'])try: localizator = gettext.translation('base', localedir='locales', languages=[language]) localizator.install() _ = localizator.gettext except: pass
st.markdown("<h1 style='text-align:center;'> Magic Board</h1>", unsafe_allow_html=True)# Specify canvas parameters in applicationdrawing_mode = st.sidebar.selectbox( _("Drawing tool:"), ("freedraw", "line", "rect", "circle", "transform"))
stroke_width = st.sidebar.slider(_("Stroke width: "), 1, 25, 3)stroke_color = st.sidebar.color_picker(_("Stroke color hex: "))bg_color = st.sidebar.color_picker(_("Background color hex: "), "#eee")bg_image = st.sidebar.file_uploader(_("Background image:"), type=["png", "jpg"])strength = st.sidebar.slider(_("Strength value: "), 0.0, 1.0, 0.1)
# Create a canvas componentcanvas_result = st_canvas( fill_color="rgba(255, 165, 0, 0.3)", # Fixed fill color with some opacity stroke_width=stroke_width, stroke_color=stroke_color, background_color=bg_color, background_image=Image.open(bg_image) if bg_image else None, update_streamlit=True, height=450, width=680, drawing_mode=drawing_mode, #point_display_radius=point_display_radius if drawing_mode == 'point' else 0, key="canvas",)
prompt = st.text_input(_('Prompt to generate your cool image'), placeholder=_('write you text here to improve the image with your favourite style'))
if st.button('Submit'): img_size = (512, 512) if bg_image: img_back=Image.open(bg_image).convert('RGB').resize(img_size) comp_img = img_back if np.any(canvas_result.image_data): img_draw=Image.fromarray(canvas_result.image_data).convert('RGB').resize(img_size) comp_img = img_draw if bg_image and np.any(canvas_result.image_data): comp_img = Image.new("RGB", img_size) comp_img = Image.blend(img_draw, img_back, alpha=0.5)
images = pipe_image(prompt=prompt, init_image=comp_img, strength=strength) for image in images: st.image(image)12345torchtransformersscipyftfygit+https://github.com/huggingface/diffusers12345678910111213141516171819202122232425import torchfrom diffusers import StableDiffusionImg2ImgPipeline
import osTOKEN=os.environ.get('sd')
torch.cuda.empty_cache()device = "cpu"
def pipe_image(prompt, init_image=None, strength=0.6): pipe = StableDiffusionImg2ImgPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", revision="fp16", torch_dtype=torch.float32, use_auth_token=TOKEN)
pipe = pipe.to(device) pipe.enable_attention_slicing() images = pipe(prompt=prompt, init_image=init_image, strength=strength, guidance_scale=7.5, num_inference_steps=1000).images return imagesREADME.md+32-13
locales/base.potAdded+56
locales/it/LC_MESSAGES/base.moAdded
locales/it/LC_MESSAGES/base.poAdded+56
main.pyAdded+64
requirements.txtAdded+5
utils.pyAdded+25