Spaces:
Sleeping
Sleeping
| from yt_dlp import YoutubeDL | |
| from moviepy.editor import AudioFileClip | |
| from faster_whisper import WhisperModel | |
| import subprocess | |
| from pyngrok import ngrok | |
| from flask import Flask, render_template, request, jsonify | |
| import os | |
| app = Flask(__name__) | |
| def get_output_webm_paths(folder_path): | |
| for root, dirs, files in os.walk(folder_path): | |
| for file in files: | |
| if file.startswith("output.webm"): | |
| file_path = os.path.join(root, file) | |
| return file_path | |
| def transcribe_video(video_path): | |
| youtube_link=video_path | |
| ydl_opts = { | |
| 'outtmpl': 'output.webm', | |
| } | |
| with YoutubeDL(ydl_opts) as ydl: | |
| ydl.download([youtube_link]) | |
| output_webm_path = get_output_webm_paths("/content/drive/MyDrive/Prometheus_RVC/transcript") | |
| audio = AudioFileClip(output_webm_path) | |
| audio.write_audiofile("/content/drive/MyDrive/Prometheus_RVC/transcript/output.wav") | |
| AUDIO_FILE_NAME = "/content/drive/MyDrive/Prometheus_RVC/transcript/output.wav" | |
| model_size = "medium" | |
| model = WhisperModel(model_size, device="cuda", compute_type="float16") | |
| segments, info = model.transcribe(AUDIO_FILE_NAME, beam_size=5) | |
| output_file = "/content/drive/MyDrive/Prometheus_RVC/transcript/transcription.txt" # 出力ファイル名 | |
| with open(output_file, "w") as file: | |
| for segment in segments: | |
| file.write("%s\n" % segment.text) | |
| text = open(output_file, "r").read() | |
| os.remove(output_webm_path) | |
| os.remove("/content/drive/MyDrive/Prometheus_RVC/transcript/output.wav") | |
| return text | |
| def transcript(): | |
| data = request.get_json() | |
| video_url = data['video_url'] | |
| transcript = transcribe_video(video_url) | |
| return jsonify({'transcript': transcript}) | |
| if __name__ == "__main__": | |
| ngrok_tunnel = ngrok.connect(5000) | |
| http_tunnel = ngrok.connect(addr="5000", proto="http", hostname="measured-smashing-piglet.ngrok-free.app") | |
| print(f"ngrok tunnel URL: {ngrok_tunnel.public_url}") | |
| app.run() |