LingoSynth/lingosynth/app/api.py

29 lines
689 B
Python
Raw Permalink Normal View History

2023-05-11 21:48:16 +02:00
import flask
2023-05-13 14:29:15 +02:00
import time
2023-05-11 21:48:16 +02:00
2023-05-11 23:28:39 +02:00
from . import ai, prompts
2023-05-11 21:48:16 +02:00
def register(app: flask.Flask):
@app.route('/api/rephrase', methods=['GET', 'POST'])
@app.rate_limiter.limit('2 per second')
def api_rephrase():
text = flask.request.args.get('text') or flask.request.json.get('text')
2023-05-11 23:28:39 +02:00
messages = prompts.MESSAGES
messages.append({
'role': 'user',
'content': text
})
2023-05-11 21:48:16 +02:00
2023-05-13 14:29:15 +02:00
try:
generated = ai.generate(messages)
except Exception:
return flask.Response('Sorry, an error has occurred!', status=500)
2023-05-11 21:48:16 +02:00
return flask.jsonify(
{
2023-05-13 14:29:15 +02:00
'text': generated
2023-05-11 21:48:16 +02:00
}
)