{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "8804bc60-c9e2-4713-9800-1bc2fea11241", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/umesh/conda/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "from transformers import pipeline" ] }, { "cell_type": "code", "execution_count": 3, "id": "322cd68e-9257-4d07-aafc-19e9072f1bba", "metadata": {}, "outputs": [], "source": [ "from transformers import AutoModelForSequenceClassification, AutoTokenizer\n", "\n", "model = AutoModelForSequenceClassification.from_pretrained('.')\n", "tokenizer = AutoTokenizer.from_pretrained('.')" ] }, { "cell_type": "code", "execution_count": 4, "id": "fde22c8f-4d57-4544-bc2b-55e4dbbc5fd3", "metadata": {}, "outputs": [], "source": [ "classifier = pipeline('sentiment-analysis',model = model, tokenizer = tokenizer, top_k =1)\n", "det_classer = pipeline('sentiment-analysis',model = model, tokenizer = tokenizer, top_k =None)" ] }, { "cell_type": "code", "execution_count": 5, "id": "087c3506-5d27-4dd6-9377-fb34e6926698", "metadata": {}, "outputs": [], "source": [ "inputs=['This is a rather confusing statement','I do not want to bee seen with you Kusakabe','Rin chan was elegant in dismissing your favours','Damn you! Bonn-kun']" ] }, { "cell_type": "code", "execution_count": 6, "id": "f5683415-239a-4e68-8f4c-1b9ba8295787", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[{'label': 'DISGUST', 'score': 0.2899283468723297}],\n", " [{'label': 'DISGUST', 'score': 0.20709046721458435}],\n", " [{'label': 'OPTIMISM', 'score': 0.1878553181886673}],\n", " [{'label': 'ANGER', 'score': 0.46816307306289673}]]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classifier(inputs)" ] }, { "cell_type": "code", "execution_count": 7, "id": "9b45cd86-cd63-40b1-81ff-7b510ac94c8d", "metadata": {}, "outputs": [], "source": [ "statement='Today I did not finish my project and stayed silently away from meetings but my boss found it out and shouted at me in front of a female colleague '" ] }, { "cell_type": "code", "execution_count": 8, "id": "3ae20452-e327-4550-a40d-f28280aad3c8", "metadata": {}, "outputs": [], "source": [ "x = det_classer(statement)" ] }, { "cell_type": "code", "execution_count": 9, "id": "ba091f5c-b98c-49ee-a361-f8937c3693c1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'DISGUST'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[0][0]['label']" ] }, { "cell_type": "code", "execution_count": 14, "id": "003693e3-1aac-4d67-9c54-f17bcda1af34", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'DISGUST': 0.3289419114589691}\n", "{'ANGER': 0.2868015170097351}\n", "{'SADNESS': 0.12498752772808075}\n", "{'FEAR': 0.09882446378469467}\n", "{'ANTICIPATION': 0.051536574959754944}\n", "{'JOY': 0.03244535252451897}\n", "{'SURPRISE': 0.023728473111987114}\n", "{'PESSIMISM': 0.020501434803009033}\n", "{'OPTIMISM': 0.020457664504647255}\n", "{'COMPLICATED': 0.006496347486972809}\n", "{'TRUST': 0.0026527740992605686}\n", "{'LOVE': 0.0026260693557560444}\n" ] } ], "source": [ "for a in x[0]:\n", " print ({a['label']:a['score']})" ] }, { "cell_type": "code", "execution_count": 23, "id": "a7361b72-d959-4993-80cf-5c0d65f3e703", "metadata": {}, "outputs": [], "source": [ "def classify (statement):\n", " preds = det_classer(statement)\n", " return {i['label']:float(i['score']) for i in preds[0]}" ] }, { "cell_type": "code", "execution_count": 16, "id": "9b6bff6a-3a70-48d8-8b03-1d8327278cad", "metadata": {}, "outputs": [], "source": [ "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 21, "id": "d1aa90b3-678f-4ae0-bfa7-6018034c5b3e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7862\n", "IMPORTANT: You are using gradio version 4.26.0, however version 4.29.0 is available, please upgrade.\n", "--------\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": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demo = gr.Interface(fn=classify, inputs='text',outputs=gr.Label())\n", "demo.launch()" ] }, { "cell_type": "code", "execution_count": null, "id": "e7852458-9afc-4443-bfc8-ebd16909dc6a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }