File size: 2,264 Bytes
966d998
 
 
35e43fd
 
8107aa0
e3bb042
 
 
35e43fd
3bfabc7
35e43fd
 
3bfabc7
35e43fd
 
 
 
3bfabc7
8107aa0
 
3bfabc7
35e43fd
 
966d998
 
 
 
 
 
 
 
 
 
35e43fd
966d998
 
 
 
 
 
8107aa0
 
 
 
 
966d998
 
 
 
 
 
 
 
3bfabc7
 
966d998
 
 
 
 
3bfabc7
966d998
 
 
711a612
55884fe
cbaf479
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import gradio as gr
import yt_dlp
import torch
import numpy as np
from faster_whisper import WhisperModel
import os
os.system("pip install -q gradio torch whisper faster-whisper yt-dlp numpy")



# Load model sekali aja
model = WhisperModel("small", device="cpu", compute_type="float32")

# Fungsi untuk transkripsi dari file
def transcribe_audio(file):
    segments, _ = model.transcribe(file)
    transcript = "\n".join(segment.text for segment in segments)

    # Simpan ke file TXT
    file_path = "transcription.txt"
    with open(file_path, "w", encoding="utf-8") as f:
        f.write(transcript)

    return f"**Transcription:**\n{transcript}"

# Fungsi untuk ambil audio dari YouTube
def get_audio_from_youtube(url):
    ydl_opts = {
        "format": "bestaudio/best",
        "postprocessors": [{"key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "192"}],
        "outtmpl": "temp_audio.%(ext)s",
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=True)
        return "temp_audio.mp3"

# Fungsi untuk transkripsi dari YouTube
def transcribe_youtube(url):
    audio_file = get_audio_from_youtube(url)
    return transcribe_audio(audio_file)

#memastikan file ada sebelum digunakan
if not os.path.exists('transcription.txt'):
    with open("transcription.txt", "w", encoding="utf-8") as f:
        f.write("")

# Gradio UI
with gr.Blocks() as app:
    gr.Markdown("# 🎤 YouTube & Audio Transcriber with Whisper AI")
    
    with gr.Tab("Upload File"):
        audio_input = gr.File(label="Upload Audio File")
        file_transcribe_button = gr.Button("Transcribe")
        file_output = gr.Textbox(label="Transcription")
        download_file = gr.File(label="Download Transcription", value="transcription.txt")

    with gr.Tab("YouTube Video"):
        youtube_url = gr.Textbox(label="YouTube URL")
        yt_transcribe_button = gr.Button("Transcribe")
        yt_output = gr.Textbox(label="Transcription")

    file_transcribe_button.click(transcribe_audio, inputs=audio_input, outputs=[file_output, download_file])
    yt_transcribe_button.click(transcribe_youtube, inputs=youtube_url, outputs=yt_output)

# Run Gradio app
app.launch(server_name="0.0.0.0", server_port=7880)