NCTCMumbai
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -17,9 +17,11 @@ from huggingface_hub import InferenceClient
|
|
17 |
# Bhashini API translation function
|
18 |
api_key = getenv('API_KEY')
|
19 |
user_id = getenv('USER_ID')
|
20 |
-
|
21 |
-
def bhashini_translate(text: str, from_code: str = "en", to_code: str = "te") -> dict:
|
22 |
"""Translates text from source language to target language using the Bhashini API."""
|
|
|
|
|
|
|
23 |
url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
|
24 |
headers = {
|
25 |
"Content-Type": "application/json",
|
@@ -30,14 +32,21 @@ def bhashini_translate(text: str, from_code: str = "en", to_code: str = "te") ->
|
|
30 |
"pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
|
31 |
"pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
|
32 |
}
|
|
|
|
|
33 |
response = requests.post(url, json=payload, headers=headers)
|
34 |
|
35 |
if response.status_code != 200:
|
|
|
36 |
return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
|
37 |
|
|
|
38 |
response_data = response.json()
|
39 |
service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
|
40 |
callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
|
|
|
|
|
|
|
41 |
headers2 = {
|
42 |
"Content-Type": "application/json",
|
43 |
response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
|
@@ -47,13 +56,18 @@ def bhashini_translate(text: str, from_code: str = "en", to_code: str = "te") ->
|
|
47 |
"inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
|
48 |
}
|
49 |
|
|
|
50 |
compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
|
|
|
51 |
if compute_response.status_code != 200:
|
|
|
52 |
return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
|
53 |
|
|
|
54 |
compute_response_data = compute_response.json()
|
55 |
translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
|
56 |
|
|
|
57 |
return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
|
58 |
|
59 |
|
|
|
17 |
# Bhashini API translation function
|
18 |
api_key = getenv('API_KEY')
|
19 |
user_id = getenv('USER_ID')
|
20 |
+
def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
|
|
|
21 |
"""Translates text from source language to target language using the Bhashini API."""
|
22 |
+
print(f'Starting translation process from {from_code} to {to_code}...')
|
23 |
+
gr.Warning(f'Translating to {to_code}...')
|
24 |
+
|
25 |
url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
|
26 |
headers = {
|
27 |
"Content-Type": "application/json",
|
|
|
32 |
"pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
|
33 |
"pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
|
34 |
}
|
35 |
+
|
36 |
+
print('Sending initial request to get the pipeline...')
|
37 |
response = requests.post(url, json=payload, headers=headers)
|
38 |
|
39 |
if response.status_code != 200:
|
40 |
+
print(f'Error in initial request: {response.status_code}')
|
41 |
return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
|
42 |
|
43 |
+
print('Initial request successful, processing response...')
|
44 |
response_data = response.json()
|
45 |
service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
|
46 |
callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
|
47 |
+
|
48 |
+
print(f'Service ID: {service_id}, Callback URL: {callback_url}')
|
49 |
+
|
50 |
headers2 = {
|
51 |
"Content-Type": "application/json",
|
52 |
response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
|
|
|
56 |
"inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
|
57 |
}
|
58 |
|
59 |
+
print(f'Sending translation request with text: "{text}"')
|
60 |
compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
|
61 |
+
|
62 |
if compute_response.status_code != 200:
|
63 |
+
print(f'Error in translation request: {compute_response.status_code}')
|
64 |
return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
|
65 |
|
66 |
+
print('Translation request successful, processing translation...')
|
67 |
compute_response_data = compute_response.json()
|
68 |
translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
|
69 |
|
70 |
+
print(f'Translation successful. Translated content: "{translated_content}"')
|
71 |
return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
|
72 |
|
73 |
|