Translate / app.py
Athspi's picture
Update app.py
a30e87b verified
raw
history blame
6.04 kB
# app.py
import os
import time
import tempfile
import uuid
import google.generativeai as genai
import requests
from flask import Flask, request, render_template, send_from_directory, url_for, flash
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.audio.io.AudioFileClip import AudioFileClip
from werkzeug.utils import secure_filename
from dotenv import load_dotenv
# Initialize Flask app and load secrets
load_dotenv()
app = Flask(__name__)
# Configuration
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
TTS_API_URL = os.getenv("TTS_API_URL")
if not GEMINI_API_KEY:
raise ValueError("GEMINI_API_KEY not found in .env file!")
if not TTS_API_URL:
raise ValueError("TTS_API_URL not found in .env file!")
genai.configure(api_key=GEMINI_API_KEY)
# Setup directories
UPLOAD_FOLDER = 'uploads'
DOWNLOAD_FOLDER = 'downloads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(DOWNLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024
app.secret_key = os.urandom(24)
# Constants
VOICE_CHOICES = {
"Male (Charon)": "Charon",
"Female (Zephyr)": "Zephyr"
}
GEMINI_PROMPT = """
You are an expert AI scriptwriter. Your task is to watch the provided video and transcribe ALL spoken dialogue into a SINGLE, CONTINUOUS block of modern, colloquial Tamil.
**CRITICAL INSTRUCTIONS:**
1. Combine all dialogue into one continuous script.
2. NO timestamps or speaker labels.
3. Add performance directions (e.g., `Say happily:`, `[laugh]`) directly in the text.
**EXAMPLE OUTPUT:**
Say happily: வணக்கம்! [laugh] எப்படி இருக்கீங்க? Whisper mysteriously: அந்த ரகசியம் எனக்கு மட்டும் தான் தெரியும்.
"""
def generate_tamil_script(video_path):
"""Generate Tamil script from video using Gemini AI"""
print("Uploading video to Gemini...")
video_file = genai.upload_file(video_path, mime_type="video/mp4")
while video_file.state.name == "PROCESSING":
time.sleep(5)
video_file = genai.get_file(video_file.name)
if video_file.state.name != "ACTIVE":
raise Exception(f"Gemini processing failed: {video_file.state.name}")
model = genai.GenerativeModel(model_name="models/gemini-2.5-flash")
response = model.generate_content([GEMINI_PROMPT, video_file])
genai.delete_file(video_file.name)
if hasattr(response, 'text') and response.text:
return " ".join(response.text.strip().splitlines())
raise Exception("No valid script generated")
def generate_audio(script, voice, is_cheerful, output_path):
"""Generate audio from script using TTS API"""
print(f"Generating audio (Voice: {voice}, Cheerful: {is_cheerful})")
payload = {
"text": script,
"voice_name": voice,
"cheerful": is_cheerful
}
response = requests.post(TTS_API_URL, json=payload, timeout=300)
if response.status_code == 200:
with open(output_path, "wb") as f:
f.write(response.content)
return True
raise Exception(f"TTS API error: {response.status_code} - {response.text}")
def dub_video(video_path, audio_path, output_path):
"""Replace video audio with generated audio"""
print("Dubbing video...")
video_clip = AudioFileClip = None
try:
video_clip = VideoFileClip(video_path)
audio_clip = AudioFileClip(audio_path)
video_clip.audio = audio_clip
video_clip.write_videofile(
output_path,
codec="libx264",
audio_codec="aac",
logger='bar'
)
finally:
if audio_clip: audio_clip.close()
if video_clip: video_clip.close()
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/process', methods=['POST'])
def process_video():
input_path = audio_path = None
try:
# Validate upload
if 'video' not in request.files:
flash("No file selected", "error")
return render_template('index.html')
file = request.files['video']
if file.filename == '':
flash("No file selected", "error")
return render_template('index.html')
# Save uploaded file
filename = secure_filename(file.filename)
input_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(input_path)
# Process options
voice = VOICE_CHOICES[request.form['voice_choice']]
cheerful = 'cheerful' in request.form
# Generate script and audio
script = generate_tamil_script(input_path)
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
audio_path = f.name
generate_audio(script, voice, cheerful, audio_path)
# Create dubbed video
output_filename = f"dubbed_{filename}"
output_path = os.path.join(app.config['DOWNLOAD_FOLDER'], output_filename)
dub_video(input_path, audio_path, output_path)
flash("Video processing complete!", "success")
return render_template('index.html',
result_video=url_for('serve_video', filename=output_filename),
script=script)
except Exception as e:
print(f"Error: {e}")
flash(f"Processing failed: {str(e)}", "error")
return render_template('index.html')
finally:
# Cleanup temp files
if input_path and os.path.exists(input_path):
os.remove(input_path)
if audio_path and os.path.exists(audio_path):
os.remove(audio_path)
@app.route('/downloads/<filename>')
def serve_video(filename):
return send_from_directory(app.config['DOWNLOAD_FOLDER'], filename)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=7860)