Santipab commited on
Commit
214f631
·
verified ·
1 Parent(s): b4410bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +208 -53
app.py CHANGED
@@ -5,74 +5,229 @@ from aift import setting
5
  import configparser
6
  import requests
7
  import json
8
- import subprocess
 
 
 
9
 
10
- setting.set_api_key('T69FqnYgOdreO5G0nZaM8gHcjo1sifyU')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- st.write("# เลือกภาษาถิ่น")
13
- land = ["< โปรดเลือกภาษาถิ่นของคุณ >", "อีสาน", "เหนือ", "ใต้"]
14
- selected_region = st.selectbox("Select Region:", land, index=0)
15
 
16
- region_ports = {
17
- "อีสาน": 27020,
18
- "เหนือ": 27021,
19
- "ใต้": 27022
20
- }
21
 
22
- config = configparser.ConfigParser()
23
- config.read("config.ini")
24
 
25
- new_port = str(region_ports.get(selected_region, config["DEFAULT"].get("_server_port", "27021")))
26
- config["DEFAULT"]["_server_port"] = new_port
27
- config["SERVER"]["_server_port"] = new_port
 
 
 
 
28
 
29
- with open("config.ini", "w") as configfile:
30
- config.write(configfile)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- audio_value = st.audio_input("Record a voice message")
 
 
 
 
 
 
33
 
34
- if audio_value is not None:
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- st.audio(audio_value)
37
- audio_bytes = audio_value.getvalue()
38
- file_path = "./recorded_audio.wav"
39
- with open(file_path, "wb") as file:
40
- file.write(audio_bytes)
41
- temp_file = "./recorded_audio.wav"
42
- with open(temp_file, "wb") as file:
43
- file.write(audio_bytes)
44
 
45
- audio = AudioSegment.from_wav(temp_file)
46
- audio = audio.set_frame_rate(16000).set_channels(1)
47
- output_file = "recorded_audio.wav"
48
- audio.export(output_file, format="wav")
49
 
50
- st.success(f"Audio saved and modified as {output_file}")
51
- # Run external process to get transcript
52
- command = ["python", "partii-client-process-wav-file.py", output_file, "T69FqnYgOdreO5G0nZaM8gHcjo1sifyU"]
53
- result = subprocess.run(command, capture_output=True, text=True)
54
 
55
- output_list = [line for line in result.stdout.strip().split('\n') if line.startswith("transcript")]
56
- if output_list:
57
- speech_t = output_list[-1].replace("transcript", "").strip()
58
- st.write(f"Transcript: {speech_t}")
59
 
60
- # Generate answer using textqa
61
- answer = textqa.generate(f'{selected_region} คำว่า "{speech_t}" แปลว่าอะไรภาษาไทย (ตอบแค่คำแปล)', return_json=False)
 
 
 
62
 
63
- # Translate answer to English
64
- url = "https://api.aiforthai.in.th/xiaofan-en-th/th2en"
65
- payload = json.dumps({"text": answer})
 
 
 
 
 
 
 
 
66
  headers = {
67
- 'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
68
  'Content-Type': 'application/json'
69
  }
70
- response = requests.post(url, headers=headers, data=payload)
71
- Outt = eval(response.text)["translated_text"]
72
- st.write(f'Translated Answer: {Outt}')
73
- else:
74
- st.error("Failed to extract transcript.")
75
 
76
- else:
77
- st.warning("Please select your dialect before recording.")
78
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import configparser
6
  import requests
7
  import json
8
+ import subprocess
9
+ import whisper
10
+ # Set up API key for aift
11
+ st.set_page_config(layout="wide")
12
 
13
+ st.markdown(
14
+ """
15
+ <style>
16
+ .stApp {
17
+ background-color: #f0f0f0;
18
+ }
19
+ div[data-testid="stVerticalBlock"] > div:nth-of-type(1) {
20
+ background-color: #87CEEB;
21
+ padding: 10px;
22
+ border-radius: 5px;
23
+ }
24
+ div[data-testid="stVerticalBlock"] > div:nth-of-type(2) {
25
+ background-color: #FFC0CB;
26
+ padding: 10px;
27
+ border-radius: 5px;
28
+ }
29
+ </style>
30
+ """,
31
+ unsafe_allow_html=True
32
+ )
33
 
34
+ setting.set_api_key('T69FqnYgOdreO5G0nZaM8gHcjo1sifyU')
 
 
35
 
36
+ # Create two columns
37
+ col1, col2= st.columns(2)
 
 
 
38
 
 
 
39
 
40
+ with col1:
41
+ # Code in the left column
42
+ st.markdown(
43
+ "<h1 style='text-align: center; font-family: Times New Roman;'>Choose Your Country</h1>",
44
+ unsafe_allow_html=True
45
+ )
46
+ col4, col5, col6 = st.columns(3)
47
 
48
+ # Display flags in the respective columns
49
+ list = ["thailand","english","chinese"]
50
+ with col4:
51
+ st.image("Thai.jpg", use_container_width=True)
52
+ if st.button("ประเทศไทย (ภาษากลาง)",use_container_width=True):
53
+ config = configparser.ConfigParser()
54
+ config.read("config.ini")
55
+ config["DEFAULT"]["_server_port"] = "27016"
56
+ config["SERVER"]["_server_port"] = "27016"
57
+ country = "Thailand"
58
+ open("country.txt", "w").write("Thailand")
59
+ with open("config.ini", "w") as configfile:
60
+ config.write(configfile)
61
+ with col5:
62
+ st.image("UK.png", use_container_width=True)
63
+ if st.button("English",use_container_width=True):
64
+ open("country.txt", "w").write("English")
65
+ with col6:
66
+ st.image("China.png", use_container_width=True)
67
+ if st.button("Chinese",use_container_width=True):
68
+ country = "Chinese"
69
+ open("country.txt", "w").write("Chinese")
70
 
71
+ audio_value = st.audio_input("Record a voice of you",key=1)
72
+ if audio_value is not None:
73
+ st.audio(audio_value)
74
+ audio_bytes = audio_value.getvalue()
75
+ file_path = "./recorded_audio_en.wav"
76
+ with open(file_path, "wb") as file:
77
+ file.write(audio_bytes)
78
 
79
+ audio = AudioSegment.from_wav(file_path)
80
+ audio = audio.set_frame_rate(16000).set_channels(1)
81
+ output_file = "./recorded_audio_en.wav"
82
+ audio.export(output_file, format="wav")
83
+ with open(file_path, "wb") as file:
84
+ file.write(audio_bytes)
85
+ st.success(f"Audio saved and modified as {output_file}")
86
+
87
+ if (country := open("country.txt").read().strip()) == "English":
88
+ model = whisper.load_model("tiny")
89
+ audio = whisper.load_audio("recorded_audio_en.wav")
90
+ result = model.transcribe(audio)
91
+ st.write(result["text"])
92
 
93
+ # elif (country := open("country.txt").read().strip()) == "Chinese":
94
+ # elif (country := open("country.txt").read().strip()) == "Thailand":
 
 
 
 
 
 
95
 
96
+
 
 
 
97
 
98
+ with col2:
99
+ # Code in the left column
100
+ st.markdown("<link href='https://fonts.googleapis.com/css2?family=Sarabun&display=swap' rel='stylesheet'><h1 style='text-align: center; font-family: Sarabun, sans-serif;'>เลือกภาษาถิ่น</h1>", unsafe_allow_html=True)
 
101
 
102
+ land = ["< โปรดเลือกภาษาถิ่นของคุณ >", "อีสาน", "เหนือ", "ใต้"]
103
+ selected_region = st.selectbox("Select Region:", land, index=0)
 
 
104
 
105
+ region_ports = {
106
+ "อีสาน": 27020,
107
+ "เหนือ": 27021,
108
+ "ใต้": 27022
109
+ }
110
 
111
+ def text_to_speech(text, filename="audio_file.mp3", speaker=1, language="th", volume=1, speed=1):
112
+ url = "https://api-voice.botnoi.ai/openapi/v1/generate_audio"
113
+ payload = {
114
+ "text": text,
115
+ "speaker": speaker,
116
+ "volume": volume,
117
+ "speed": speed,
118
+ "type_media": "mp3",
119
+ "save_file": "true",
120
+ "language": language
121
+ }
122
  headers = {
123
+ 'Botnoi-Token': 'ZHBaNlR4WEI3dWgyZGVRajRMaGt5S3NXeUVZMjU2MTg5NA==',
124
  'Content-Type': 'application/json'
125
  }
 
 
 
 
 
126
 
