Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""App.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1VSsT1xPpcetosAuTtw9TavvN4tYuV3Jp
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import whisper
|
| 12 |
+
import os
|
| 13 |
+
from pydub import AudioSegment
|
| 14 |
+
|
| 15 |
+
# Load Whisper model (can be "small", "medium", "large", etc.)
|
| 16 |
+
model = whisper.load_model("base")
|
| 17 |
+
|
| 18 |
+
def convert_video_to_audio(video_path):
|
| 19 |
+
"""Convert video file to WAV audio."""
|
| 20 |
+
audio_path = os.path.splitext(video_path)[0] + ".wav"
|
| 21 |
+
audio = AudioSegment.from_file(video_path)
|
| 22 |
+
audio.export(audio_path, format="wav")
|
| 23 |
+
return audio_path
|
| 24 |
+
|
| 25 |
+
def chunk_audio(audio_path, chunk_size_mb=25, max_duration_sec=900):
|
| 26 |
+
"""Chunk audio if too large or long."""
|
| 27 |
+
audio = AudioSegment.from_file(audio_path)
|
| 28 |
+
chunk_limit_bytes = chunk_size_mb * 1024 * 1024
|
| 29 |
+
max_duration_ms = min(len(audio), max_duration_sec * 1000)
|
| 30 |
+
|
| 31 |
+
if len(audio) > max_duration_ms or audio.frame_count() * audio.frame_width > chunk_limit_bytes:
|
| 32 |
+
return [audio[:max_duration_ms]]
|
| 33 |
+
return [audio]
|
| 34 |
+
|
| 35 |
+
def translate_audio(file):
|
| 36 |
+
"""Translate audio/video to English."""
|
| 37 |
+
if file is None:
|
| 38 |
+
return "Error: No file uploaded.", None
|
| 39 |
+
|
| 40 |
+
ext = os.path.splitext(file.name)[1].lower()
|
| 41 |
+
is_video = ext in ['.mp4', '.avi', '.mov']
|
| 42 |
+
|
| 43 |
+
audio_path = convert_video_to_audio(file.name) if is_video else file.name
|
| 44 |
+
chunks = chunk_audio(audio_path)
|
| 45 |
+
|
| 46 |
+
translations = []
|
| 47 |
+
for i, chunk in enumerate(chunks):
|
| 48 |
+
chunk_file = f"chunk_{i}.wav"
|
| 49 |
+
chunk.export(chunk_file, format="wav")
|
| 50 |
+
result = model.transcribe(chunk_file, task="translate") # 👈 Use task='translate'
|
| 51 |
+
translations.append(result["text"])
|
| 52 |
+
os.remove(chunk_file)
|
| 53 |
+
|
| 54 |
+
if is_video:
|
| 55 |
+
os.remove(audio_path)
|
| 56 |
+
|
| 57 |
+
full_translation = " ".join(translations)
|
| 58 |
+
output_txt = os.path.splitext(os.path.basename(file.name))[0] + "_translated.txt"
|
| 59 |
+
|
| 60 |
+
with open(output_txt, "w", encoding="utf-8") as f:
|
| 61 |
+
f.write(full_translation)
|
| 62 |
+
|
| 63 |
+
return full_translation, output_txt
|
| 64 |
+
|
| 65 |
+
# Gradio Interface
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=translate_audio,
|
| 68 |
+
inputs=gr.File(label="Upload Audio or Video File (Any Language)"),
|
| 69 |
+
outputs=[
|
| 70 |
+
gr.Textbox(label="English Translation"),
|
| 71 |
+
gr.File(label="Download Translation (.txt)")
|
| 72 |
+
],
|
| 73 |
+
title="AutoTranslate AI",
|
| 74 |
+
description=(
|
| 75 |
+
"Upload an audio or video file in any language. "
|
| 76 |
+
"The tool will translate its spoken content to English using OpenAI Whisper. "
|
| 77 |
+
"Files over 25MB or longer than 15 minutes will be chunked automatically."
|
| 78 |
+
)
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
iface.launch(share=True)
|