{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "# Gradio Example Notebook" ], "metadata": { "id": "-Ix16ey2erLl" } }, { "cell_type": "markdown", "source": [ " A separate notebook that demonstrates various Gradio components. If needed,use hardcoded data to show the functionality of the Gradio components.\n" ], "metadata": { "id": "3dxNBkEhexES" } }, { "cell_type": "markdown", "source": [ "## Code" ], "metadata": { "id": "YlfGrVVBe16M" } }, { "cell_type": "markdown", "source": [ "### Libraries\n", "\n", "Start by installing and importing necessary libraries" ], "metadata": { "id": "7lICoKpGe4kP" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "JBJQeBn0YrwE" }, "outputs": [], "source": [ "!pip install gradio\n", "!pip install wget\n", "!pip install transformers" ] }, { "cell_type": "code", "source": [ "import gradio as gr # Gradio library to create an interactive interface\n", "from transformers import pipeline # Transformers libraries which imports pipeline to use Hugging-Face models\n", "import pandas as pd # Pandas library for data manipulation and analysis" ], "metadata": { "id": "OTYay5Uge-Jw" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "### Initialize Sentiment Analyzer and Define Function\n", "\n", "Here the sentiment-analyzer is initialized using the Hugging-Face pipeline, Also the function that will be used by the Gradio" ], "metadata": { "id": "Dx7kdVxhfQ0l" } }, { "cell_type": "code", "source": [ "# Initialize the analyzer\n", "\n", "# Loads a pretrained model for the English language\n", "analyzer = pipeline(\"sentiment-analysis\")" ], "metadata": { "collapsed": true, "id": "RwSoWhwosP3a" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Define Function\n", "\n", "def sentiment_analysis(filename):\n", " with open(filename, \"r\") as fn: # Open the file and read the sentences\n", " sentences = fn.readlines()\n", " result = analyzer(sentences) # Store the analyzer results for each sentence\n", " df = pd.DataFrame(result) # Convert the results in a formatted table\n", " df['sentences'] = sentences # add column for sentences\n", " return df" ], "metadata": { "id": "dGn2a4A4fQGl" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "### Build Gradio Interface\n", "\n", "\n", "Here, The Gradio interface is set up using the following components:\n", "\n", "**A file uploader** allows user to upload a text file which contains the sentences to be analyzed\n", "\n", "**DataFrame Output** Displays program's output in a structured table format\n", "\n", "**Title and Description**: Provides clear title and description for the interface" ], "metadata": { "id": "IHHnKudGfFtI" } }, { "cell_type": "code", "source": [ "#Create the gradio interface\n", "demo = gr.Interface(\n", " fn=sentiment_analysis, # Function used by gradio\n", " inputs=gr.File(label=\"Upload a file (txt)\"), # User inputs (file)\n", " outputs=gr.Dataframe(label='Results'), # Program's output (DataFrame)\n", " title='Sentiment-Analysis',\n", " description=\"Gradio interface that allows users to upload a text file containing sentences to be analyzed, the sentences will be classified and results will be in a formatted table\"\n", ")\n", "demo.launch(debug=True) # \"Debug=True\" Displays inerface errors" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 680 }, "id": "xE3M_HFgfN4L", "outputId": "d647960b-41c8-4a7d-b67d-bb3055c77721" }, "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n", "\n", "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n", "Running on public URL: https://934478ea31903b3a25.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "
" ] }, "metadata": {} }, { "output_type": "stream", "name": "stdout", "text": [ "Keyboard interruption in main thread... closing server.\n", "Killing tunnel 127.0.0.1:7860 <> https://934478ea31903b3a25.gradio.live\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [] }, "metadata": {}, "execution_count": 9 } ] } ] }