Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,8 @@ import inflect
|
|
9 |
import re
|
10 |
|
11 |
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
|
|
|
|
12 |
|
13 |
checkpoint = "microsoft/speecht5_tts"
|
14 |
processor = SpeechT5Processor.from_pretrained(checkpoint)
|
@@ -20,26 +22,45 @@ speaker_embeddings = {
|
|
20 |
"BDL": "cmu_us_bdl_arctic-wav-arctic_a0009.npy",
|
21 |
}
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def convert_number_to_words(number: float) -> str:
|
24 |
p = inflect.engine()
|
25 |
words = p.number_to_words(number)
|
26 |
|
27 |
-
# Translate using httpx
|
28 |
-
async def translate_text(text, source_lang, target_lang):
|
29 |
-
async with httpx.AsyncClient() as client:
|
30 |
-
response = await client.get(
|
31 |
-
f'https://api.mymemory.translated.net/get?q={text}&langpair={source_lang}|{target_lang}'
|
32 |
-
)
|
33 |
-
translation = response.json()
|
34 |
-
return translation['responseData']['translatedText']
|
35 |
-
|
36 |
-
# You can change 'en' to the appropriate source language code
|
37 |
-
source_lang = 'en'
|
38 |
-
# You can change 'hy' to the appropriate target language code
|
39 |
-
target_lang = 'hy'
|
40 |
-
|
41 |
# Use asyncio.run even if an event loop is already running (nested asyncio)
|
42 |
-
translated_words =
|
43 |
|
44 |
return translated_words
|
45 |
|
|
|
9 |
import re
|
10 |
|
11 |
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
12 |
+
import requests
|
13 |
+
from requests.exceptions import Timeout
|
14 |
|
15 |
checkpoint = "microsoft/speecht5_tts"
|
16 |
processor = SpeechT5Processor.from_pretrained(checkpoint)
|
|
|
22 |
"BDL": "cmu_us_bdl_arctic-wav-arctic_a0009.npy",
|
23 |
}
|
24 |
|
25 |
+
def translate_text(text):
|
26 |
+
trans_text = ''
|
27 |
+
|
28 |
+
for sent in nlp(text).sents:
|
29 |
+
try:
|
30 |
+
# Add a timeout of 5 seconds (adjust as needed)
|
31 |
+
response = requests.get(
|
32 |
+
"https://translate.googleapis.com/translate_a/single",
|
33 |
+
params={
|
34 |
+
'client': 'gtx',
|
35 |
+
'sl': 'auto',
|
36 |
+
'tl': 'hy',
|
37 |
+
'dt': 't',
|
38 |
+
'q': sent.text,
|
39 |
+
},
|
40 |
+
timeout=50,
|
41 |
+
)
|
42 |
+
response.raise_for_status() # Raise an HTTPError for bad responses
|
43 |
+
|
44 |
+
# Extract the translated text from the response
|
45 |
+
translation = response.json()[0][0][0]
|
46 |
+
|
47 |
+
trans_text += translation
|
48 |
+
|
49 |
+
except Timeout:
|
50 |
+
print("Translation request timed out.")
|
51 |
+
# Handle the timeout as needed
|
52 |
+
except requests.exceptions.RequestException as e:
|
53 |
+
print(f"An error occurred: {e}")
|
54 |
+
# Handle other request exceptions as needed
|
55 |
+
|
56 |
+
return trans_text
|
57 |
+
|
58 |
def convert_number_to_words(number: float) -> str:
|
59 |
p = inflect.engine()
|
60 |
words = p.number_to_words(number)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
# Use asyncio.run even if an event loop is already running (nested asyncio)
|
63 |
+
translated_words = translate_text(words)
|
64 |
|
65 |
return translated_words
|
66 |
|