Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import os
|
6 |
|
7 |
-
#
|
8 |
-
processor =
|
9 |
-
model =
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
video = gr.Video(video_file)
|
19 |
-
audio = video.audio
|
20 |
-
audio.write_audiofile(output_audio)
|
21 |
-
# Step 2: Speech-to-text
|
22 |
-
audio, sr = librosa.load(output_audio, sr=16000)
|
23 |
-
input_features = processor(audio, sampling_rate=sr, return_tensors="pt").input_features
|
24 |
-
predicted_ids = model.generate(input_features)
|
25 |
-
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
26 |
-
# Step 3: Text-to-speech
|
27 |
-
speech = bark_pipe(transcription)
|
28 |
-
output_file = "output_dubbed.wav"
|
29 |
-
scipy.io.wavfile.write(output_file, speech["sampling_rate"], speech["audio"])
|
30 |
-
# Step 4: Merge audio to video (temporary: agar video hai, toh audio replace karein)
|
31 |
-
# NOTE: Gradio ke current video component ke saath direct audio replace support nahi hai,
|
32 |
-
# toh hum sirf audio output file return karenge, jise user download kar sake
|
33 |
-
# Agar aapko video+audio merge karna hai, toh moviepy ka use karein, aur output video file return karein
|
34 |
-
# Yahan sirf audio output file return kar rahe hain
|
35 |
-
return transcription, output_file
|
36 |
-
|
37 |
-
# Moviepy se video+audio merge (optional, agar video chahiye)
|
38 |
-
def merge_audio_to_video(video_file, audio_file, output_video="output_dubbed.mp4"):
|
39 |
-
import moviepy.editor as mp
|
40 |
-
video = mp.VideoFileClip(video_file)
|
41 |
-
audio = mp.AudioFileClip(audio_file)
|
42 |
-
video = video.set_audio(audio)
|
43 |
-
video.write_videofile(output_video)
|
44 |
-
return output_video
|
45 |
-
|
46 |
-
# NOTE: Gradio Audio component sirf audio file upload karta hai, video file ke liye Gradio Video component use karein
|
47 |
-
# Lekin Gradio Video component output mein filepath return nahi karta, toh hum sirf audio file return karenge
|
48 |
|
49 |
with gr.Blocks() as demo:
|
50 |
-
gr.Markdown("
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
inputs=file_in,
|
59 |
-
outputs=[transcription_out, audio_out]
|
60 |
)
|
61 |
-
# Agar video output chahiye, toh yeh function use karein (optional, Gradio Video output ke liye thoda advanced code chahiye)
|
62 |
-
# Yahan sirf audio output hai
|
63 |
|
64 |
-
demo.launch(
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, BarkModel
|
3 |
+
import scipy.io.wavfile
|
4 |
+
import numpy as np
|
|
|
5 |
|
6 |
+
# Model और Processor लोड करें
|
7 |
+
processor = AutoProcessor.from_pretrained("suno/bark")
|
8 |
+
model = BarkModel.from_pretrained("suno/bark")
|
9 |
+
model.to("cuda") # अगर GPU उपलब्ध है
|
10 |
|
11 |
+
def generate_speech(text):
|
12 |
+
inputs = processor(text, voice_preset="v2/hi_speaker_1") # हिंदी के लिए
|
13 |
+
audio_array = model.generate(**inputs)
|
14 |
+
audio_array = audio_array.cpu().numpy().squeeze()
|
15 |
+
sample_rate = model.generation_config.sample_rate
|
16 |
+
scipy.io.wavfile.write("output.wav", rate=sample_rate, data=audio_array)
|
17 |
+
return "output.wav"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
with gr.Blocks() as demo:
|
20 |
+
gr.Markdown("## Suno Bark Text-to-Speech")
|
21 |
+
text_input = gr.Textbox(label="टेक्स्ट इनपुट करें")
|
22 |
+
audio_output = gr.Audio(label="ऑडियो आउटपुट")
|
23 |
+
generate_button = gr.Button("Generate Speech")
|
24 |
+
generate_button.click(
|
25 |
+
fn=generate_speech,
|
26 |
+
inputs=text_input,
|
27 |
+
outputs=audio_output
|
|
|
|
|
28 |
)
|
|
|
|
|
29 |
|
30 |
+
demo.launch()
|
31 |
+
|