├── .gitignore ├── LICENSE ├── Procfile ├── README.md ├── app.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 AI Anytime 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: uvicorn app:app --host 0.0.0.0 --port $PORT 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sentiment-Analysis-API 2 | A simple Sentiment Analysis API in FastAPI. 3 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request, redirect, url_for 2 | import numpy as np 3 | from scipy.optimize import minimize 4 | import matplotlib.pyplot as plt 5 | import io 6 | import base64 7 | import time 8 | 9 | app = Flask(__name__) 10 | 11 | # Fonction pour calculer D_AB 12 | def calculate_D_AB(Xa, a_AB, a_BA, λ_a, λ_b, q_a, q_b, D_AB0, D_BA0, T): 13 | Xb = 1 - Xa # Fraction molaire de B 14 | D = Xa*(D_BA0) + Xb*np.log(D_AB0) + \ 15 | 2*(Xa*np.log(Xa+(Xb*λ_b)/λ_a)+Xb*np.log(Xb+(Xa*λ_a)/λ_b)) + \ 16 | 2*Xa*Xb*((λ_a/(Xa*λ_a+Xb*λ_b))*(1-(λ_a/λ_b)) + 17 | (λ_b/(Xa*λ_a+Xb*λ_b))*(1-(λ_b/λ_a))) + \ 18 | Xb*q_a*((1-((Xb*q_b*np.exp(-a_BA/T))/(Xa*q_a+Xb*q_b*np.exp(-a_BA/T)))**2)*(-a_BA/T)+(1-((Xb*q_b)/(Xb*q_b+Xa*q_a*np.exp(-a_AB/T)))**2)*np.exp(-a_AB/T)*(-a_AB/T)) + \ 19 | Xa*q_b*((1-((Xa*q_a*np.exp(-a_AB/T))/(Xa*q_a*np.exp(-a_AB/T)+Xb*q_b))**2)*(-a_AB/T)+(1-((Xa*q_a)/(Xa*q_a+Xb*q_b*np.exp(-a_BA/T)))**2)*np.exp(-a_BA/T)*(-a_BA/T)) 20 | # Calcul de D_AB 21 | return np.exp(D) 22 | 23 | # Fonction objectif pour la minimisation 24 | def objective(params, Xa_values, D_AB_exp, λ_a, λ_b, q_a, q_b, D_AB0, D_BA0, T): 25 | a_AB, a_BA = params 26 | D_AB_calculated = calculate_D_AB(Xa_values, a_AB, a_BA, λ_a, λ_b, q_a, q_b, D_AB0, D_BA0, T) 27 | return np.sum((D_AB_calculated - D_AB_exp)**2) 28 | 29 | @app.route('/') 30 | def input_data(): 31 | return render_template('index.html') 32 | 33 | @app.route('/calculate', methods=['POST']) 34 | def calculate(): 35 | if request.method == 'POST': 36 | D_AB_exp = float(request.form['D_AB_exp']) 37 | T = float(request.form['T']) 38 | Xa = float(request.form['Xa']) 39 | λ_a = eval(request.form['λ_a']) 40 | λ_b = eval(request.form['λ_b']) 41 | q_a = float(request.form['q_a']) 42 | q_b = float(request.form['q_b']) 43 | D_AB0 = float(request.form['D_AB0']) 44 | D_BA0 = float(request.form['D_BA0']) 45 | 46 | # Paramètres initiaux 47 | params_initial = [0, 0] 48 | 49 | # Tolerance 50 | tolerance = 1e-12 51 | 52 | # Nombre maximal d'itérations 53 | max_iterations = 1000 54 | iteration = 0 55 | 56 | # Temps de départ 57 | start_time = time.time() 58 | 59 | # Boucle d'ajustement des paramètres 60 | while iteration < max_iterations: 61 | # Minimisation de l'erreur 62 | result = minimize(objective, params_initial, args=(Xa, D_AB_exp, λ_a, λ_b, q_a, q_b, D_AB0, D_BA0, T), method='Nelder-Mead') 63 | # Paramètres optimisés 64 | a_AB_opt, a_BA_opt = result.x 65 | # Calcul de D_AB avec les paramètres optimisés 66 | D_AB_opt = calculate_D_AB(Xa, a_AB_opt, a_BA_opt, λ_a, λ_b, q_a, q_b, D_AB0, D_BA0, T) 67 | # Calcul de l'erreur 68 | error = np.abs(D_AB_opt - D_AB_exp) 69 | # Vérifier si la différence entre les paramètres optimisés est inférieure à la tolérance 70 | if np.max(np.abs(np.array(params_initial) - np.array([a_AB_opt, a_BA_opt]))) < tolerance: 71 | break 72 | # Mise à jour des paramètres initiaux 73 | params_initial = [a_AB_opt, a_BA_opt] 74 | # Incrémentation du nombre d'itérations 75 | iteration += 1 76 | 77 | # Temps d'exécution 78 | execution_time = time.time() - start_time 79 | 80 | # Générer la courbe 81 | Xa_values = np.linspace(0, 0.7, 100) # Fraction molaire de A 82 | D_AB_values = calculate_D_AB(Xa_values, a_AB_opt, a_BA_opt, λ_a, λ_b, q_a, q_b, D_AB0, D_BA0, T) 83 | plt.plot(Xa_values, D_AB_values) 84 | plt.xlabel('Fraction molaire de A') 85 | plt.ylabel('Coefficient de diffusion (cm^2/s)') 86 | plt.title('Variation du coefficient de diffusion en fonction du fraction molaire') 87 | plt.grid(True) 88 | 89 | # Convertir le graphique en une représentation base64 90 | buffer = io.BytesIO() 91 | plt.savefig(buffer, format='png') 92 | buffer.seek(0) 93 | graph = base64.b64encode(buffer.getvalue()).decode() 94 | plt.close() 95 | 96 | # Affichage des résultats 97 | return render_template('result.html', a_AB_opt=a_AB_opt, a_BA_opt=a_BA_opt, D_AB_opt=D_AB_opt, error=error, iteration=iteration, execution_time=execution_time, graph=graph) 98 | 99 | if __name__ == '__main__': 100 | app.run(debug=True) 101 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | textblob 2 | fastapi 3 | uvicorn 4 | flask 5 | --------------------------------------------------------------------------------