Created
September 19, 2023 13:52
-
-
Save dillera/3efa529674685b2d23e996fff29d54cc to your computer and use it in GitHub Desktop.
simple openAI translation service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python3 -m venv venv | |
source venv/bin/activate | |
pip install Flask openai | |
python ./app.py | |
---------------------------------------------- | |
from flask import Flask, request, jsonify | |
import openai | |
import os | |
# Set your OpenAI API key here | |
#openai.api_key = os.getenv("OPENAI_API_KEY") | |
openai.api_key = "API" | |
app = Flask(__name__) | |
@app.route('/translate', methods=['POST']) | |
def translate(): | |
data = request.get_json() | |
# Ensure 'text' is provided in the JSON request | |
if 'text' not in data: | |
return jsonify({'error': 'Missing "text" in request'}), 400 | |
text_to_translate = data['text'] | |
# Use ChatGPT API to translate | |
response = openai.Completion.create( | |
model="text-davinci-003", | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant that translates English to Spanish."}, | |
{"role": "user", "content": text_to_translate} | |
] | |
) | |
# Extract the translated message | |
translated_text = response['choices'][0]['message']['content'] | |
return jsonify({'translation': translated_text}) | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment