-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
63 lines (50 loc) · 2.14 KB
/
app.py
File metadata and controls
63 lines (50 loc) · 2.14 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from flask import Flask, render_template, request, jsonify
from googletrans import Translator
import os
app = Flask(__name__)
translator = Translator()
static_dir = os.path.join(os.path.dirname(__file__), 'static')
if not os.path.exists(static_dir):
try:
os.makedirs(static_dir)
print(f"Created static directory at: {static_dir}")
except OSError as e:
print(f"Error creating static directory: {e}")
@app.route("/")
def home():
"""Serves the main HTML page."""
return render_template("index.html")
@app.route("/translate", methods=["POST"])
def translate_text():
"""Translates text using googletrans."""
data = request.json
text = data.get("text")
target_lang = data.get("target_lang")
if not text or not target_lang:
return jsonify({"error": "Missing text or target language"}), 400
try:
if not text.strip():
return jsonify({"translated_text": ""})
translated = translator.translate(text, dest=target_lang)
return jsonify({"translated_text": translated.text})
except Exception as e:
print(f"Translation Error: {e}")
error_message = "Translation failed. Please check the input or try again later."
if "invalid destination language" in str(e).lower():
error_message = "Invalid target language selected."
return jsonify({"error": error_message}), 500
@app.route("/detect_lang", methods=["GET"])
def detect_lang():
"""Detects the language of the provided text."""
text = request.args.get("text", "")
if not text or not text.strip():
return jsonify({"detected_lang": "N/A", "confidence": 0})
try:
detected = translator.detect(text)
confidence = detected.confidence if detected.confidence is not None else 0
return jsonify({"detected_lang": detected.lang, "confidence": confidence})
except Exception as e:
print(f"Detection Error: {e}")
return jsonify({"error": "Language detection failed.", "detected_lang": "Error", "confidence": 0}), 500
if __name__ == "__main__":
app.run(debug=True)