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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import pymongo
|
4 |
+
import pandas as pd
|
5 |
+
from bson import ObjectId
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import requests
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
video_status = {
|
12 |
+
"Not Started": "NOT_STARTED",
|
13 |
+
"Dubbing Started": "DUBBING_STARTED",
|
14 |
+
"Waiting QA": "WAITING_QA",
|
15 |
+
"Sent to Client": "SENT_TO_CLIENT",
|
16 |
+
"Approved": "APPROVED",
|
17 |
+
"Rejected": "REJECTED",
|
18 |
+
"Churned": "CHURNED",
|
19 |
+
"Re-review Requested": "RE_REVIEW_REQUESTED"
|
20 |
+
}
|
21 |
+
|
22 |
+
ALL_LANGUAGES = ['Hindi', 'English', 'Bengali', 'Kannada', 'Spanish', 'French', 'German', 'Italian', 'Tamil', 'Telugu']
|
23 |
+
|
24 |
+
language_maps = {
|
25 |
+
"Hindi": "hi",
|
26 |
+
"English": "en",
|
27 |
+
"Spanish": "es",
|
28 |
+
"French": "fr",
|
29 |
+
"German": "de",
|
30 |
+
"Italian": "it",
|
31 |
+
"Tamil": "ta",
|
32 |
+
"Telugu": "te",
|
33 |
+
"Bengali": "bn",
|
34 |
+
"Kannada": "kn",
|
35 |
+
"hi": "Hindi",
|
36 |
+
"en": "English",
|
37 |
+
"es": "Spanish",
|
38 |
+
"fr": "French",
|
39 |
+
"de": "German",
|
40 |
+
"it": "Italian",
|
41 |
+
"ta": "Tamil",
|
42 |
+
"te": "Telugu",
|
43 |
+
"bn": "Bengali",
|
44 |
+
"kn": "Kannada"
|
45 |
+
}
|
46 |
+
|
47 |
+
REVIEW_LIST_API = "https://api.dev-env.deepsync.co/api/v1/dashboard/dubbed/videos/review/requested"
|
48 |
+
|
49 |
+
def get_translators(platform):
|
50 |
+
if platform == "dev":
|
51 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_DEV_URI"))
|
52 |
+
source_db = client.deepsync_stage
|
53 |
+
else:
|
54 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_URI"))
|
55 |
+
source_db = client.deepsync_prod
|
56 |
+
translators = source_db.translators.find({})
|
57 |
+
transaltors_data = []
|
58 |
+
for translator in translators:
|
59 |
+
try:
|
60 |
+
data = source_db.companies.find_one({"_id": translator["translator"]})
|
61 |
+
transaltors_data.append({
|
62 |
+
"_id": str(translator["_id"]),
|
63 |
+
"Name": data["name"],
|
64 |
+
"Email": data["email"],
|
65 |
+
"Source Language": ", ".join(list(map(lambda x: language_maps[x], translator["sourceAccent"]))),
|
66 |
+
"Target Language": ", ".join(list(map(lambda x: language_maps[x], translator["targetAccent"])))
|
67 |
+
})
|
68 |
+
except:
|
69 |
+
pass
|
70 |
+
return pd.DataFrame(transaltors_data)
|
71 |
+
|
72 |
+
|
73 |
+
def add_translator(platform, email_id, source_langs, target_langs, contact_num, pricing):
|
74 |
+
if platform == "dev":
|
75 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_DEV_URI"))
|
76 |
+
source_db = client.deepsync_stage
|
77 |
+
else:
|
78 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_URI"))
|
79 |
+
source_db = client.deepsync_prod
|
80 |
+
|
81 |
+
try:
|
82 |
+
company_id_validity = source_db.companies.find_one({"email": email_id})
|
83 |
+
except:
|
84 |
+
company_id_validity = None
|
85 |
+
if company_id_validity is None:
|
86 |
+
return "Invalid Company ID. Unable to add translator."
|
87 |
+
|
88 |
+
if source_langs is None:
|
89 |
+
source_accents = []
|
90 |
+
else:
|
91 |
+
source_accents = [language_maps[l] for l in source_langs]
|
92 |
+
if target_langs is None:
|
93 |
+
target_accents = []
|
94 |
+
else:
|
95 |
+
target_accents = [language_maps[l] for l in target_langs]
|
96 |
+
|
97 |
+
response = source_db.translators.insert_one({
|
98 |
+
"translator": company_id_validity["_id"],
|
99 |
+
"targetAccent": target_accents,
|
100 |
+
"sourceAccent": source_accents,
|
101 |
+
"contactNum": None if len(contact_num.strip()) == 0 else contact_num.strip(),
|
102 |
+
"pricing": None if pricing is None or pricing == -1 else pricing
|
103 |
+
})
|
104 |
+
return f"Translator added succesfully, ID : {str(response.inserted_id)}"
|
105 |
+
|
106 |
+
|
107 |
+
def get_translator_names(platform):
|
108 |
+
if platform == "dev":
|
109 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_DEV_URI"))
|
110 |
+
source_db = client.deepsync_stage
|
111 |
+
else:
|
112 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_URI"))
|
113 |
+
source_db = client.deepsync_prod
|
114 |
+
translators = source_db.translators.find({})
|
115 |
+
translators_names = []
|
116 |
+
for translator in translators:
|
117 |
+
try:
|
118 |
+
data = source_db.companies.find_one({"_id": translator["translator"]})
|
119 |
+
translators_names.append(data["name"])
|
120 |
+
except:
|
121 |
+
pass
|
122 |
+
return gr.Dropdown.update(choices=translators_names)
|
123 |
+
|
124 |
+
|
125 |
+
def select_current_data(platform, translator_name):
|
126 |
+
if translator_name is None:
|
127 |
+
return "Please select a translator."
|
128 |
+
if platform == "dev":
|
129 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_DEV_URI"))
|
130 |
+
source_db = client.deepsync_stage
|
131 |
+
else:
|
132 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_URI"))
|
133 |
+
source_db = client.deepsync_prod
|
134 |
+
companies = source_db.companies.find({"name": translator_name})
|
135 |
+
translators = list(source_db.translators.find({}))
|
136 |
+
translator_company_ids = {tr["translator"]: tr["_id"] for tr in translators if "translator" in tr}
|
137 |
+
company_id = None
|
138 |
+
for company in companies:
|
139 |
+
if company["_id"] in translator_company_ids.keys():
|
140 |
+
company_id = company["_id"]
|
141 |
+
break
|
142 |
+
for tr in translators:
|
143 |
+
if tr["_id"] == translator_company_ids[company_id]:
|
144 |
+
translator = tr
|
145 |
+
break
|
146 |
+
return gr.CheckboxGroup.update(value=[language_maps[l] for l in translator["sourceAccent"]]), gr.CheckboxGroup.update(value=[language_maps[l] for l in translator["targetAccent"]]), gr.Textbox.update(value="" if translator["contactNum"] is None else translator["contactNum"]), gr.Textbox.update(value=-1 if translator["pricing"] is None else translator["pricing"])
|
147 |
+
|
148 |
+
|
149 |
+
def update_translator(platform, translator_name, source_languages, target_languages, contact_num, pricing):
|
150 |
+
if translator_name is None:
|
151 |
+
return "Please select a translator."
|
152 |
+
if platform == "dev":
|
153 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_DEV_URI"))
|
154 |
+
source_db = client.deepsync_stage
|
155 |
+
else:
|
156 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_URI"))
|
157 |
+
source_db = client.deepsync_prod
|
158 |
+
companies = source_db.companies.find({"name": translator_name})
|
159 |
+
translators = list(source_db.translators.find({}))
|
160 |
+
translator_company_ids = {tr["translator"]: tr["_id"] for tr in translators if "translator" in tr}
|
161 |
+
company_id = None
|
162 |
+
for company in companies:
|
163 |
+
if company["_id"] in translator_company_ids.keys():
|
164 |
+
company_id = company["_id"]
|
165 |
+
break
|
166 |
+
source_accents = [language_maps[l] for l in source_languages]
|
167 |
+
target_accents = [language_maps[l] for l in target_languages]
|
168 |
+
|
169 |
+
updateable_data = {
|
170 |
+
"sourceAccent": source_accents,
|
171 |
+
"targetAccent": target_accents,
|
172 |
+
"contactNum": None if len(contact_num.strip()) == 0 else contact_num.strip(),
|
173 |
+
"pricing": None if pricing is None or pricing == -1 else pricing
|
174 |
+
}
|
175 |
+
|
176 |
+
response = source_db.translators.update_one(
|
177 |
+
{"_id": translator_company_ids[company_id]},
|
178 |
+
{"$set": updateable_data}
|
179 |
+
)
|
180 |
+
return f"Updated translator accent succesfully."
|
181 |
+
|
182 |
+
|
183 |
+
def get_videos_under_review(status_keys):
|
184 |
+
headers = {
|
185 |
+
"x-api-key": os.environ.get('X-API-KEY'),
|
186 |
+
"Content-Type": "application/json"
|
187 |
+
}
|
188 |
+
if status_keys is None or len(status_keys) == 0:
|
189 |
+
API_URL = "https://api.dev-env.deepsync.co/api/v1/dashboard/dubbed/videos/review/requested"
|
190 |
+
response = requests.get(API_URL, headers=headers)
|
191 |
+
if response.status_code == 200:
|
192 |
+
options = [f"{str(v['_id'])} / {v['title']}" for v in response.json()['data'] if 'title' in v]
|
193 |
+
return gr.Dropdown.update(choices=options, value=None)
|
194 |
+
else:
|
195 |
+
return gr.Dropdown.update(choices=[], value=None)
|
196 |
+
else:
|
197 |
+
options = []
|
198 |
+
for status in status_keys:
|
199 |
+
API_URL = f"https://api.dev-env.deepsync.co/api/v1/dashboard/dubbed/videos/review/requested/{video_status[status]}"
|
200 |
+
response = requests.get(API_URL, headers=headers)
|
201 |
+
if response.status_code == 200:
|
202 |
+
current_options = [f"{str(v['_id'])} / {v['title']}" for v in response.json()['data'] if 'title' in v]
|
203 |
+
options.extend(current_options)
|
204 |
+
return gr.Dropdown.update(choices=options, value=None)
|
205 |
+
|
206 |
+
|
207 |
+
def get_traslators_assign(video):
|
208 |
+
video_id = video.split("/", maxsplit=1)[0].strip()
|
209 |
+
client = pymongo.MongoClient(os.environ.get("MONGO_DEV_URI"))
|
210 |
+
source_db = client.deepsync_stage
|
211 |
+
video = source_db.dubbedvideos.find_one({"_id": ObjectId(video_id)})
|
212 |
+
source_language = video["sourceLanguage"].split("-")[0]
|
213 |
+
target_language = video["targetLanguage"].split("-")[0]
|
214 |
+
translators = source_db.translators.find({"sourceAccent": {"$elemMatch": {"$eq": source_language}}, "targetAccent": {"$elemMatch": {"$eq": target_language}}})
|
215 |
+
translators_list = []
|
216 |
+
for translator in translators:
|
217 |
+
company = source_db.companies.find_one({"_id": translator["translator"]})
|
218 |
+
translators_list.append(f"{company['name']} / {str(translator['_id'])}")
|
219 |
+
|
220 |
+
button = gr.Button.update(interactive=False)
|
221 |
+
if "videoReviewInformation" in video:
|
222 |
+
if video["videoReviewInformation"]["status"] in {"NOT_STARTED", "DUBBING_STARTED", "WAITING_QA"}:
|
223 |
+
button = gr.Button.update(interactive=True)
|
224 |
+
return gr.Dropdown.update(choices=translators_list), language_maps[source_language], language_maps[target_language], button
|
225 |
+
|
226 |
+
|
227 |
+
def assign_translator(translator, video, role):
|
228 |
+
translator_id = translator.rsplit("/", maxsplit=1)[1].strip()
|
229 |
+
video_id = video.split("/", maxsplit=1)[0].strip()
|
230 |
+
headers = {
|
231 |
+
"x-api-key": os.environ.get('X-API-KEY'),
|
232 |
+
"Content-Type": "application/json"
|
233 |
+
}
|
234 |
+
role = "QA" if role == "QA" else "translator"
|
235 |
+
API_URL = f"https://api.dev-env.deepsync.co/api/v1/dashboard/dubbed/video/assign/{role}"
|
236 |
+
data = {
|
237 |
+
"videoId": video_id,
|
238 |
+
"userId": translator_id
|
239 |
+
}
|
240 |
+
response = requests.post(API_URL, json=data, headers=headers)
|
241 |
+
return str(response.status_code)
|
242 |
+
|
243 |
+
|
244 |
+
with gr.Blocks() as demo:
|
245 |
+
gr.Markdown("# Translators Admin Dashboard")
|
246 |
+
with gr.Tab("List"):
|
247 |
+
source_platform = gr.Radio(["dev", "prod"], value="prod", label="Source Platform")
|
248 |
+
refresh_list = gr.Button("Load")
|
249 |
+
translators_list = gr.Dataframe(label="Translators", interactive=False, max_rows=20, overflow_row_behaviour="paginate")
|
250 |
+
with gr.Tab("Add"):
|
251 |
+
gr.Markdown("Add a new translator")
|
252 |
+
source_platform_add = gr.Radio(["dev", "prod"], value="dev", label="Source Platform")
|
253 |
+
email_id = gr.Textbox(label="Email ID", lines=1)
|
254 |
+
source_languages = gr.CheckboxGroup(ALL_LANGUAGES, label="Source Languages")
|
255 |
+
target_languages = gr.CheckboxGroup(ALL_LANGUAGES, label="Target Languages")
|
256 |
+
contact_num_box = gr.Textbox(label="Contact Number", lines=1)
|
257 |
+
pricing_box = gr.Number(label="Pricing", value=-1)
|
258 |
+
submit = gr.Button("Submit")
|
259 |
+
result = gr.Textbox(label="Logs")
|
260 |
+
with gr.Tab("Update"):
|
261 |
+
gr.Markdown("Update Translator")
|
262 |
+
source_platform_update = gr.Radio(["dev", "prod"], value="dev", label="Source Platform")
|
263 |
+
refresh_update_platform = gr.Button("Load")
|
264 |
+
translator_name = gr.Dropdown([], label="Translators")
|
265 |
+
source_languages_update = gr.CheckboxGroup(ALL_LANGUAGES, label="Source Languages")
|
266 |
+
target_languages_update = gr.CheckboxGroup(ALL_LANGUAGES, label="Target Languages")
|
267 |
+
contact_num_update = gr.Textbox(label="Contact Number", lines=1)
|
268 |
+
pricing_update = gr.Number(label="Pricing")
|
269 |
+
update_language = gr.Button("Update")
|
270 |
+
result_update = gr.Textbox(label="Logs")
|
271 |
+
with gr.Tab("Assign"):
|
272 |
+
gr.Markdown("Assign Translator / QA")
|
273 |
+
filter_videos_assign = gr.CheckboxGroup(['Not Started', 'Dubbing Started', 'Waiting QA', 'Sent to Client', 'Approved', 'Rejected', 'Churned', 'Re-review Requested'], label="Filter videos. Do not select any of them for videos without request info.")
|
274 |
+
refresh_assign = gr.Button("Load Videos")
|
275 |
+
under_review_videos_list = gr.Dropdown([], label="Video IDs")
|
276 |
+
with gr.Row():
|
277 |
+
source_language_box = gr.Textbox(label="Source Language")
|
278 |
+
target_language_box = gr.Textbox(label="Target Language")
|
279 |
+
translator_name_assign = gr.Dropdown([], label="Translators")
|
280 |
+
role_assign = gr.Radio(["Translator", "QA"], label="Role", value="Translator")
|
281 |
+
submit_assign_button = gr.Button("Assign Role")
|
282 |
+
assign_logs = gr.Textbox(label="Logs")
|
283 |
+
|
284 |
+
|
285 |
+
# For list
|
286 |
+
refresh_list.click(get_translators, source_platform, translators_list)
|
287 |
+
source_platform.change(get_translators, source_platform, translators_list)
|
288 |
+
|
289 |
+
# For add
|
290 |
+
submit.click(add_translator, [source_platform_add, email_id, source_languages, target_languages, contact_num_box, pricing_box], result)
|
291 |
+
|
292 |
+
# For Update
|
293 |
+
refresh_update_platform.click(get_translator_names, source_platform_update, translator_name)
|
294 |
+
source_platform_update.change(get_translator_names, source_platform_update, translator_name)
|
295 |
+
translator_name.change(select_current_data, [source_platform_update, translator_name], [source_languages_update, target_languages_update, contact_num_update, pricing_update])
|
296 |
+
update_language.click(update_translator, [source_platform_update, translator_name, source_languages_update, target_languages_update, contact_num_update, pricing_update], result_update)
|
297 |
+
|
298 |
+
# For Assign
|
299 |
+
refresh_assign.click(get_videos_under_review, filter_videos_assign, under_review_videos_list)
|
300 |
+
under_review_videos_list.change(get_traslators_assign, under_review_videos_list, [translator_name_assign, source_language_box, target_language_box, submit_assign_button])
|
301 |
+
submit_assign_button.click(assign_translator, [translator_name_assign, under_review_videos_list, role_assign], assign_logs)
|
302 |
+
|
303 |
+
|
304 |
+
if __name__=="__main__":
|
305 |
+
demo.launch(share=True, auth=(os.environ.get("GRADIO_USERNAME"), os.environ.get("GRADIO_PASSWORD")))
|