from flask import Flask, request, jsonify, make_response from flasgger import Swagger, swag_from from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer import random import csv app = Flask(__name__) swagger = Swagger(app) # Définition du titre de la documentation Swagger app.config['SWAGGER'] = { 'title': 'API Analyse de sentiment', 'description': 'Documentation de l\'API pour l\'analyse de sentiment', 'version': '1.0.0' } # Charger les informations d'authentification à partir du fichier credentials.csv def load_credentials(file_path): credentials = {} with open(file_path, newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: credentials[row['username']] = {'password': row['password'], 'v1': int(row['v1']), 'v2': int(row['v2'])} return credentials credentials = load_credentials('credentials.csv') # Route pour vérifier l'état de l'API @app.route('/status') def status(): """ API Status --- responses: 200: description: API is working examples: message: 1 """ return '1' # Route pour saluer l'utilisateur @app.route('/welcome') def welcome(): """ Message de bienvenue --- parameters: - name: username in: query type: string required: true description: Message de bienvenue à l'utilisateur responses: 200: description: Message examples: message: Bienvenue sur l'API d'Analyse de sentiment, Olivier! """ username = request.args.get('username') return f"Bienvenue sur l'API d'Analyse de sentiment {username}!" # Route pour vérifier les credentials de l'utilisateur @app.route('/credentials', methods=['POST']) def check_credentials(): """ credentials Route --- consumes: - application/json parameters: - in: header name: Authorization type: string required: true description: The user's credentials in the format "username=password" responses: 200: description: A list of user credentials examples: username: John v1: 1 v2: 0 """ auth = request.headers.get('Authorization') if not auth: return 'Unauthorized', 401 username, password = auth.split('=') if username not in credentials or credentials[username]['password'] != password: return 'Unauthorized', 401 response_data = {'username': username, 'v1': credentials[username]['v1'], 'v2': credentials[username]['v2']} response = make_response(jsonify(response_data), 200) response.headers['Content-Type'] = 'application/json' return response # Route pour l'analyse de sentiment avec le modèle v1 @app.route('/v1/sentiment', methods=['POST']) def sentiment_v1(): """ Sentiment Analysis Route (Model v1) --- parameters: - in: header name: Authorization type: string required: true description: The user's credentials in the format "username=password" - in: formData # Modification ici pour spécifier le type de données du formulaire name: sentence type: string required: true description: The sentence to analyze responses: 200: description: The sentiment score examples: score: 0.5 """ auth = request.headers.get('Authorization') if not auth: return 'Unauthorized', 401 username, password = auth.split('=') if username not in credentials or credentials[username]['password'] != password: return 'Unauthorized (wrong username and/or password)', 401 if credentials[username]['v1']==0: return 'User unauthorized for Sentiment Analysis v1', 401 sentence = request.form.get('sentence') if not sentence: return 'Bad Request', 400 # Implémentation factice de l'analyse de sentiment pour le modèle v1 (nombre aléatoire entre -1 et 1) sentiment_score = random.uniform(-1, 1) return str(sentiment_score) # Route pour l'analyse de sentiment avec le modèle v2 @app.route('/v2/sentiment', methods=['POST']) def sentiment_v2(): """ Sentiment Analysis Route (Model v2) --- parameters: - in: header name: Authorization type: string required: true description: The user's credentials in the format "username=password" - in: formData # Modification ici pour spécifier le type de données du formulaire name: sentence type: string required: true description: The sentence to analyze responses: 200: description: The sentiment score examples: score: 0.5 """ auth = request.headers.get('Authorization') if not auth: return 'Unauthorized', 401 username, password = auth.split('=') if username not in credentials or credentials[username]['password'] != password: return 'Unauthorized (wrong username and/or password)', 401 if credentials[username]['v2']==0: return 'User unauthorized for Sentiment Analysis v2', 401 sentence = request.form.get('sentence') if not sentence: return 'Bad Request', 400 # Implémentation de l'analyse de sentiment avec le modèle v2 (VaderSentiment) analyzer = SentimentIntensityAnalyzer() sentiment_score = analyzer.polarity_scores(sentence)['compound'] return str(sentiment_score) if __name__ == '__main__': app.run(host="0.0.0.0",debug=True)