Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -12,10 +12,12 @@
flash
)

import openai

from glossary import Glossary
from helpers import generate_star_rating

# load_dotenv()
load_dotenv()


application = Flask(__name__)
Expand All @@ -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,
Expand All @@ -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():
Expand All @@ -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")
Expand Down
15 changes: 14 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Werkzeug==2.3.3
yarl==1.9.2
42 changes: 8 additions & 34 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,11 @@ <h3>{{ term }}:</h3>
{% endfor %}
</select>
<span class="star-rating" title="Current Rating: {{ translation.rating }}">
{{ generate_star_rating(translation.rating)|safe }}
{{ generate_star_rating(translation.rating)|safe }} ({{ translation.rating_no | default(0) }})
</span>
<button type="submit" class="btn btn-sm btn-outline-primary">Submit</button>
</form>
</li>
<!-- <li class="list-group-item d-flex justify-content-between align-items-center">
{{ translation.persian }}
<span class="star-rating" title="Rating: {{ translation.rating }}">
{{ generate_star_rating(translation.rating)|safe }}
</span>
<form action="/" method="post" class="d-inline">
<input type="hidden" name="term" value="{{ term }}">
<input type="hidden" name="translation_index" value="{{ loop.index0 }}">
<input type="hidden" name="rating_change" value="1">
<button type="submit" class="btn btn-sm btn-outline-primary">+</button>
</form>
<form action="/" method="post" class="d-inline">
<input type="hidden" name="term" value="{{ term }}">
<input type="hidden" name="translation_index" value="{{ loop.index0 }}">
<input type="hidden" name="rating_change" value="-1">
<button type="submit" class="btn btn-sm btn-outline-primary">-</button>
</form>
</li> -->
<!-- <li class="list-group-item d-flex justify-content-between align-items-center">
{{ translation.persian }} ({{ translation.rating }})
<form action="/rate_translation" method="post">
<input type="hidden" name="english_term" value="{{ term }}">
<input type="hidden" name="persian_translation" value="{{ translation.persian }}">
<select name="rating" class="form-select form-select-sm">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<button type="submit" class="btn btn-sm btn-outline-primary ms-2">Rate</button>
</form>
</li> -->
{% endfor %}
</ul>
</div>
Expand All @@ -108,6 +75,13 @@ <h4>Add this term and its translation to the dictionary:</h4>
</div>
</form>
{% endif %}

{% if description %}
<div class="description">
<h3>Description</h3>
<p>{{ description }}</p>
</div>
{% endif %}
</div>
</body>
</html>