AhmedAlyouSIF commited on
Commit
24a4ce1
·
verified ·
1 Parent(s): 7d84f55

Upload analyze_sentiment_w_gradio.ipynb

Browse files
Files changed (1) hide show
  1. analyze_sentiment_w_gradio.ipynb +142 -0
analyze_sentiment_w_gradio.ipynb ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "metadata": {
7
+ "id": "MuswhH2SXxcn"
8
+ },
9
+ "outputs": [],
10
+ "source": [
11
+ "# Install required libraries\n",
12
+ "!pip install gradio transformers torch"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": null,
18
+ "metadata": {
19
+ "id": "17-gxYX7Y9NP"
20
+ },
21
+ "outputs": [],
22
+ "source": [
23
+ "# Import necessary libraries\n",
24
+ "import gradio as gr\n",
25
+ "import torch\n",
26
+ "from transformers import pipeline"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": null,
32
+ "metadata": {
33
+ "id": "UlX7j4-7Y_Hl"
34
+ },
35
+ "outputs": [],
36
+ "source": [
37
+ "# Detect GPU availability and set the device\n",
38
+ "device = 0 if torch.cuda.is_available() else -1\n",
39
+ "print(f\"Using device: {'GPU' if torch.cuda.is_available() else 'CPU'}\")"
40
+ ]
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "execution_count": null,
45
+ "metadata": {
46
+ "id": "CQj6Q5PlZEIt"
47
+ },
48
+ "outputs": [],
49
+ "source": [
50
+ "# Load the sentiment analysis pipeline on the appropriate device\n",
51
+ "sentiment_pipeline = pipeline(\"sentiment-analysis\", device=device)"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "code",
56
+ "execution_count": null,
57
+ "metadata": {
58
+ "id": "1v6_XfCyfblX"
59
+ },
60
+ "outputs": [],
61
+ "source": [
62
+ "# Function to analyze sentiment\n",
63
+ "def analyze_sentiment(text):\n",
64
+ " result = sentiment_pipeline(text)\n",
65
+ " label = result[0]['label']\n",
66
+ " score = result[0]['score']\n",
67
+ " return f\"Sentiment: {label} (Confidence: {score:.2f})\""
68
+ ]
69
+ },
70
+ {
71
+ "cell_type": "code",
72
+ "execution_count": null,
73
+ "metadata": {
74
+ "id": "bkH7BGHLZDRr"
75
+ },
76
+ "outputs": [],
77
+ "source": [
78
+ "# Define Gradio interface\n",
79
+ "examples = [\n",
80
+ " [\"I absolutely love this new phone! The camera is amazing.\"],\n",
81
+ " [\"The food was terrible, and the service was even worse.\"],\n",
82
+ " [\"It's an average experience, nothing too special.\"],\n",
83
+ "]\n"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "execution_count": null,
89
+ "metadata": {
90
+ "id": "dlY15g1mXvkV"
91
+ },
92
+ "outputs": [],
93
+ "source": [
94
+ "iface = gr.Interface(\n",
95
+ " fn=analyze_sentiment,\n",
96
+ " inputs=gr.Textbox(label=\"Enter text\"),\n",
97
+ " outputs=gr.Textbox(label=\"Sentiment Analysis Result\"),\n",
98
+ " examples=examples,\n",
99
+ " title=\"Sentiment Analysis App\",\n",
100
+ " description=\"Enter a sentence to analyze its sentiment (Positive/Negative/Neutral).\",\n",
101
+ " theme=\"compact\",\n",
102
+ ")"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": null,
108
+ "metadata": {
109
+ "id": "ekuCMwoSXub8"
110
+ },
111
+ "outputs": [],
112
+ "source": [
113
+ "# Launch the app\n",
114
+ "iface.launch()"
115
+ ]
116
+ }
117
+ ],
118
+ "metadata": {
119
+ "colab": {
120
+ "provenance": []
121
+ },
122
+ "kernelspec": {
123
+ "display_name": "Python 3",
124
+ "language": "python",
125
+ "name": "python3"
126
+ },
127
+ "language_info": {
128
+ "codemirror_mode": {
129
+ "name": "ipython",
130
+ "version": 3
131
+ },
132
+ "file_extension": ".py",
133
+ "mimetype": "text/x-python",
134
+ "name": "python",
135
+ "nbconvert_exporter": "python",
136
+ "pygments_lexer": "ipython3",
137
+ "version": "3.8.3"
138
+ }
139
+ },
140
+ "nbformat": 4,
141
+ "nbformat_minor": 1
142
+ }