-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
165 lines (159 loc) · 6.26 KB
/
Copy pathdatabase.py
File metadata and controls
165 lines (159 loc) · 6.26 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import pandas as pd
from sqlalchemy import create_engine, text
import streamlit as st
from contextlib import contextmanager
@st.cache_resource
def get_database_connection():
"""Create database connection using environment variables."""
connection_string = os.getenv('DATABASE_URL')
if not connection_string:
connection_string = (
f"postgresql://{os.getenv('PGUSER')}:{os.getenv('PGPASSWORD')}@"
f"{os.getenv('PGHOST')}:{os.getenv('PGPORT')}/{os.getenv('PGDATABASE')}"
)
return create_engine(connection_string, pool_pre_ping=True)
@contextmanager
def get_db_session():
"""Context manager for database sessions."""
engine = get_database_connection()
connection = engine.connect()
try:
yield connection
connection.commit()
except Exception:
connection.rollback()
raise
finally:
connection.close()
@st.cache_data
def load_language_data():
"""Load language data from PostgreSQL database with NMT pair information."""
with get_db_session() as connection:
query = """
WITH ordered_pairs AS (
SELECT
l1.id as source_id,
l2.lang_name as target_name,
l2.id as target_id,
ARRAY[
ST_Y(l2.coordinates::geometry),
ST_X(l2.coordinates::geometry)
]::float[] as target_coords,
nps.chrf_plus,
nps.spbleu_spm_200
FROM language_new l1
JOIN nmt_pairs_source nps ON l1.id = nps.source_lang_id OR l1.id = nps.target_lang_id
JOIN language_new l2 ON
(nps.source_lang_id = l2.id OR nps.target_lang_id = l2.id) AND
l2.id != l1.id
WHERE l2.coordinates IS NOT NULL
AND ST_IsValid(l2.coordinates::geometry)
ORDER BY l2.lang_name
),
lang_connections AS (
SELECT
source_id as lang_id,
array_agg(DISTINCT target_name) as connected_languages,
array_agg(DISTINCT target_coords) FILTER (WHERE
target_coords[1] BETWEEN -90 AND 90
AND target_coords[2] BETWEEN -180 AND 180
) as connected_coords,
array_agg(DISTINCT target_id) as connected_lang_ids,
array_agg(DISTINCT chrf_plus) as chrf_scores,
array_agg(DISTINCT spbleu_spm_200) as bleu_scores
FROM ordered_pairs
GROUP BY source_id
)
SELECT
l.id,
l.lang_name as name,
l.iso_code,
ST_Y(l.coordinates::geometry) as latitude,
ST_X(l.coordinates::geometry) as longitude,
ARRAY[
CASE WHEN l.asr THEN 'ASR' END,
CASE WHEN l.nmt THEN 'NMT' END,
CASE WHEN l.tts THEN 'TTS' END
] as available_models,
(SELECT COUNT(*)
FROM nmt_pairs_source nps
WHERE nps.source_lang_id = l.id OR nps.target_lang_id = l.id) as nmt_pair_count,
COALESCE(lc.connected_languages, ARRAY[]::text[]) as connected_languages,
COALESCE(lc.connected_coords, ARRAY[]::float[][]) as connected_coords,
COALESCE(lc.connected_lang_ids, ARRAY[]::integer[]) as connected_lang_ids,
COALESCE(lc.chrf_scores, ARRAY[]::float[]) as chrf_scores,
COALESCE(lc.bleu_scores, ARRAY[]::float[]) as bleu_scores,
CASE WHEN lc.lang_id IS NOT NULL THEN TRUE ELSE FALSE END as has_nmt_pair
FROM language_new l
LEFT JOIN lang_connections lc ON l.id = lc.lang_id
WHERE l.coordinates IS NOT NULL
AND ST_IsValid(l.coordinates::geometry)
AND ST_X(l.coordinates::geometry) BETWEEN -180 AND 180
AND ST_Y(l.coordinates::geometry) BETWEEN -90 AND 90
ORDER BY l.lang_name
"""
return pd.read_sql(query, connection)
@st.cache_data
def get_model_types():
"""Get unique model types from database."""
return ['ASR', 'NMT', 'TTS']
@st.cache_data
def get_language_nmt_pairs(language_id):
"""Get NMT pairs for a specific language."""
with get_db_session() as connection:
query = """
SELECT
nps.chrf_plus as chrf_score,
nps.spbleu_spm_200 as bleu_score,
source_lang.lang_name AS source_language,
target_lang.lang_name AS target_language,
CASE
WHEN source_lang.id = :lang_id THEN 'Source'
ELSE 'Target'
END as role
FROM
nmt_pairs_source nps
LEFT JOIN
language_new AS source_lang
ON
nps.source_lang_id = source_lang.id
LEFT JOIN
language_new AS target_lang
ON
nps.target_lang_id = target_lang.id
WHERE
nps.source_lang_id IS NOT NULL
AND nps.target_lang_id IS NOT NULL
AND (source_lang.id = :lang_id OR target_lang.id = :lang_id)
ORDER BY
role, nps.chrf_plus DESC NULLS LAST;
"""
return pd.read_sql(text(query), connection, params={'lang_id': language_id})
@st.cache_data
def get_all_nmt_pairs():
"""Get all NMT pairs with their scores."""
with get_db_session() as connection:
query = """
SELECT
nps.chrf_plus as chrf_score,
nps.spbleu_spm_200 as bleu_score,
source_lang.lang_name AS source_language,
target_lang.lang_name AS target_language
FROM
nmt_pairs_source nps
LEFT JOIN
language_new AS source_lang
ON
nps.source_lang_id = source_lang.id
LEFT JOIN
language_new AS target_lang
ON
nps.target_lang_id = target_lang.id
WHERE
nps.source_lang_id IS NOT NULL
AND nps.target_lang_id IS NOT NULL
ORDER BY
nps.chrf_plus DESC NULLS LAST;
"""
return pd.read_sql(query, connection)