johnpaulbin commited on
Commit
30a52ec
1 Parent(s): fba60ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -1
app.py CHANGED
@@ -1,4 +1,7 @@
1
  from flask import Flask, request, jsonify
 
 
 
2
  import asyncio
3
  from hypercorn.asyncio import serve
4
  from hypercorn.config import Config
@@ -66,6 +69,7 @@ def deepmojify(sentence, top_n=5, prob_only=False):
66
  return list_emojis, prob
67
 
68
  app = Flask(__name__)
 
69
 
70
  @app.route('/', methods=['GET'])
71
  def home():
@@ -155,6 +159,7 @@ def random_spanish_pair2():
155
 
156
 
157
 
 
158
  @app.route('/translate', methods=['POST'])
159
  def dotranslate():
160
  data = request.get_json()
@@ -164,7 +169,11 @@ def dotranslate():
164
  dest = data.get('dest', 'es')
165
 
166
  if txt:
167
- translation = translate(txt, dest=dest, src=src)
 
 
 
 
168
  return jsonify({'translation': translation}), 200
169
  else:
170
  return jsonify({'error': 'No text provided'}), 400
 
1
  from flask import Flask, request, jsonify
2
+ from flask_caching import Cache
3
+ import time
4
+
5
  import asyncio
6
  from hypercorn.asyncio import serve
7
  from hypercorn.config import Config
 
69
  return list_emojis, prob
70
 
71
  app = Flask(__name__)
72
+ cache = Cache(app, config={'CACHE_TYPE': 'simple', 'CACHE_DEFAULT_TIMEOUT': 60})
73
 
74
  @app.route('/', methods=['GET'])
75
  def home():
 
159
 
160
 
161
 
162
+
163
  @app.route('/translate', methods=['POST'])
164
  def dotranslate():
165
  data = request.get_json()
 
169
  dest = data.get('dest', 'es')
170
 
171
  if txt:
172
+ cache_key = f"{txt}_{src}_{dest}"
173
+ translation = cache.get(cache_key)
174
+ if translation is None:
175
+ translation = translate(txt, dest=dest, src=src)
176
+ cache.set(cache_key, translation)
177
  return jsonify({'translation': translation}), 200
178
  else:
179
  return jsonify({'error': 'No text provided'}), 400