File size: 1,775 Bytes
a9cce51 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
{
"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
}
|