samir-fama's picture
Update app.py
cc89b7d
raw
history blame
2.67 kB
import os
os.system("python -m pip install --upgrade pip")
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("base")
# 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>Samir's AI Model Implementation - Automatic Speech Recognition</h1>")
gr.Markdown("<h2>YouTube Audio AutoTranscribe: Effortless Transcription</h2>")
gr.Markdown("<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>")
gr.Markdown("<b>The time for the model to perform transcription typically takes around 30 seconds for every 1 minute of video. <br>For example, a 10-minute video would take approximately 300 seconds to transcribe the audio content.</b>")
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='👇Transcription👇')
result_button_transcribe.click(url_to_text, inputs = input_text_url, outputs = output_text_transcribe)
demo.queue().launch(debug = False)