File size: 11,230 Bytes
c136704
 
 
 
 
 
 
214f631
675ffc8
214f631
c136704
214f631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c136704
214f631
c136704
214f631
675ffc8
214f631
c136704
 
214f631
ab7dda8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
675ffc8
ab7dda8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214f631
 
 
 
 
 
c136704
214f631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c136704
ab7dda8
c136704
ab7dda8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b78e79
ab7dda8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c136704
214f631
 
 
c136704
214f631
 
c136704
214f631
 
 
 
 
c136704
214f631
 
 
 
 
 
 
 
 
 
 
c136704
214f631
c136704
 
 
214f631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
675ffc8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import streamlit as st
from pydub import AudioSegment
from aift.multimodal import textqa
from aift import setting
import configparser
import requests
import json
import subprocess

st.set_page_config(layout="wide")

st.markdown(
    """
    <style>
    .stApp {
        background-color: #f0f0f0;
    }
    div[data-testid="stVerticalBlock"] > div:nth-of-type(1) {
        background-color: #87CEEB;
        padding: 10px;
        border-radius: 5px;
    }
    div[data-testid="stVerticalBlock"] > div:nth-of-type(2) {
        background-color: #FFC0CB;
        padding: 10px;
        border-radius: 5px;
    }
    </style>
    """,
    unsafe_allow_html=True
)

setting.set_api_key('T69FqnYgOdreO5G0nZaM8gHcjo1sifyU')

# Create two columns

col1, col2= st.columns(2)


with col1:
    def text_to_speech(text, filename="audio_file.mp3", speaker=1, language="th", volume=1, speed=1):
            url = "https://api-voice.botnoi.ai/openapi/v1/generate_audio"
            payload = {
                "text": text,
                "speaker": speaker,
                "volume": volume,
                "speed": speed,
                "type_media": "mp3",
                "save_file": "true",
                "language": language
            }
            headers = {
                'Botnoi-Token': 'ZHBaNlR4WEI3dWgyZGVRajRMaGt5S3NXeUVZMjU2MTg5NA==',
                'Content-Type': 'application/json'
            }

            try:
                response = requests.post(url, headers=headers, json=payload)
                response.raise_for_status()
                data = response.json()
                audio_url = data.get("audio_url")
                if audio_url:
                    audio_response = requests.get(audio_url)
                    audio_response.raise_for_status()
                    with open(filename, "wb") as file:
                        file.write(audio_response.content)
                    print(f"Audio downloaded successfully as '{filename}'.")
                else:
                    print("Audio URL not found in the response.")
            except requests.exceptions.RequestException as e:
                print(f"An error occurred: {e}")
    # Code in the left column
    st.markdown(
    "<h1 style='text-align: center; font-family: Times New Roman;'>Choose Your Country</h1>",
    unsafe_allow_html=True
    )
    col4, col5, col6 = st.columns(3)

    # Display flags in the respective columns
    list = ["thailand","english","chinese"]
    with col4:
        st.image("Thai.jpg", use_container_width=True)
        if st.button("ประเทศไทย (ภาษากลาง)",use_container_width=True):
            config = configparser.ConfigParser()
            config.read("config.ini")
            config["DEFAULT"]["_server_port"] = "27016"
            config["SERVER"]["_server_port"] = "27016"
            country = "Thailand"
            open("country.txt", "w").write("Thailand")
            with open("config.ini", "w") as configfile:
                config.write(configfile)
    with col5:
        st.image("UK.png", use_container_width=True)
        if st.button("English",use_container_width=True):
            open("country.txt", "w").write("English")
    with col6:
        st.image("China.png", use_container_width=True)
        if st.button("Chinese",use_container_width=True):
            country = "Chinese"
            open("country.txt", "w").write("Chinese")

    text_var = st.text_input("Enter text value:", value="")

    if text_var is not None:
        if (country := open("country.txt").read().strip()) == "Chinese":
            url = "https://api.aiforthai.in.th/xiaofan-zh-th"
            
            payload = json.dumps({
            "input": f"{text_var}",
            "src": "zh",
            "trg": "th"
            })
            headers = {
            'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
            'Content-Type': 'application/json'
            }
            
            response = requests.request("POST", url, headers=headers, data=payload)
            back = eval(response.text)["output"]
            text_to_speech(f'{back}', filename="greeting_audio.mp3", speaker=6, language="th")
            st.audio("greeting_audio.mp3", format="audio/mp3", loop=False, autoplay=False)


        elif (country := open("country.txt").read().strip()) == "English":
            url = "https://api.aiforthai.in.th/xiaofan-en-th/en2th"
            payload = json.dumps({
            "input": f"{text_var}"
            })
            headers = {
            'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
            'Content-Type': 'application/json'
            }
            
            response = requests.request("POST", url, headers=headers, data=payload)
            
            st.write("English now is broken ⛓️‍💥")

