Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -38,7 +38,37 @@ col1, col2= st.columns(2)
|
|
38 |
|
39 |
|
40 |
with col1:
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Code in the left column
|
43 |
st.markdown(
|
44 |
"<h1 style='text-align: center; font-family: Times New Roman;'>Choose Your Country</h1>",
|
@@ -69,10 +99,41 @@ with col1:
|
|
69 |
country = "Chinese"
|
70 |
open("country.txt", "w").write("Chinese")
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
with col2:
|
78 |
# Code in the left column
|
|
|
38 |
|
39 |
|
40 |
with col1:
|
41 |
+
def text_to_speech(text, filename="audio_file.mp3", speaker=1, language="th", volume=1, speed=1):
|
42 |
+
url = "https://api-voice.botnoi.ai/openapi/v1/generate_audio"
|
43 |
+
payload = {
|
44 |
+
"text": text,
|
45 |
+
"speaker": speaker,
|
46 |
+
"volume": volume,
|
47 |
+
"speed": speed,
|
48 |
+
"type_media": "mp3",
|
49 |
+
"save_file": "true",
|
50 |
+
"language": language
|
51 |
+
}
|
52 |
+
headers = {
|
53 |
+
'Botnoi-Token': 'ZHBaNlR4WEI3dWgyZGVRajRMaGt5S3NXeUVZMjU2MTg5NA==',
|
54 |
+
'Content-Type': 'application/json'
|
55 |
+
}
|
56 |
+
|
57 |
+
try:
|
58 |
+
response = requests.post(url, headers=headers, json=payload)
|
59 |
+
response.raise_for_status()
|
60 |
+
data = response.json()
|
61 |
+
audio_url = data.get("audio_url")
|
62 |
+
if audio_url:
|
63 |
+
audio_response = requests.get(audio_url)
|
64 |
+
audio_response.raise_for_status()
|
65 |
+
with open(filename, "wb") as file:
|
66 |
+
file.write(audio_response.content)
|
67 |
+
print(f"Audio downloaded successfully as '{filename}'.")
|
68 |
+
else:
|
69 |
+
print("Audio URL not found in the response.")
|
70 |
+
except requests.exceptions.RequestException as e:
|
71 |
+
print(f"An error occurred: {e}")
|
72 |
# Code in the left column
|
73 |
st.markdown(
|
74 |
"<h1 style='text-align: center; font-family: Times New Roman;'>Choose Your Country</h1>",
|
|
|
99 |
country = "Chinese"
|
100 |
open("country.txt", "w").write("Chinese")
|
101 |
|
102 |
+
text_var = st.text_input("Enter text value:", value="")
|
103 |
+
|
104 |
+
if text_var is not None:
|
105 |
+
if (country := open("country.txt").read().strip()) == "Chinese":
|
106 |
+
url = "https://api.aiforthai.in.th/xiaofan-zh-th"
|
107 |
+
|
108 |
+
payload = json.dumps({
|
109 |
+
"input": f"{text_var}",
|
110 |
+
"src": "zh",
|
111 |
+
"trg": "th"
|
112 |
+
})
|
113 |
+
headers = {
|
114 |
+
'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
|
115 |
+
'Content-Type': 'application/json'
|
116 |
+
}
|
117 |
+
|
118 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
119 |
+
back = eval(response.text)["output"]
|
120 |
+
text_to_speech(f'{back}', filename="greeting_audio.mp3", speaker=6, language="th")
|
121 |
+
st.audio("greeting_audio.mp3", format="audio/mp3", loop=False, autoplay=True)
|
122 |
+
|
123 |
+
|
124 |
+
elif (country := open("country.txt").read().strip()) == "English":
|
125 |
+
url = "https://api.aiforthai.in.th/xiaofan-en-th/en2th"
|
126 |
+
payload = json.dumps({
|
127 |
+
"input": f"{text_var}"
|
128 |
+
})
|
129 |
+
headers = {
|
130 |
+
'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
|
131 |
+
'Content-Type': 'application/json'
|
132 |
+
}
|
133 |
+
|
134 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
135 |
+
|
136 |
+
st.write("English now is broken ⛓️💥")
|
137 |
|
138 |
with col2:
|
139 |
# Code in the left column
|