samir-fama's picture
Update app.py
4790383
raw
history blame
2.7 kB
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)