Untitled
unknown
plain_text
6 months ago
2.5 kB
2
Indexable
from flask import Flask, request, render_template, send_file, redirect, url_for import os from googletrans import Translator app = Flask(__name__) # Initialize the translator translator = Translator() # Home page to upload subtitles @app.route('/') def index(): return ''' <h2>Subtitle Translator</h2> <form action="/edit" method="POST" enctype="multipart/form-data"> <input type="file" name="subtitle_file" accept=".srt" required> <button type="submit">Upload and Edit</button> </form> ''' # Edit subtitles before translating @app.route('/edit', methods=['POST']) def edit_subtitle(): subtitle_file = request.files['subtitle_file'] # Save the uploaded subtitle file temporarily subtitle_path = os.path.join("uploads", subtitle_file.filename) subtitle_file.save(subtitle_path) # Read the subtitle content with open(subtitle_path, 'r', encoding='utf-8') as file: subtitles = file.read() # Display subtitles in a textarea for editing return f''' <h2>Edit Subtitles</h2> <form action="/translate" method="POST"> <textarea name="edited_subtitles" rows="20" cols="80">{subtitles}</textarea><br> <button type="submit">Translate to Amharic</button> </form> ''' # Translate the edited subtitle @app.route('/translate', methods=['POST']) def translate_subtitle(): edited_subtitles = request.form['edited_subtitles'] # Split the content into lines for translation subtitles = edited_subtitles.splitlines() # Translate each line to Amharic translated_subtitles = [] for line in subtitles: if line.strip().isdigit() or '-->' in line: translated_subtitles.append(line) else: translation = translator.translate(line, src='en', dest='am') translated_subtitles.append(translation.text) # Join the translated subtitles into a single string translated_subtitles = '\n'.join(translated_subtitles) # Create translated subtitle file translated_path = os.path.join("translated", "translated_subtitle.srt") with open(translated_path, 'w', encoding='utf-8') as file: file.write(translated_subtitles) return send_file(translated_path, as_attachment=True) if __name__ == '__main__': os.makedirs("uploads", exist_ok=True) os.makedirs("translated", exist_ok=True) app.run(debug=True)
Editor is loading...
Leave a Comment