Last active
April 4, 2023 04:20
-
-
Save halr9000/2a9742137d02eea2ca89db2ee8ebd8cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Gradio Quickstart\n", | |
"Source: https://gradio.app/quickstart/" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Install requirements\n", | |
"%pip install gradio" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# simple form with slider\n", | |
"import gradio as gr\n", | |
"\n", | |
"def greet(name, is_morning, temperature):\n", | |
" salutation = \"Good morning\" if is_morning else \"Good evening\"\n", | |
" greeting = f\"{salutation} {name}. It is {temperature} degrees today\"\n", | |
" celsius = (temperature - 32) * 5 / 9\n", | |
" return greeting, round(celsius, 2)\n", | |
"\n", | |
"demo = gr.Interface(\n", | |
" fn=greet,\n", | |
" inputs=[\"text\", \"checkbox\", gr.Slider(0, 100)],\n", | |
" outputs=[\"text\", \"number\"],\n", | |
")\n", | |
"\n", | |
"demo.launch() " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# working with images\n", | |
"import numpy as np\n", | |
"import gradio as gr\n", | |
"\n", | |
"def sepia(input_img):\n", | |
" sepia_filter = np.array([\n", | |
" [0.393, 0.769, 0.189], \n", | |
" [0.349, 0.686, 0.168], \n", | |
" [0.272, 0.534, 0.131]\n", | |
" ])\n", | |
" sepia_img = input_img.dot(sepia_filter.T)\n", | |
" sepia_img /= sepia_img.max()\n", | |
" return sepia_img\n", | |
"\n", | |
"demo = gr.Interface(sepia, gr.Image(shape=(200, 200)), \"image\")\n", | |
"demo.launch()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Hello blocks\n", | |
"import gradio as gr\n", | |
"\n", | |
"def greet(name):\n", | |
" return \"Hello \" + name + \"!\"\n", | |
"\n", | |
"with gr.Blocks() as demo:\n", | |
" name = gr.Textbox(label=\"Name\")\n", | |
" output = gr.Textbox(label=\"Output Box\")\n", | |
" greet_btn = gr.Button(\"Greet\")\n", | |
" greet_btn.click(fn=greet, inputs=name, outputs=output)\n", | |
"\n", | |
"demo.launch(share=True)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"gr.Interface.load(\"spaces/eugenesiow/remove-bg\", inputs=\"webcam\", title=\"Remove your webcam background!\").launch()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# create image from text\n", | |
"from PIL import Image, ImageDraw, ImageFont\n", | |
"import os\n", | |
"\n", | |
"def text_on_img(filename='01.png', text=\"Hello\", size=12):\n", | |
"\t\"Draw a text on an Image, saves it, show it\"\n", | |
"\tfnt = ImageFont.truetype('DeliciousHandrawn-Regular.ttf', 52)\n", | |
"\t# create image\n", | |
"\timage = Image.new(mode = \"RGB\", size = (200,70), color = \"red\")\n", | |
"\tdraw = ImageDraw.Draw(image)\n", | |
"\t# draw text\n", | |
"\tdraw.text((10,10), text, font=fnt, fill=(255,255,0))\n", | |
"\t# save file\n", | |
"\timage.save(filename)\n", | |
"\t# show file\n", | |
"\tos.system(filename)\n", | |
"\n", | |
"\n", | |
"text_on_img(text=\"Text\", size=52)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": ".venv", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.9" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment