File size: 10,736 Bytes
c47c965
38357a6
c47c965
 
 
38357a6
c47c965
 
 
 
38357a6
c47c965
 
38357a6
 
c47c965
 
8bb922b
38357a6
8bb922b
c47c965
38357a6
 
 
 
 
986a5af
38357a6
 
23a1b44
 
c47c965
38357a6
 
 
 
 
 
c47c965
38357a6
 
 
c47c965
38357a6
c47c965
 
 
986a5af
 
38357a6
23a1b44
38357a6
 
2bd08fd
 
 
e8bab12
2bd08fd
 
 
 
 
 
9d95ffc
986a5af
9d95ffc
 
d01733c
 
 
c2f03c9
c47c965
986a5af
b9a1991
986a5af
c47c965
23a1b44
 
 
 
 
 
 
c47c965
 
 
c2f03c9
c47c965
c2f03c9
c47c965
2bd08fd
c47c965
 
986a5af
c47c965
 
2bd08fd
c47c965
 
 
 
2bd08fd
c47c965
 
 
 
 
 
 
2bd08fd
c47c965
2bd08fd
c47c965
 
 
 
 
 
 
 
 
 
2bd08fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c47c965
986a5af
2bd08fd
 
 
 
c47c965
 
 
 
 
 
 
 
 
 
 
 
 
 
2bd08fd
c47c965
2bd08fd
c47c965
c2f03c9
e8bab12
 
 
 
 
 
 
c2f03c9
c47c965
986a5af
c47c965
 
 
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
import os
import tempfile

# LLM Models
from langchain_openai import ChatOpenAI
from openai import OpenAI

# Gradio
import gradio as gr

# Environment variables
from dotenv import load_dotenv

os.environ['USER_AGENT'] = 'JimYin88'
load_dotenv()

chat_model = ChatOpenAI(model="gpt-4o-mini-2024-07-18",
                        max_completion_tokens=1024,
                        api_key=os.getenv("OPENAI_API_KEY"),
                        temperature=0.3)

client = OpenAI()


def english_chinese_translation(text_input: str = 'Hi, how are you?',
                                voice: str = 'coral',
                                tone: str = 'positive',
                                speed: float = 1.0):

    response = chat_model.invoke(f"Translate the following words in English to traditional Chinese. "
                                 f"Give only the Chinese translation: {text_input}")

    audio = client.audio.speech.create(input=response.content,
                                       model="gpt-4o-mini-tts",
                                       voice=voice,
                                       instructions=f"Speak in a {tone} tone.",
                                       response_format="mp3",
                                       speed=speed)

    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio_file:
        temp_audio_file.write(audio.content)
        temp_file_path = temp_audio_file.name

    return response.content, temp_file_path


interface = gr.Interface(fn=english_chinese_translation,
                         inputs=[gr.Text(value='Hi, how are you?',
                                         label="English", elem_classes="text_input", elem_id="label"),
                                 gr.Dropdown(choices=['alloy', 'ash', 'ballad', 'coral', 'echo',
                                                      'fable', 'nova', 'onyx', 'sage', 'shimmer', 'verse'],
                                             value='coral',
                                             multiselect=False,
                                             elem_classes="text_input",
                                             elem_id="textarea",
                                             label='VOICE'),
                                 gr.Text(value='positive', label='Tone', elem_classes="text-container"),
                                 gr.Slider(minimum=0.70,
                                           maximum=1.30,
                                           value=1.0,
                                           step=0.01,
                                           label="Speed",
                                           elem_classes="text_input")],
                         outputs=[gr.Text(label="中文",
                                          # interactive=False,
                                          elem_classes="text_input",
                                          elem_id="label"),
                                  gr.Audio(label="Generated Audio",
                                           type="filepath",
                                           autoplay=True,
                                           show_share_button=False)],
                         flagging_mode="never",
                         # examples=[['Hi, how are you?', 'ash', 'cheerful and positive', 1.0]],
                         #           ['Do you speak Chinese?', 'coral', 'commanding', 1.0]],
                         # live=False,
                         title='English-Chinese Translator',
                         description="""
                                     Translate English words or phrases into traditional Chinese characters,
                                     and then pronounce them in Chinese. Type in the English words or phrases.
                                     Select the voice among 11 choices. Type in the tone like e.g. 'professional',
                                     'friendly', 'cheerful', etc. Select the speed from 0.7 - 1.3. Then click on
                                     the Submit button to hear the pronunciation in Chinese.
                                     """,
                         theme=gr.themes.Base(),
                         css="""
                             .gradio-container {font-family: 'Arial';
                                                # font-size: 54px;
                                                # font-weight: normal;
                                                # text-align: left;
                                                border: 1px solid black;
                                                border-radius: 0px;
                                                background-color: lightblue}

                                textarea {font-size: 24px;
                                       font-family: 'Verdana';
                                       font-weight: normal;
                                       # color: lightblue;
                                       # color: rgb(238, 130, 238); 
                                       background-color: White;
                                       text-align: left;
                                       border: 1px solid black;
                                       border-radius: 0px
                                       }

                                button {font-size: 16px;
                                          font-family: 'Arial';
                                          font-weight: bold;
                                          # text-align: right;
                                          font-style: normal;
                                          # text-transform: uppercase;
                                          border: 1px solid black;
                                       border-radius: 0px;
                                       }

                                label {font-family: 'Arial';
                                       # font-size: 24px;
                                       font-weight: bold;
                                       text-align: center;
                                       font-style: normal;
                                       text-transform: uppercase;
                                       line-height: 1.0;
                                       letter-spacing: 2px}
                                       
                                h1 {
                                       font-size: 28px;
                                       font-family: Verdana, Arial, Sans-Serif;
                                       font-style: normal;
                                       font-weight: bold;
                                       color: black; # rgb(238, 130, 238)
                                       # background-color: yellow;
                                       # text-align: right;
                                       text-transform: normal;
                                       letter-spacing: 2px;
                                       border: 1px solid lightblue;
                                       border-radius: 0px
                                    }
                                    
                                p {
                                       font-size: 14px;
                                       font-family: Times New Roman, Times, Serif;
                                       font-style: normal;
                                       font-weight: normal;
                                       color: black; # rgb(238, 130, 238)
                                       # background-color: yellow;
                                       text-align: left;
                                       text-transform: normal;
                                       letter-spacing: 1px;
                                       border: 0px solid lightblue;
                                       border-radius: 0px
                                    }
                                    
                                div {
                                       font-size: 14px;
                                       font-family: Verdana, Arial, Sans-Serif;
                                       font-style: normal;
                                       font-weight: normal;
                                       color: black; # rgb(238, 130, 238)
                                       # background-color: yellow;
                                       text-align: center;
                                       text-transform: normal;
                                       letter-spacing: 0px;
                                       border: 1px solid lightblue;
                                       border-radius: 0px
                                    }

                            .text_input {font-family: 'Arial';
                                         # font-size: 32px;
                                         # font-weight: normal;
                                         # text-align: left;
                                         background-color: lightblue}

                                textarea {font-size: 20px;
                                          font-family: 'Verdana';
                                          font-weight: normal;
                                          # color: DodgerBlue;
                                          text-align: left}

                                label {font-family: 'Arial';
                                       # font-size: 36px;
                                       # font-weight: normal;
                                       text-align: center;
                                       font-style: normal;
                                       text-transform: uppercase;
                                       line-height: 1.0;
                                       letter-spacing: 0px;
                                       border: 1px solid black;
                                       border-radius: 0px;
                                       }
                                       
                            .text_container {font-family: 'Arial';
                                             font-size: 48px;
                                             font-weight: bold;
                                             text-align: right;
                                             font-style: italic;
                                             text-transform: uppercase;
                                             line-height: 1.0;
                                             }

                            """
                         )

interface.launch(share=False)