Kevin676 commited on
Commit
8b2a323
ยท
1 Parent(s): 4766f29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -37
app.py CHANGED
@@ -2,10 +2,9 @@ import os
2
  os.system("pip install git+https://github.com/openai/whisper.git")
3
  import gradio as gr
4
  import whisper
5
- import time
6
  import openai
7
- from gtts import gTTS
8
- from IPython.display import Audio
9
 
10
  model = whisper.load_model("base")
11
 
@@ -23,9 +22,7 @@ mes3 = [
23
 
24
  res = []
25
 
26
- question = []
27
-
28
- def transcribe(apikey, audio, choice1, choice2):
29
 
30
  #global messages
31
 
@@ -52,7 +49,7 @@ def transcribe(apikey, audio, choice1, choice2):
52
  messages = mes1
53
  elif choice1 == "Therapist":
54
  messages = mes2
55
- else:
56
  messages = mes3
57
 
58
  # chatgpt
@@ -69,33 +66,186 @@ def transcribe(apikey, audio, choice1, choice2):
69
 
70
  messages.append({"role": "assistant", "content": chat_response})
71
 
72
- if choice2 == "Chinese":
73
- tts = gTTS(chat_response, lang='zh-CN')
74
- elif choice2 == "English":
75
- tts = gTTS(chat_response, lang='en', tld='us')
76
-
77
- tts.save('1.wav')
78
- sound_file = '1.wav'
79
-
80
- question.append(chat_response)
81
-
82
- return [result.text, chat_response, sound_file, res, question]
83
-
84
- output_1 = gr.Textbox(label="Speech to Text")
85
- output_2 = gr.Textbox(label="ChatGPT Output")
86
- output_3 = gr.Audio(label="Audio")
87
-
88
- gr.Interface(
89
- title = 'TalktoAI๏ผŒ้šๆ—ถ้šๅœฐ๏ผŒ่ฐˆๅคฉ่ฏดๅœฐ๏ผ',
90
- description = "่ฎฉๆœ‰ไบบๆ–‡ๅ…ณๆ€€็š„AI้€ ็ฆไธ–็•Œ๏ผAIๅ‘ๅ–„๏ผŒๆ–‡ๆ˜Ž็’€็’จ๏ผ",
91
- fn=transcribe,
92
- inputs=[
93
- gr.Textbox(lines=1, label = "่ฏทๅกซๅ†™ๆ‚จ็š„OpenAI_API_key"),
94
- gr.inputs.Audio(source="microphone", type="filepath"),
95
- gr.Radio(["TOEFL", "Therapist", "Alice"], label="TOEFL Examiner, Therapist Tina, or Assistant Alice?"),
96
- gr.Radio(["Chinese", "English"], label="Chinese or English?")
97
- ],
98
- outputs=[
99
- output_1, output_2, output_3
100
- ],
101
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  os.system("pip install git+https://github.com/openai/whisper.git")
3
  import gradio as gr
4
  import whisper
 
5
  import openai
6
+
7
+ from share_btn import community_icon_html, loading_icon_html, share_js
8
 
9
  model = whisper.load_model("base")
10
 
 
22
 
23
  res = []
24
 
25
+ def inference(apikey, audio, choice1):
 
 
26
 
27
  #global messages
28
 
 
49
  messages = mes1
50
  elif choice1 == "Therapist":
51
  messages = mes2
52
+ elif choice1 == "Alice":
53
  messages = mes3
54
 
55
  # chatgpt
 
66
 
67
  messages.append({"role": "assistant", "content": chat_response})
68
 
69
+ return [result.text, chat_response, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)]
70
+
71
+
72
+ css = """
73
+ .gradio-container {
74
+ font-family: 'IBM Plex Sans', sans-serif;
75
+ }
76
+ .gr-button {
77
+ color: white;
78
+ border-color: black;
79
+ background: black;
80
+ }
81
+ input[type='range'] {
82
+ accent-color: black;
83
+ }
84
+ .dark input[type='range'] {
85
+ accent-color: #dfdfdf;
86
+ }
87
+ .container {
88
+ max-width: 730px;
89
+ margin: auto;
90
+ padding-top: 1.5rem;
91
+ }
92
+
93
+ .details:hover {
94
+ text-decoration: underline;
95
+ }
96
+ .gr-button {
97
+ white-space: nowrap;
98
+ }
99
+ .gr-button:focus {
100
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
101
+ outline: none;
102
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
103
+ --tw-border-opacity: 1;
104
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
105
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
106
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
107
+ --tw-ring-opacity: .5;
108
+ }
109
+ .footer {
110
+ margin-bottom: 45px;
111
+ margin-top: 35px;
112
+ text-align: center;
113
+ border-bottom: 1px solid #e5e5e5;
114
+ }
115
+ .footer>p {
116
+ font-size: .8rem;
117
+ display: inline-block;
118
+ padding: 0 10px;
119
+ transform: translateY(10px);
120
+ background: white;
121
+ }
122
+ .dark .footer {
123
+ border-color: #303030;
124
+ }
125
+ .dark .footer>p {
126
+ background: #0b0f19;
127
+ }
128
+ .prompt h4{
129
+ margin: 1.25em 0 .25em 0;
130
+ font-weight: bold;
131
+ font-size: 115%;
132
+ }
133
+ .animate-spin {
134
+ animation: spin 1s linear infinite;
135
+ }
136
+ @keyframes spin {
137
+ from {
138
+ transform: rotate(0deg);
139
+ }
140
+ to {
141
+ transform: rotate(360deg);
142
+ }
143
+ }
144
+ #share-btn-container {
145
+ display: flex; margin-top: 1.5rem !important; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
146
+ }
147
+ #share-btn {
148
+ all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
149
+ }
150
+ #share-btn * {
151
+ all: unset;
152
+ }
153
+ """
154
+
155
+ block = gr.Blocks(css=css)
156
+
157
+
158
+
159
+ with block:
160
+ gr.HTML(
161
+ """
162
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
163
+ <div
164
+ style="
165
+ display: inline-flex;
166
+ align-items: center;
167
+ gap: 0.8rem;
168
+ font-size: 1.75rem;
169
+ "
170
+ >
171
+ <svg
172
+ width="0.65em"
173
+ height="0.65em"
174
+ viewBox="0 0 115 115"
175
+ fill="none"
176
+ xmlns="http://www.w3.org/2000/svg"
177
+ >
178
+ <rect width="23" height="23" fill="white"></rect>
179
+ <rect y="69" width="23" height="23" fill="white"></rect>
180
+ <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
181
+ <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
182
+ <rect x="46" width="23" height="23" fill="white"></rect>
183
+ <rect x="46" y="69" width="23" height="23" fill="white"></rect>
184
+ <rect x="69" width="23" height="23" fill="black"></rect>
185
+ <rect x="69" y="69" width="23" height="23" fill="black"></rect>
186
+ <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
187
+ <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
188
+ <rect x="115" y="46" width="23" height="23" fill="white"></rect>
189
+ <rect x="115" y="115" width="23" height="23" fill="white"></rect>
190
+ <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
191
+ <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
192
+ <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
193
+ <rect x="92" y="69" width="23" height="23" fill="white"></rect>
194
+ <rect x="69" y="46" width="23" height="23" fill="white"></rect>
195
+ <rect x="69" y="115" width="23" height="23" fill="white"></rect>
196
+ <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
197
+ <rect x="46" y="46" width="23" height="23" fill="black"></rect>
198
+ <rect x="46" y="115" width="23" height="23" fill="black"></rect>
199
+ <rect x="46" y="69" width="23" height="23" fill="black"></rect>
200
+ <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
201
+ <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
202
+ <rect x="23" y="69" width="23" height="23" fill="black"></rect>
203
+ </svg>
204
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
205
+ TalktoAI๏ผŒ้šๆ—ถ้šๅœฐ๏ผŒ่ฐˆๅคฉ่ฏดๅœฐ๏ผ
206
+ </h1>
207
+ </div>
208
+ <p style="margin-bottom: 10px; font-size: 94%">
209
+ ่ฎฉๆœ‰ไบบๆ–‡ๅ…ณๆ€€็š„AI้€ ็ฆๆฏไธ€ไธชไบบ๏ผAIๅ‘ๅ–„๏ผŒๆ–‡ๆ˜Ž็’€็’จ๏ผ
210
+ </p>
211
+ </div>
212
+ """
213
+ )
214
+
215
+ with gr.Group():
216
+ with gr.Box():
217
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
218
+ apikey = gr.Textbox(lines=3, label = "่ฏทๅกซๅ†™ๆ‚จ็š„OpenAI_API_key")
219
+ audio = gr.Audio(
220
+ label="Input Audio",
221
+ show_label=False,
222
+ source="microphone",
223
+ type="filepath"
224
+ )
225
+ choice1 = gr.Radio(["TOEFL", "Therapist", "Alice"], label="TOEFL Examiner, Therapist Tina, or Assistant Alice?")
226
+
227
+ btn = gr.Button("ๆไบค")
228
+
229
+ text = gr.Textbox(label="Speech to Text", elem_id="result-textarea")
230
+
231
+ response = gr.Textbox(label="ChatGPT Output")
232
+
233
+ with gr.Group(elem_id="share-btn-container"):
234
+ community_icon = gr.HTML(community_icon_html, visible=False)
235
+ loading_icon = gr.HTML(loading_icon_html, visible=False)
236
+ share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
237
+
238
+
239
+
240
+
241
+ btn.click(inference, inputs=[apikey, audio, choice1], outputs=[text, response, community_icon, loading_icon, share_button])
242
+ share_button.click(None, [], [], _js=share_js)
243
+
244
+ gr.HTML('''
245
+ <div class="footer">
246
+ <p>Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a> - Gradio Demo by ๐Ÿค— Hugging Face
247
+ </p>
248
+ </div>
249
+ ''')
250
+
251
+ block.launch()