-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
37 lines (27 loc) · 1.13 KB
/
app.py
File metadata and controls
37 lines (27 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from flask import Flask, render_template, request, jsonify
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
app = Flask(__name__, static_folder='static', template_folder='templates')
# Load model and tokenizer
model_path = "model"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
@app.route('/')
def home():
return render_template('app.html')
@app.route('/translate', methods=['POST'])
def translate():
try:
data = request.get_json()
input_text = data.get("text", "")
if not input_text.strip():
return jsonify({"error": "Input text is empty"}), 400
input_text = input_text.lower()
# Tokenize and translate
inputs = tokenizer.encode(input_text, return_tensors="pt")
outputs = model.generate(inputs)
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
return jsonify({"translation": translation})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)