-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
74 lines (64 loc) · 2.22 KB
/
app.py
File metadata and controls
74 lines (64 loc) · 2.22 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
64
65
66
67
68
69
70
71
72
73
74
from flask import Flask, render_template, request, jsonify
from src.database import SessionLocal, engine, Base
from src.nlp_service import NLPService
from src.models import Language
import traceback
app = Flask(__name__, template_folder='templates', static_folder='static')
# Create tables if not exist
Base.metadata.create_all(bind=engine)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/api/languages')
def get_languages():
# Only return languages that are marked 'ready' in the database
db = SessionLocal()
try:
langs = db.query(Language).filter_by(is_ready=True).all()
# Hardcoded Names for display
names = {'hi': 'Hindi', 'kn': 'Kannada', 'fr': 'French', 'es': 'Spanish', 'de': 'German', 'en': 'English'}
response = []
for l in langs:
if l.code == 'en': continue # Skip English in dropdown
response.append({'code': l.code, 'name': names.get(l.code, l.code)})
return jsonify(response)
except Exception as e:
return jsonify({"error": str(e)}), 500
finally:
db.close()
@app.route('/api/process', methods=['POST'])
def process():
db = SessionLocal()
try:
data = request.json
word = data.get('word')
lang = data.get('lang')
service = NLPService(db)
result = service.process_query(word, lang)
return jsonify(result)
except Exception as e:
traceback.print_exc()
return jsonify({"error": str(e)}), 500
finally:
db.close()
@app.route('/api/visualize', methods=['POST'])
def visualize():
db = SessionLocal()
try:
data = request.json
words = data.get('words', [])
service = NLPService(db)
image = service.generate_pca_plot(words)
return jsonify({"image": image})
except Exception as e:
traceback.print_exc()
return jsonify({"error": str(e)}), 500
finally:
db.close()
# DUMMY ROUTE TO STOP 404 ERRORS FROM OLD CACHED JS
@app.route('/api/status')
def status():
return jsonify({"state": "COMPLETED", "progress": 100})
if __name__ == '__main__':
print("🚀 Server Started! Go to http://localhost:5000")
app.run(debug=True, port=5000)