-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
59 lines (48 loc) · 1.79 KB
/
app.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from flask import Flask, request, jsonify, send_from_directory, current_app
from flask_cors import CORS
from translate import TranslationService
import os
app = Flask(__name__)
app.static_folder = 'frontend' # Set the static folder
app.static_url_path = '/static' # Set the static URL path
CORS(app) # Enable CORS for all routes
translation_service = TranslationService()
@app.route('/')
def index():
"""Serve the main HTML page"""
try:
return current_app.send_static_file('index.html')
except Exception as e:
return str(e), 500
@app.route('/api/detect', methods=['POST'])
def detect_language():
"""Detect the language of the input text"""
try:
data = request.get_json()
text = data.get('text', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
detected_lang = translation_service.detect_language(text)
return jsonify({'detected_language': detected_lang})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/translate', methods=['POST'])
def translate_text():
"""Translate text from source language to target language"""
try:
data = request.get_json()
text = data.get('text', '')
target_lang = data.get('target_lang', 'en')
source_lang = data.get('source_lang', 'auto')
if not text:
return jsonify({'error': 'No text provided'}), 400
translated_text = translation_service.translate(
text,
target_lang=target_lang,
source_lang=source_lang
)
return jsonify({'translated_text': translated_text})
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)