{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from transformers import AutoModelForSequenceClassification, AutoTokenizer, TextClassificationPipeline\n", "import torch\n", "import gradio as gr\n", "\n", "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "# Load the fine-tuned model and tokenizer\n", "new_model = AutoModelForSequenceClassification.from_pretrained('roberta-rating')\n", "new_tokenizer = AutoTokenizer.from_pretrained('roberta-rating')\n", "\n", "\n", "# Create a classification pipeline\n", "classifier = TextClassificationPipeline(model=new_model, tokenizer=new_tokenizer, device=device)\n", "\n", "# Add label mapping for sentiment analysis (assuming LABEL_0 = 'negative' and LABEL_1 = 'positive')\n", "label_mapping = {1: '1/5', 2: '2/5', 3: '3/5', 4: '4/5', 5: '5/5'}\n", "\n", "def evaluate(text):\n", " result = classifier(text)\n", " return label_mapping[int(result[0]['label'].split('_')[1])] + \".\", result[0]['score']\n", "\n", "iface = gr.Interface(\n", " fn=evaluate,\n", " inputs=gr.Textbox(label=\"Review\"),\n", " outputs=[gr.Textbox(label=\"Evaluation\"), gr.Textbox(label=\"Score\")],\n", " title='Write a review',\n", " description=\"Write a product review, and the model will evaluate its numerical rating\"\n", ")\n", "\n", "iface.launch(share=True)" ] } ], "metadata": { "kernelspec": { "display_name": "SolutionsInPR", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }