{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "2534f67e", "metadata": {}, "outputs": [], "source": [ "# Installing Gradio\n", "!pip install gradio transformers -q\n", "\n", "# Import the required Libraries\n", "import gradio as gr\n", "import numpy as np\n", "import pandas as pd\n", "import pickle\n", "import transformers\n", "from transformers import AutoTokenizer \n", "from transformers import AutoConfig\n", "from transformers import AutoModelForSequenceClassification\n", "from transformers import TFAutoModelForSequenceClassification\n", "from transformers import pipeline\n", "from scipy.special import softmax" ] }, { "cell_type": "code", "execution_count": 2, "id": "1dc8e034", "metadata": {}, "outputs": [], "source": [ "# Requirements\n", "model_path =\"HOLYBOY/Sentiment_Analysis\"\n", "tokenizer = AutoTokenizer.from_pretrained(model_path)\n", "config = AutoConfig.from_pretrained(model_path)\n", "model = AutoModelForSequenceClassification.from_pretrained(model_path)" ] }, { "cell_type": "code", "execution_count": 3, "id": "02f78081", "metadata": {}, "outputs": [], "source": [ "# Preprocess text (username and link placeholders)\n", "def preprocess(text):\n", " new_text = []\n", " for t in text.split(\" \"):\n", " t = \"@user\" if t.startswith(\"@\") and len(t) > 1 else t\n", " t = \"http\" if t.startswith(\"http\") else t\n", " new_text.append(t)\n", " return \" \".join(new_text)" ] }, { "cell_type": "code", "execution_count": 4, "id": "70126857", "metadata": {}, "outputs": [], "source": [ "# ---- Function to process the input and return prediction\n", "def sentiment_analysis(text):\n", " text = preprocess(text)\n", "\n", " encoded_input = tokenizer(text, return_tensors = \"pt\") # for PyTorch-based models\n", " output = model(**encoded_input)\n", " scores_ = output[0][0].detach().numpy()\n", " scores_ = softmax(scores_)\n", " \n", " # Format output dict of scores\n", " labels = [\"Negative\", \"Neutral\", \"Positive\"]\n", " scores = {l:float(s) for (l,s) in zip(labels, scores_) }\n", " \n", " return scores" ] }, { "cell_type": "code", "execution_count": 10, "id": "4901894b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7865\n", "\n", "Could not create share link. Please check your internet connection or our status page: https://status.gradio.app. \n", "\n", "Also please ensure that your antivirus or firewall is not blocking the binary file located at: C:\\Users\\user\\anaconda3\\lib\\site-packages\\gradio\\frpc_windows_amd64_v0.2\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "