|
from flask import Flask, request, jsonify |
|
from transformers import pipeline |
|
|
|
|
|
translator = pipeline('translation_en_to_uk', model='Helsinki-NLP/opus-mt-en-uk') |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
@app.route("/") |
|
def home(): |
|
return open("index.html").read() |
|
|
|
@app.route("/translate", methods=["POST"]) |
|
def translate(): |
|
data = request.json |
|
text = data.get("text", "") |
|
if not text: |
|
return jsonify({"error": "No text provided"}), 400 |
|
|
|
translated_text = translator(text, max_length=100)[0]['translation_text'] |
|
return jsonify({"translation": translated_text}) |
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=7860) |
|
|