with col2:
    # Code in the left column
    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)

    land = ["< โปรดเลือกภาษาถิ่นของคุณ >", "อีสาน", "เหนือ", "ใต้"]
    selected_region = st.selectbox("Select Region:", land, index=0)

    region_ports = {
        "อีสาน": 27020,
        "เหนือ": 27021,
        "ใต้": 27022
    }

    def text_to_speech(text, filename="audio_file.mp3", speaker=1, language="th", volume=1, speed=1):
        url = "https://api-voice.botnoi.ai/openapi/v1/generate_audio"
        payload = {
            "text": text,
            "speaker": speaker,
            "volume": volume,
            "speed": speed,
            "type_media": "mp3",
            "save_file": "true",
            "language": language
        }
        headers = {
            'Botnoi-Token': 'ZHBaNlR4WEI3dWgyZGVRajRMaGt5S3NXeUVZMjU2MTg5NA==',
            'Content-Type': 'application/json'
        }

        try:
            response = requests.post(url, headers=headers, json=payload)
            response.raise_for_status()
            data = response.json()
            audio_url = data.get("audio_url")
            if audio_url:
                audio_response = requests.get(audio_url)
                audio_response.raise_for_status()
                with open(filename, "wb") as file:
                    file.write(audio_response.content)
                print(f"Audio downloaded successfully as '{filename}'.")
            else:
                print("Audio URL not found in the response.")
        except requests.exceptions.RequestException as e:
            print(f"An error occurred: {e}")

    config = configparser.ConfigParser()
    config.read("config.ini")

    new_port = str(region_ports.get(selected_region, config["DEFAULT"].get("_server_port", "27021")))
    config["DEFAULT"]["_server_port"] = new_port
    config["SERVER"]["_server_port"] = new_port

    with open("config.ini", "w") as configfile:
        config.write(configfile)

    audio_value = st.audio_input("Record a voice message",key=2)

    if audio_value is not None:
        st.audio(audio_value)
        audio_bytes = audio_value.getvalue()
        file_path = "./recorded_audio.wav"
        with open(file_path, "wb") as file:
            file.write(audio_bytes)

        audio = AudioSegment.from_wav(file_path)
        audio = audio.set_frame_rate(16000).set_channels(1)
        output_file = "recorded_audio.wav"
        audio.export(output_file, format="wav")

        st.success(f"Audio saved and modified as {output_file}")
        command = ["python", "partii-client-process-wav-file.py", output_file, "T69FqnYgOdreO5G0nZaM8gHcjo1sifyU"]
        result = subprocess.run(command, capture_output=True, text=True)

        output_list = [line for line in result.stdout.strip().split('\n') if line.startswith("transcript")]

        if (country := open("country.txt").read().strip()) == "English":
            if output_list:
                speech_t = output_list[-1].replace("transcript", "").strip()
                st.write(f"Transcript: {speech_t}")

                answer = textqa.generate(f'{selected_region} คำว่า "{speech_t}" แปลว่าอะไรภาษาไทย (ตอบแค่คำแปล)', return_json=False)
                
                url = "https://api.aiforthai.in.th/xiaofan-en-th/th2en"
                payload = json.dumps({"text": answer})
                headers = {
                    'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
                    'Content-Type': 'application/json'
                }
                response = requests.post(url, headers=headers, data=payload)
                Outt = eval(response.text)["translated_text"]

                text_to_speech(f'{Outt}', filename="greeting_audio.mp3", speaker=100, language="en")
                st.audio("greeting_audio.mp3", format="audio/mp3", loop=False, autoplay=True)
            else:
                st.error("Failed to extract transcript.")

        elif (country := open("country.txt").read().strip()) == "Chinese":
            if output_list:
                speech_t = output_list[-1].replace("transcript", "").strip()
                st.write(f"Transcript: {speech_t}")
                
                answer = textqa.generate(f'{selected_region} คำว่า "{speech_t}" แปลว่าอะไรภาษาไทย (ตอบแค่คำแปล)', return_json=False)

                url = "https://api.aiforthai.in.th/xiaofan-zh-th"
                payload = json.dumps({
                "input": "ฉันจะซื้อคอมพิวเตอร์เครื่องใหม่ให้คุณ",
                "src": "th",
                "trg": "zh"
                })
                headers = {
                'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
                'Content-Type': 'application/json'
                }
                
                response = requests.request("POST", url, headers=headers, data=payload)
                Outt = eval(response.text)["output"]

                text_to_speech(f'{Outt}', filename="greeting_audio.mp3", speaker=60, language="zh")
                st.audio("greeting_audio.mp3", format="audio/mp3", loop=False, autoplay=True)
            else:
                st.error("Failed to extract transcript.")

        elif (country := open("country.txt").read().strip()) == "Thailand":
            if output_list:
                speech_t = output_list[-1].replace("transcript", "").strip()
                st.write(f"Transcript: {speech_t}")

                answer = textqa.generate(f'{selected_region} คำว่า "{speech_t}" แปลว่าอะไรภาษาไทย (ตอบแค่คำแปล)', return_json=False)

                text_to_speech(f'{answer}', filename="greeting_audio.mp3", speaker=6, language="th")
                st.audio("greeting_audio.mp3", format="audio/mp3", loop=False, autoplay=True)
            else:
                st.error("Failed to extract transcript.")

    else:
        st.warning("Please select your dialect before recording.")