File size: 5,687 Bytes
9d177c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import gradio as gr
import requests
from yt_dlp import YoutubeDL
import os
import tempfile
from typing import Optional
import json
import subprocess

# Define the FastAPI URL
API_URL = "http://localhost:9998"

def download_youtube_audio(url: str, output_dir: Optional[str] = None) -> str:
    if output_dir is None:
        output_dir = tempfile.gettempdir()

    yt_dlp_command = [
        "/home/ubuntu/miniconda3/envs/gradio_whisper/bin/yt-dlp",
        "-f", "bestaudio/best",
        "-x",  # Extract audio
        "--audio-format", "mp3",
        "--audio-quality", "192K",
        "-o", os.path.join(output_dir, "%(id)s.%(ext)s"),
        "-v",
        "--print", "after_move:filepath",  # Print the output filepath
        "--username", "oauth2",
        "--password", "", 
        "--no-playlist",
        "--print-json",
        url
    ]

    try:
        print(' '.join(yt_dlp_command))
        result = subprocess.run(yt_dlp_command, capture_output=True, text=True, check=True)
        
        # Parse the JSON output to get video information
        video_info = json.loads(result.stdout.splitlines()[-2])  # The last line is the filepath
        
        # Get the output filepath from the last line of stdout
        audio_file = result.stdout.splitlines()[-1].strip()
        
        print(f"Successfully downloaded: {video_info['title']}")
        return audio_file
    except subprocess.CalledProcessError as e:
        raise Exception(f"Error downloading YouTube audio: {e.stderr}")
    except json.JSONDecodeError:
        raise Exception("Error parsing video information")
    except Exception as e:
        raise Exception(f"Unexpected error: {str(e)}")


def run_asr(audio_file, youtube_url):
    temp_file = None
    try:
        if youtube_url:
            # It's a YouTube URL
            audio_file = download_youtube_audio(youtube_url)
            temp_file = audio_file
        elif not audio_file:
            return "Please provide either an audio file or a YouTube URL."

        files = {'file': open(audio_file, 'rb')}
        data = {'language': 'en', 'model_name': 'whisper-large-v2-imda'}
        response = requests.post(f"{API_URL}/asr", data=data, files=files)

        if response.status_code == 200:
            return response.json().get("text", "")
        else:
            return f"Error: {response.status_code}"
    except Exception as e:
        return f"Error: {str(e)}"
    finally:
        # Clean up the temporary file if it was a YouTube download
        if temp_file and os.path.exists(temp_file):
            os.remove(temp_file)

def embed_youtube(youtube_url):
    if youtube_url:
        try:
            # video_id = YoutubeDL().extract_info(youtube_url, download=False)['id']\
            video_id = youtube_url.split("v=")[1]
            print(video_id)
            embed_html = f'<iframe width="560" height="315" src="https://www.youtube.com/embed/{video_id}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>'
            return gr.update(value=embed_html, visible=True), "", None
        except:
            return gr.update(value="", visible=False), "Invalid YouTube URL", None
    return gr.update(value="", visible=False), "", None

def clear_on_audio_input(audio):
    if audio is not None:
        return "", gr.update(value="", visible=False), ""
    return gr.update(), gr.update(), gr.update()

# Create the Gradio interface with improved aesthetics
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# πŸŽ™οΈ Audio Transcription Service")
    gr.Markdown("Upload an audio file, record your voice, or paste a YouTube URL to get an English transcription.")
    
    with gr.Row():
        with gr.Column(scale=2):
            audio_input = gr.Audio(sources=['microphone', 'upload'], type="filepath", label="Audio Input")
            youtube_input = gr.Textbox(label="YouTube URL", placeholder="Or paste a YouTube URL here...")
            video_player = gr.HTML(visible=False)
        with gr.Column(scale=3):
            result = gr.Textbox(
                label="Transcription Result",
                placeholder="Your transcription will appear here...",
                lines=10
            )

    run_button = gr.Button("πŸš€ Transcribe Audio", variant="primary")
    run_button.click(run_asr, inputs=[audio_input, youtube_input], outputs=[result])

    # Update video player and clear transcription and audio input when YouTube URL is entered
    youtube_input.change(
        fn=embed_youtube,
        inputs=[youtube_input],
        outputs=[video_player, result, audio_input]
    )

    # Clear transcription, YouTube input, and video player when audio is input
    audio_input.change(
        fn=clear_on_audio_input,
        inputs=[audio_input],
        outputs=[result, video_player, youtube_input]
    )

    gr.Markdown("### How to use:")
    gr.Markdown("1. Upload an audio file or record your voice using the microphone, OR paste a YouTube URL.")
    gr.Markdown("2. If you paste a YouTube URL, the video will be displayed for your reference, and any previous transcription or audio input will be cleared.")
    gr.Markdown("3. If you upload or record audio, any previous transcription, YouTube URL, and video will be cleared.")
    gr.Markdown("4. Click the 'Transcribe Audio' button to start the process.")
    gr.Markdown("5. Wait for a few seconds, and your transcription will appear in the result box.")

# Launch the Gradio interface
demo.launch(
    server_name='0.0.0.0', 
    server_port=5008, 
    ssl_certfile='/home/ubuntu/astarwiz_com/astarwiz_com.crt', 
    ssl_keyfile='/home/ubuntu/astarwiz_com/astarwiz_com.key', 
    ssl_verify=False
)