File size: 1,259 Bytes
e88d83b
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: diffusers_with_batching"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio torch transformers diffusers"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import torch\n", "from diffusers import DiffusionPipeline  # type: ignore\n", "import gradio as gr\n", "\n", "generator = DiffusionPipeline.from_pretrained(\"CompVis/ldm-text2im-large-256\")\n", "# move to GPU if available\n", "if torch.cuda.is_available():\n", "    generator = generator.to(\"cuda\")\n", "\n", "def generate(prompts):\n", "  images = generator(list(prompts)).images  # type: ignore\n", "  return [images]\n", "\n", "demo = gr.Interface(generate,\n", "             \"textbox\",\n", "             \"image\",\n", "             batch=True,\n", "             max_batch_size=4  # Set the batch size based on your CPU/GPU memory\n", ")\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}