127
+ try:
128
+ response = requests.post(url, headers=headers, json=payload)
129
+ response.raise_for_status()
130
+ data = response.json()
131
+ audio_url = data.get("audio_url")
132
+ if audio_url:
133
+ audio_response = requests.get(audio_url)
134
+ audio_response.raise_for_status()
135
+ with open(filename, "wb") as file:
136
+ file.write(audio_response.content)
137
+ print(f"Audio downloaded successfully as '{filename}'.")
138
+ else:
139
+ print("Audio URL not found in the response.")
140
+ except requests.exceptions.RequestException as e:
141
+ print(f"An error occurred: {e}")
142
+
143
+ config = configparser.ConfigParser()
144
+ config.read("config.ini")
145
+
146
+ new_port = str(region_ports.get(selected_region, config["DEFAULT"].get("_server_port", "27021")))
147
+ config["DEFAULT"]["_server_port"] = new_port
148
+ config["SERVER"]["_server_port"] = new_port
149
+
150
+ with open("config.ini", "w") as configfile:
151
+ config.write(configfile)
152
+
153
+ audio_value = st.audio_input("Record a voice message",key=2)
154
+
155
+ if audio_value is not None:
156
+ st.audio(audio_value)
157
+ audio_bytes = audio_value.getvalue()
158
+ file_path = "./recorded_audio.wav"
159
+ with open(file_path, "wb") as file:
160
+ file.write(audio_bytes)
161
+
162
+ audio = AudioSegment.from_wav(file_path)
163
+ audio = audio.set_frame_rate(16000).set_channels(1)
164
+ output_file = "recorded_audio.wav"
165
+ audio.export(output_file, format="wav")
166
+
167
+ st.success(f"Audio saved and modified as {output_file}")
168
+ command = ["python", "partii-client-process-wav-file.py", output_file, "T69FqnYgOdreO5G0nZaM8gHcjo1sifyU"]
169
+ result = subprocess.run(command, capture_output=True, text=True)
170
+
171
+ output_list = [line for line in result.stdout.strip().split('\n') if line.startswith("transcript")]
172
+
173
+ if (country := open("country.txt").read().strip()) == "English":
174
+ if output_list:
175
+ speech_t = output_list[-1].replace("transcript", "").strip()
176
+ st.write(f"Transcript: {speech_t}")
177
+
178
+ answer = textqa.generate(f'{selected_region} คำว่า "{speech_t}" แปลว่าอะไรภาษาไทย (ตอบแค่คำแปล)', return_json=False)
179
+
180
+ url = "https://api.aiforthai.in.th/xiaofan-en-th/th2en"
181
+ payload = json.dumps({"text": answer})
182
+ headers = {
183
+ 'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
184
+ 'Content-Type': 'application/json'
185
+ }
186
+ response = requests.post(url, headers=headers, data=payload)
187
+ Outt = eval(response.text)["translated_text"]
188
+
189
+ text_to_speech(f'{Outt}', filename="greeting_audio.mp3", speaker=100, language="en")
190
+ st.audio("greeting_audio.mp3", format="audio/mp3", loop=False, autoplay=True)
191
+ else:
192
+ st.error("Failed to extract transcript.")
193
+
194
+ elif (country := open("country.txt").read().strip()) == "Chinese":
195
+ if output_list:
196
+ speech_t = output_list[-1].replace("transcript", "").strip()
197
+ st.write(f"Transcript: {speech_t}")
198
+
199
+ answer = textqa.generate(f'{selected_region} คำว่า "{speech_t}" แปลว่าอะไรภาษาไทย (ตอบแค่คำแปล)', return_json=False)
200
+
201
+ url = "https://api.aiforthai.in.th/xiaofan-zh-th"
202
+ payload = json.dumps({
203
+ "input": "ฉันจะซื้อคอมพิวเตอร์เครื่องใหม่ให้คุณ",
204
+ "src": "th",
205
+ "trg": "zh"
206
+ })
207
+ headers = {
208
+ 'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
209
+ 'Content-Type': 'application/json'
210
+ }
211
+
212
+ response = requests.request("POST", url, headers=headers, data=payload)
213
+ Outt = eval(response.text)["output"]
214
+
215
+ text_to_speech(f'{Outt}', filename="greeting_audio.mp3", speaker=60, language="zh")
216
+ st.audio("greeting_audio.mp3", format="audio/mp3", loop=False, autoplay=True)
217
+ else:
218
+ st.error("Failed to extract transcript.")
219
+
220
+ elif (country := open("country.txt").read().strip()) == "Thailand":
221
+ if output_list:
222
+ speech_t = output_list[-1].replace("transcript", "").strip()
223
+ st.write(f"Transcript: {speech_t}")
224
+
225
+ answer = textqa.generate(f'{selected_region} คำว่า "{speech_t}" แปลว่าอะไรภาษาไทย (ตอบแค่คำแปล)', return_json=False)
226
+
227
+ text_to_speech(f'{answer}', filename="greeting_audio.mp3", speaker=6, language="th")
228
+ st.audio("greeting_audio.mp3", format="audio/mp3", loop=False, autoplay=True)
229
+ else:
230
+ st.error("Failed to extract transcript.")
231
+
232
+ else:
233
+ st.warning("Please select your dialect before recording.")