Demosthene-OR commited on
Commit
2b879a3
1 Parent(s): 48058de

Add application file

Browse files
Files changed (4) hide show
  1. Dockerfile +11 -0
  2. app.py +189 -0
  3. credentials.csv +95 -0
  4. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.8
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify, make_response
2
+ from flasgger import Swagger, swag_from
3
+ from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
4
+ import random
5
+ import csv
6
+
7
+ app = Flask(__name__)
8
+ swagger = Swagger(app)
9
+
10
+ # Définition du titre de la documentation Swagger
11
+ app.config['SWAGGER'] = {
12
+ 'title': 'API Analyse de sentiment',
13
+ 'description': 'Documentation de l\'API pour l\'analyse de sentiment',
14
+ 'version': '1.0.0'
15
+ }
16
+
17
+ # Charger les informations d'authentification à partir du fichier credentials.csv
18
+ def load_credentials(file_path):
19
+ credentials = {}
20
+ with open(file_path, newline='') as csvfile:
21
+ reader = csv.DictReader(csvfile)
22
+ for row in reader:
23
+ credentials[row['username']] = {'password': row['password'], 'v1': int(row['v1']), 'v2': int(row['v2'])}
24
+ return credentials
25
+
26
+ credentials = load_credentials('credentials.csv')
27
+
28
+ # Route pour vérifier l'état de l'API
29
+ @app.route('/status')
30
+ def status():
31
+ """
32
+ API Status
33
+ ---
34
+ responses:
35
+ 200:
36
+ description: API is working
37
+ examples:
38
+ message: 1
39
+ """
40
+ return '1'
41
+
42
+
43
+
44
+ # Route pour saluer l'utilisateur
45
+ @app.route('/welcome')
46
+ def welcome():
47
+ """
48
+ Message de bienvenue
49
+ ---
50
+ parameters:
51
+ - name: username
52
+ in: query
53
+ type: string
54
+ required: true
55
+ description: Message de bienvenue à l'utilisateur
56
+ responses:
57
+ 200:
58
+ description: Message
59
+ examples:
60
+ message: Bienvenue sur l'API d'Analyse de sentiment, Olivier!
61
+ """
62
+ username = request.args.get('username')
63
+ return f"Bienvenue sur l'API d'Analyse de sentiment {username}!"
64
+
65
+
66
+
67
+ # Route pour vérifier les credentials de l'utilisateur
68
+ @app.route('/credentials', methods=['POST'])
69
+ def check_credentials():
70
+ """
71
+ credentials Route
72
+ ---
73
+ consumes:
74
+ - application/json
75
+ parameters:
76
+ - in: header
77
+ name: Authorization
78
+ type: string
79
+ required: true
80
+ description: The user's credentials in the format "username=password"
81
+ responses:
82
+ 200:
83
+ description: A list of user credentials
84
+ examples:
85
+ username: John
86
+ v1: 1
87
+ v2: 0
88
+ """
89
+ auth = request.headers.get('Authorization')
90
+ if not auth:
91
+ return 'Unauthorized', 401
92
+
93
+ username, password = auth.split('=')
94
+ if username not in credentials or credentials[username]['password'] != password:
95
+ return 'Unauthorized', 401
96
+
97
+ response_data = {'username': username, 'v1': credentials[username]['v1'], 'v2': credentials[username]['v2']}
98
+ response = make_response(jsonify(response_data), 200)
99
+ response.headers['Content-Type'] = 'application/json'
100
+ return response
101
+
102
+
103
+ # Route pour l'analyse de sentiment avec le modèle v1
104
+ @app.route('/v1/sentiment', methods=['POST'])
105
+ def sentiment_v1():
106
+ """
107
+ Sentiment Analysis Route (Model v1)
108
+ ---
109
+ parameters:
110
+ - in: header
111
+ name: Authorization
112
+ type: string
113
+ required: true
114
+ description: The user's credentials in the format "username=password"
115
+ - in: formData # Modification ici pour spécifier le type de données du formulaire
116
+ name: sentence
117
+ type: string
118
+ required: true
119
+ description: The sentence to analyze
120
+ responses:
121
+ 200:
122
+ description: The sentiment score
123
+ examples:
124
+ score: 0.5
125
+ """
126
+ auth = request.headers.get('Authorization')
127
+ if not auth:
128
+ return 'Unauthorized', 401
129
+
130
+ username, password = auth.split('=')
131
+ if username not in credentials or credentials[username]['password'] != password:
132
+ return 'Unauthorized (wrong username and/or password)', 401
133
+
134
+ if credentials[username]['v1']==0:
135
+ return 'User unauthorized for Sentiment Analysis v1', 401
136
+
137
+ sentence = request.form.get('sentence')
138
+ if not sentence:
139
+ return 'Bad Request', 400
140
+
141
+ # Implémentation factice de l'analyse de sentiment pour le modèle v1 (nombre aléatoire entre -1 et 1)
142
+ sentiment_score = random.uniform(-1, 1)
143
+ return str(sentiment_score)
144
+
145
+ # Route pour l'analyse de sentiment avec le modèle v2
146
+ @app.route('/v2/sentiment', methods=['POST'])
147
+ def sentiment_v2():
148
+ """
149
+ Sentiment Analysis Route (Model v2)
150
+ ---
151
+ parameters:
152
+ - in: header
153
+ name: Authorization
154
+ type: string
155
+ required: true
156
+ description: The user's credentials in the format "username=password"
157
+ - in: formData # Modification ici pour spécifier le type de données du formulaire
158
+ name: sentence
159
+ type: string
160
+ required: true
161
+ description: The sentence to analyze
162
+ responses:
163
+ 200:
164
+ description: The sentiment score
165
+ examples:
166
+ score: 0.5
167
+ """
168
+ auth = request.headers.get('Authorization')
169
+ if not auth:
170
+ return 'Unauthorized', 401
171
+
172
+ username, password = auth.split('=')
173
+ if username not in credentials or credentials[username]['password'] != password:
174
+ return 'Unauthorized (wrong username and/or password)', 401
175
+
176
+ if credentials[username]['v2']==0:
177
+ return 'User unauthorized for Sentiment Analysis v2', 401
178
+
179
+ sentence = request.form.get('sentence')
180
+ if not sentence:
181
+ return 'Bad Request', 400
182
+
183
+ # Implémentation de l'analyse de sentiment avec le modèle v2 (VaderSentiment)
184
+ analyzer = SentimentIntensityAnalyzer()
185
+ sentiment_score = analyzer.polarity_scores(sentence)['compound']
186
+ return str(sentiment_score)
187
+
188
+ if __name__ == '__main__':
189
+ app.run(host="0.0.0.0",debug=True)
credentials.csv ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ username,password,v1,v2
2
+ Quinlan,5210,0,1
3
+ Davis,5783,0,0
4
+ Montana,3134,0,0
5
+ Quintessa,8790,0,1
6
+ Camden,4837,0,0
7
+ Megan,6837,1,1
8
+ Liberty,5564,1,1
9
+ Zelda,8610,0,1
10
+ Mara,9820,1,0
11
+ Steven,5998,1,1
12
+ Rhiannon,3545,1,0
13
+ Julian,7520,0,1
14
+ Anika,8944,0,1
15
+ Justine,9156,1,0
16
+ Lionel,4527,0,1
17
+ Yoshi,4237,1,1
18
+ Brian,7263,1,1
19
+ Leila,8308,0,1
20
+ Piper,2914,1,0
21
+ Yen,3932,1,0
22
+ Amber,9274,1,1
23
+ Rhonda,6818,1,0
24
+ Lamar,6478,0,0
25
+ Nolan,4611,1,1
26
+ Ulla,3610,0,1
27
+ Ginger,6723,0,1
28
+ Geraldine,5725,0,1
29
+ Dominic,7914,1,1
30
+ Paula,8403,1,0
31
+ Hashim,7658,1,1
32
+ Demetria,2132,1,1
33
+ Wang,7829,1,0
34
+ Tyler,4453,0,0
35
+ Kim,7134,0,1
36
+ Edward,1871,1,1
37
+ Russell,3039,0,0
38
+ Malachi,5226,1,0
39
+ Barrett,2891,1,0
40
+ Harriet,6877,0,0
41
+ Portia,8105,0,0
42
+ Raya,3006,1,1
43
+ Reed,1745,0,1
44
+ Penelope,4844,1,0
45
+ Whilemina,6551,1,1
46
+ Fay,9208,0,0
47
+ Shad,4903,0,0
48
+ Pandora,2598,1,1
49
+ Abbot,3545,0,0
50
+ Ciaran,2336,0,1
51
+ Nehru,8209,1,0
52
+ Logan,9622,0,1
53
+ Vernon,1422,0,1
54
+ Gavin,9632,0,1
55
+ Dana,5425,1,0
56
+ Ashton,1128,0,0
57
+ Mark,4608,1,1
58
+ Akeem,7908,0,0
59
+ Richard,2816,1,1
60
+ Colby,2427,1,1
61
+ Yasir,9460,1,0
62
+ Octavia,6792,1,1
63
+ Noel,3409,0,1
64
+ Michael,3341,0,0
65
+ Joel,3447,0,1
66
+ Stacy,9140,1,0
67
+ Mason,6588,1,1
68
+ Kelsey,3801,0,1
69
+ Daria,3036,1,1
70
+ Joy,1306,1,1
71
+ Xantha,8532,0,0
72
+ Lael,4088,1,1
73
+ Ursa,5331,1,0
74
+ Hayes,5297,0,1
75
+ Neve,4972,1,0
76
+ Bradley,6463,1,1
77
+ Sonya,7745,0,0
78
+ Adam,5910,0,1
79
+ Alfonso,8611,1,1
80
+ Jin,4199,0,1
81
+ Jared,2126,0,0
82
+ Constance,6624,1,1
83
+ Myra,2262,1,1
84
+ Emerald,5253,0,0
85
+ Remedios,2766,1,1
86
+ Shannon,2704,0,0
87
+ Steel,2034,1,0
88
+ Xena,7153,1,0
89
+ Ann,3068,0,1
90
+ Hadassah,3371,0,1
91
+ Xavier,8313,0,0
92
+ Hall,5494,1,0
93
+ Conan,3708,0,0
94
+ Buffy,5225,1,0
95
+ Chava,2094,0,1
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ flask
2
+ vaderSentiment
3
+ flasgger