File size: 2,698 Bytes
e437e88
 
4790383
859958b
 
 
 
 
 
e437e88
859958b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e437e88
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
import os
os.system("pip install git+https://github.com/openai/whisper.git")
os.system("pip install pytube")
import whisper
from pytube import YouTube
import gradio as gr
import os
import re


model = whisper.load_model("small")

# def compress_audio(file_path, bitrate='32k'):
#     try:
#         audio = AudioSegment.from_file(file_path)
#         output_format = os.path.splitext(file_path)[1][1:]
#         compressed_audio = audio.export(file_path, format=output_format, bitrate=bitrate)
#         return True
#     except Exception as e:
#         print(f"Error: {e}")
#         return False

def url_to_text(url):
    if url != '':
        output_text_transcribe = ''

    yt = YouTube(url)
    video = yt.streams.filter(only_audio=True).first()
    out_file=video.download(output_path=".")
    file_stats = os.stat(out_file)
    
    if file_stats.st_size <= 30_000_000:
    
        base, ext = os.path.splitext(out_file)
        os.rename(out_file, base+'.mp3')
        file_path = base+'.mp3'
        # compress_audio(file_path)
        
        result = model.transcribe(file_path)
        return result['text'].strip()
    else:
        raise gr.Error("Exception: Problems with the audio transcription.")

def get_summary(article):
    first_sentences = ' '.join(re.split(r'(?<=[.:;])\s', article)[:5])
    b = summarizer(first_sentences, min_length = 20, max_length = 120, do_sample = False)
    b = b[0]['summary_text'].replace(' .', '.').strip()
    return b

with gr.Blocks() as demo:
    gr.Markdown("<h1><center>Samir's AI Model Implementation -  Automatic Speech Recognition</center></h1>")
    gr.Markdown("<h2><center>YouTube Audio AutoTranscribe: Effortless Transcription</center></h2>")
    gr.Markdown("<center><b>This application is using <a href=https://openai.com/blog/whisper/ target=_blank>OpenAI's Whisper</a>. Whisper is an intricately designed <br>neural network aiming to achieve the highest precision in the field of multilingual speech recognition.</b></center>")
    gr.Markdown("<center><b>The time for the model to perform transcription typically takes around 10 seconds for every 1 minute of video. <br>For example, a 12-minute video would take approximately 120 seconds to transcribe the audio content.</b></center>")
    
    input_text_url = gr.Textbox(placeholder='👇Youtube Video URL👇', label='YouTube URL')
    result_button_transcribe = gr.Button('Transcribe Now')
    output_text_transcribe = gr.Textbox(placeholder='Transcription of the YouTube video.', label='Transcript')
    
    result_button_transcribe.click(url_to_text, inputs = input_text_url, outputs = output_text_transcribe)

demo.queue(default_enabled = True).launch(debug = False)