tonic commited on
Commit
d886b33
1 Parent(s): cb91f53

refactor gradio interface iface()

Browse files
Files changed (1) hide show
  1. app.py +120 -65
app.py CHANGED
@@ -248,75 +248,130 @@ def process_input(image=None, file=None, audio=None, text="", translateto = "Eng
248
 
249
  return final_text, audio_output, top_phrases, translations, audio_outputs
250
 
251
- def main():
252
- with gr.Blocks() as demo:
253
- gr.Markdown(title)
254
- gr.Markdown(description)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
 
256
- with gr.Row():
257
- input_language = gr.Dropdown(choices=choices, label="Your Native Language")
258
- target_language = gr.Dropdown(choices=choices, label="Language To Learn")
259
 
260
- with gr.Accordion("Talk To 🌟AyaTonic"):
261
- with gr.Tab("🤙🏻Audio & Text"):
262
- audio_input = gr.Audio(sources="microphone", type="filepath", label="Mic Input")
263
- text_input = gr.Textbox(lines=2, label="Text Input")
264
- with gr.Tab("📸Image & File"):
265
- image_input = gr.Image(type="pil", label="Camera Input")
266
- file_input = gr.File(label="File Upload")
267
 
268
- process_button = gr.Button("🌟AyaTonic")
269
 
270
- processed_text_output = RichTextbox(label="Processed Text")
271
- longest_phrases_1 = gr.Textbox(label="Focus")
272
- translated_phrases_output_1 = gr.Textbox(label="Translated Phrases")
273
- audio_output_native_phrase_1 = gr.Audio(label="Audio Output (Native Language)")
274
- audio_output_target_phrase_1 = gr.Audio(label="Audio Output (Target Language)")
275
- longest_phrases_2 = gr.Textbox(label="Focus")
276
- translated_phrases_output_2 = gr.Textbox(label="Translated Phrases")
277
- audio_output_native_phrase_2 = gr.Audio(label="Audio Output (Native Language)")
278
- audio_output_target_phrase_2 = gr.Audio(label="Audio Output (Target Language)")
279
- longest_phrases_3 = gr.Textbox(label="Focus")
280
- translated_phrases_output_3 = gr.Textbox(label="Translated Phrases")
281
- audio_output_native_phrase_3 = gr.Audio(label="Audio Output (Native Language)")
282
- audio_output_target_phrase_3 = gr.Audio(label="Audio Output (Target Language)")
283
 
284
- def update_outputs(image, file, audio, text, input_language, target_language):
285
- final_text, top_phrases, translations, audio_outputs = process_input(
286
- image=image, file=file, audio=audio, text=text,
287
- translatefrom=input_language, translateto=target_language
288
- )
289
-
290
- processed_text_output = final_text
291
- audio_output_native_phrases = [native for _, native in audio_outputs]
292
- audio_output_target_phrases = [target for target, _ in audio_outputs]
293
-
294
- longest_phrases_outputs = top_phrases[:3]
295
- translated_phrases_outputs = translations[:3]
296
- audio_outputs_native = audio_output_native_phrases[:3]
297
- audio_outputs_target = audio_output_target_phrases[:3]
298
-
299
- return (
300
- processed_text_output,
301
- *audio_outputs_native,
302
- *audio_outputs_target,
303
- *longest_phrases_outputs,
304
- *translated_phrases_outputs
305
- )
306
 
307
- process_button.click(
308
- fn=update_outputs,
309
- inputs=[image_input, file_input, audio_input, text_input, input_language, target_language],
310
- outputs=[
311
- processed_text_output,
312
- audio_output_native_phrase_1, audio_output_target_phrase_1,
313
- audio_output_native_phrase_2, audio_output_target_phrase_2,
314
- audio_output_native_phrase_3, audio_output_target_phrase_3,
315
- longest_phrases_1, translated_phrases_output_1,
316
- longest_phrases_2, translated_phrases_output_2,
317
- longest_phrases_3, translated_phrases_output_3
318
- ]
319
- )
320
 
321
- if __name__ == "__main__":
322
- main()
 
248
 
249
  return final_text, audio_output, top_phrases, translations, audio_outputs
250
 
251
+
252
+ # Define the inputs and outputs for the Gradio Interface
253
+ inputs = [
254
+ gr.inputs.Image(type="pil", label="Camera Input"),
255
+ gr.inputs.File(label="File Upload"),
256
+ gr.inputs.Audio(sources="microphone", type="filepath", label="Mic Input"),
257
+ gr.inputs.Textbox(lines=2, label="Text Input"),
258
+ gr.inputs.Dropdown(choices=choices, label="Your Native Language"),
259
+ gr.inputs.Dropdown(choices=choices, label="Language To Learn")
260
+ ]
261
+
262
+ outputs = [
263
+ RichTextbox(label="Processed Text"),
264
+ gr.outputs.Audio(label="Audio Output (Native Language) 1"),
265
+ gr.outputs.Audio(label="Audio Output (Target Language) 1"),
266
+ gr.outputs.Audio(label="Audio Output (Native Language) 2"),
267
+ gr.outputs.Audio(label="Audio Output (Target Language) 2"),
268
+ gr.outputs.Audio(label="Audio Output (Native Language) 3"),
269
+ gr.outputs.Audio(label="Audio Output (Target Language) 3"),
270
+ gr.outputs.Textbox(label="Focus 1"),
271
+ gr.outputs.Textbox(label="Translated Phrases 1"),
272
+ gr.outputs.Textbox(label="Focus 2"),
273
+ gr.outputs.Textbox(label="Translated Phrases 2"),
274
+ gr.outputs.Textbox(label="Focus 3"),
275
+ gr.outputs.Textbox(label="Translated Phrases 3")
276
+ ]
277
+ def update_outputs(image, file, audio, text, input_language, target_language):
278
+ final_text, top_phrases, translations, audio_outputs = process_input(
279
+ image=image, file=file, audio=audio, text=text,
280
+ translatefrom=input_language, translateto=target_language
281
+ )
282
+
283
+ processed_text_output = final_text
284
+ audio_output_native_phrases = [native for _, native in audio_outputs]
285
+ audio_output_target_phrases = [target for target, _ in audio_outputs]
286
+
287
+ longest_phrases_outputs = top_phrases[:3]
288
+ translated_phrases_outputs = translations[:3]
289
+ audio_outputs_native = audio_output_native_phrases[:3]
290
+ audio_outputs_target = audio_output_target_phrases[:3]
291
+
292
+ return (
293
+ processed_text_output,
294
+ *audio_outputs_native,
295
+ *audio_outputs_target,
296
+ *longest_phrases_outputs,
297
+ *translated_phrases_outputs
298
+ )
299
+
300
+ # Create the Gradio interface
301
+ iface = gr.Interface(fn=interface_func, inputs=inputs, outputs=outputs, title=title, description=description)
302
+
303
+ if __name__ == "__main__":
304
+ iface.launch()
305
+
306
+ # def main():
307
+ # with gr.Blocks() as demo:
308
+ # gr.Markdown(title)
309
+ # gr.Markdown(description)
310
 
311
+ # with gr.Row():
312
+ # input_language = gr.Dropdown(choices=choices, label="Your Native Language")
313
+ # target_language = gr.Dropdown(choices=choices, label="Language To Learn")
314
 
315
+ # with gr.Accordion("Talk To 🌟AyaTonic"):
316
+ # with gr.Tab("🤙🏻Audio & Text"):
317
+ # audio_input = gr.Audio(sources="microphone", type="filepath", label="Mic Input")
318
+ # text_input = gr.Textbox(lines=2, label="Text Input")
319
+ # with gr.Tab("📸Image & File"):
320
+ # image_input = gr.Image(type="pil", label="Camera Input")
321
+ # file_input = gr.File(label="File Upload")
322
 
323
+ # process_button = gr.Button("🌟AyaTonic")
324
 
325
+ # processed_text_output = RichTextbox(label="Processed Text")
326
+ # longest_phrases_1 = gr.Textbox(label="Focus")
327
+ # translated_phrases_output_1 = gr.Textbox(label="Translated Phrases")
328
+ # audio_output_native_phrase_1 = gr.Audio(label="Audio Output (Native Language)")
329
+ # audio_output_target_phrase_1 = gr.Audio(label="Audio Output (Target Language)")
330
+ # longest_phrases_2 = gr.Textbox(label="Focus")
331
+ # translated_phrases_output_2 = gr.Textbox(label="Translated Phrases")
332
+ # audio_output_native_phrase_2 = gr.Audio(label="Audio Output (Native Language)")
333
+ # audio_output_target_phrase_2 = gr.Audio(label="Audio Output (Target Language)")
334
+ # longest_phrases_3 = gr.Textbox(label="Focus")
335
+ # translated_phrases_output_3 = gr.Textbox(label="Translated Phrases")
336
+ # audio_output_native_phrase_3 = gr.Audio(label="Audio Output (Native Language)")
337
+ # audio_output_target_phrase_3 = gr.Audio(label="Audio Output (Target Language)")
338
 
339
+ # def update_outputs(image, file, audio, text, input_language, target_language):
340
+ # final_text, top_phrases, translations, audio_outputs = process_input(
341
+ # image=image, file=file, audio=audio, text=text,
342
+ # translatefrom=input_language, translateto=target_language
343
+ # )
344
+
345
+ # processed_text_output = final_text
346
+ # audio_output_native_phrases = [native for _, native in audio_outputs]
347
+ # audio_output_target_phrases = [target for target, _ in audio_outputs]
348
+
349
+ # longest_phrases_outputs = top_phrases[:3]
350
+ # translated_phrases_outputs = translations[:3]
351
+ # audio_outputs_native = audio_output_native_phrases[:3]
352
+ # audio_outputs_target = audio_output_target_phrases[:3]
353
+
354
+ # return (
355
+ # processed_text_output,
356
+ # *audio_outputs_native,
357
+ # *audio_outputs_target,
358
+ # *longest_phrases_outputs,
359
+ # *translated_phrases_outputs
360
+ # )
361
 
362
+ # process_button.click(
363
+ # fn=update_outputs,
364
+ # inputs=[image_input, file_input, audio_input, text_input, input_language, target_language],
365
+ # outputs=[
366
+ # processed_text_output,
367
+ # audio_output_native_phrase_1, audio_output_target_phrase_1,
368
+ # audio_output_native_phrase_2, audio_output_target_phrase_2,
369
+ # audio_output_native_phrase_3, audio_output_target_phrase_3,
370
+ # longest_phrases_1, translated_phrases_output_1,
371
+ # longest_phrases_2, translated_phrases_output_2,
372
+ # longest_phrases_3, translated_phrases_output_3
373
+ # ]
374
+ # )
375
 
376
+ # if __name__ == "__main__":
377
+ # main()