{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# |export\n", "import gradio as gr\n", "from transformers import pipeline" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# |export\n", "MODEL_ID = \"facebook/galactica-125m\"\n", "pipe = pipeline(\"text-generation\", model=MODEL_ID)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'generated_text': 'A force of 0.6N is applied to an object, which accelerates at 3m/s. What is its mass? \\n\\nThe mass of the object is $m = \\\\frac{1}{2} \\\\cdot \\\\frac{1}{2} \\\\cdot \\\\frac{1}{2} \\\\cdot \\\\frac{1}{2} \\\\cdot \\\\frac{1}{2}'}]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipe(\n", " \"A force of 0.6N is applied to an object, which accelerates at 3m/s. What is its mass? \",\n", " max_length=100,\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# |export\n", "def generate(text):\n", " return pipe(text, max_length=100)[0][\"generated_text\"]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861\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": [ "(, 'http://127.0.0.1:7861/', None)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# |export\n", "iface = gr.Interface(\n", " fn=generate,\n", " inputs=\"text\",\n", " outputs=\"text\",\n", " examples=[[\"A force of 0.6N is applied to an object, which accelerates at 3m/s. What is its mass? \"]],\n", ")\n", "iface.launch()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7861\n" ] } ], "source": [ "iface.close()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "from nbdev.export import nb_export\n", "\n", "nb_export(\"app.ipynb\", lib_path=\".\", name=\"app\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "hf", "language": "python", "name": "hf" }, "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.8.13" }, "vscode": { "interpreter": { "hash": "66e5af1d4a3a75efffc7cd5a7f382675fc3ac06b0697676e06fa85c907378a99" } } }, "nbformat": 4, "nbformat_minor": 4 }