Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
import numpy as np
|
| 6 |
+
import scipy.io.wavfile as wavfile
|
| 7 |
+
|
| 8 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
device = 'cpu'
|
| 10 |
+
print("Device:", device)
|
| 11 |
+
|
| 12 |
+
pipe_translate = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en", device=device)
|
| 13 |
+
pipe_tts = pipeline("text-to-speech", model="facebook/mms-tts-eng", device=device) # Better quality, way faster than bark
|
| 14 |
+
|
| 15 |
+
def get_translation(text):
|
| 16 |
+
return pipe_translate(text)[0]["translation_text"]
|
| 17 |
+
|
| 18 |
+
def get_audio(text):
|
| 19 |
+
speech = pipe_tts(text)
|
| 20 |
+
return speech["sampling_rate"], (speech["audio"]* 32767).astype(np.int16).T
|
| 21 |
+
|
| 22 |
+
with gr.Blocks() as demo:
|
| 23 |
+
input_text = gr.Textbox(
|
| 24 |
+
label="Input text",
|
| 25 |
+
info="Your text",
|
| 26 |
+
lines=3,
|
| 27 |
+
placeholder="Écrire le texte à traduire",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
translation_button = gr.Button("Traduire...")
|
| 33 |
+
output_text = gr.Textbox(
|
| 34 |
+
label="Output text",
|
| 35 |
+
info="Your text",
|
| 36 |
+
lines=3,
|
| 37 |
+
placeholder="Votre traduction",
|
| 38 |
+
)
|
| 39 |
+
speech_button = gr.Button("Générer audio...")
|
| 40 |
+
translation_button.click(
|
| 41 |
+
get_translation,
|
| 42 |
+
inputs=[
|
| 43 |
+
input_text
|
| 44 |
+
],
|
| 45 |
+
outputs=[
|
| 46 |
+
output_text
|
| 47 |
+
],
|
| 48 |
+
)
|
| 49 |
+
speech_button.click(
|
| 50 |
+
get_audio,
|
| 51 |
+
inputs=[
|
| 52 |
+
output_text
|
| 53 |
+
],
|
| 54 |
+
outputs=[
|
| 55 |
+
gr.Audio(label="Output")
|
| 56 |
+
],
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
demo.launch()
|