diff --git a/application.py b/application.py index 9155025..650b213 100644 --- a/application.py +++ b/application.py @@ -2,7 +2,7 @@ import boto3 import difflib import os -# from dotenv import load_dotenv +from dotenv import load_dotenv from flask import ( Flask, render_template, @@ -12,10 +12,12 @@ flash ) +import openai + from glossary import Glossary from helpers import generate_star_rating -# load_dotenv() +load_dotenv() application = Flask(__name__) @@ -24,6 +26,9 @@ # read access key and secret access key from environment variables AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID') AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY') +OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') +openai.api_key = OPENAI_API_KEY + s3 = boto3.client(service_name='s3', aws_access_key_id=AWS_ACCESS_KEY_ID, @@ -32,9 +37,26 @@ bucket_name = 'snrazavi' glossary_file_key = 'Glossary_for_ratings.yaml' +GENERATE_DESCRIPTION = False glossary = Glossary("Glossary_for_ratings.yaml", s3, bucket_name, glossary_file_key) +def generate_description(term): + prompt = f"Write a brief description of the term '{term}' in the\ + context of AI and Machine Learning in Persian language.\ + Possibly provide a few links to learn more about the query" + + response = openai.Completion.create( + engine="text-davinci-003", + prompt=prompt, + max_tokens=500, + n=1, + stop=None, + temperature=0.6, + ) + + return response.choices[0].text.strip() + @application.route("/", methods=["GET", "POST"]) def index(): @@ -57,9 +79,16 @@ def index(): else: return render_template("index.html", not_found=True, term=term) else: - return render_template( - "index.html", generate_star_rating=generate_star_rating, - translations=translations, term=term) + if GENERATE_DESCRIPTION: + description = generate_description(term) + return render_template( + "index.html", generate_star_rating=generate_star_rating, + translations=translations, term=term, description=description) + else: + return render_template( + "index.html", generate_star_rating=generate_star_rating, + translations=translations, term=term) + elif "new-english-term" in request.form and "new-persian-translation" in request.form: english_term = request.form.get("new-english-term") persian_translation = request.form.get("new-persian-translation") diff --git a/requirements.txt b/requirements.txt index 8e4edb3..8d6610a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,17 +1,30 @@ +aiohttp==3.8.4 +aiosignal==1.3.1 +async-timeout==4.0.2 +attrs==23.1.0 blinker==1.6.2 boto3==1.26.126 botocore==1.29.126 +certifi==2022.12.7 +charset-normalizer==3.1.0 click==8.1.3 colorama==0.4.6 Flask==2.3.2 +frozenlist==1.3.3 +idna==3.4 itsdangerous==2.1.2 Jinja2==3.1.2 jmespath==1.0.1 MarkupSafe==2.1.2 +multidict==6.0.4 +openai==0.27.6 python-dateutil==2.8.2 python-dotenv==1.0.0 PyYAML==6.0 +requests==2.29.0 s3transfer==0.6.0 six==1.16.0 +tqdm==4.65.0 urllib3==1.26.15 -Werkzeug==2.3.3 \ No newline at end of file +Werkzeug==2.3.3 +yarl==1.9.2 \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index d1342db..836966b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -49,44 +49,11 @@
{{ description }}
+