{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "description: Gradio app.py\n", "output-file: app.html\n", "title: app\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#: eval: false\n", "load_dotenv()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Put the chat backend pieces together" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "skip\n" ] } ], "source": [ "def create_demo(bot: ConversationBot):\n", " sample_images = []\n", " all_imgs = [f\"{SAMPLE_IMG_DIR}/{img}\" for img in os.listdir(SAMPLE_IMG_DIR)]\n", " for i, img in enumerate(all_imgs):\n", " if i in [\n", " 1,\n", " 2,\n", " 3,\n", " ]:\n", " sample_images.append(img)\n", " with gr.Blocks() as demo:\n", " gr_img = gr.Image(type=\"filepath\")\n", " btn = gr.Button(value=\"Submit image\")\n", " ingredients_msg = gr.Text(label=\"Ingredients from image\")\n", " btn.click(bot.run_img, inputs=[gr_img], outputs=[ingredients_msg])\n", " gr.Examples(\n", " examples=sample_images,\n", " inputs=gr_img,\n", " )\n", "\n", " chatbot = gr.Chatbot(\n", " value=[(None, bot.ai_prompt_questions[\"ingredients\"].prompt.template)]\n", " )\n", "\n", " msg = gr.Textbox()\n", " # clear = gr.Button(\"Clear\")\n", " gr.Markdown(\n", " \"\"\"\n", " **🔃Refresh the page to start from scratch🔃** \n", " \n", " Recipe search tool powered by the [Edamam API](https://www.edamam.com/) \n", " \n", " ![Edamam Logo](https://www.edamam.com/assets/img/small-logo.png)\n", " \"\"\"\n", " )\n", " msg.submit(\n", " fn=bot.respond, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False\n", " )\n", " # clear.click(lambda: None, None, chatbot, queue=False).then(bot.reset)\n", " return demo" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7860\n", "Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#| eval: false\n", "if \"demo\" in globals():\n", " demo.close()\n", "\n", "demo = create_demo(\n", " ConversationBot(\n", " vegan_ingred_finder=vegan_ingred_finder, img_cap=img_cap, verbose=True\n", " )\n", ")\n", "demo.launch()" ] } ], "metadata": { "kernelspec": { "display_name": "python3", "language": "python", "name": "python3" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }