Spaces:
Sleeping
Sleeping
fixed
Browse files
app.py
CHANGED
|
@@ -1,35 +1,7 @@
|
|
| 1 |
-
#
|
| 2 |
-
#
|
| 3 |
-
#
|
| 4 |
-
#
|
| 5 |
-
# Builds a Streamlit web interface that ties everything together.
|
| 6 |
-
#
|
| 7 |
-
#Step 1: Extracting YouTube Data
|
| 8 |
-
# The first step involves extracting the video ID, metadata (title and channel name),
|
| 9 |
-
# transcript, and thumbnail image from a YouTube URL.
|
| 10 |
-
#
|
| 11 |
-
#Step 2: Summarizing the Transcript
|
| 12 |
-
# With the transcript in hand, the next step is to summarize it. This is done using
|
| 13 |
-
# the OpenAI API in summarize_text.py. Before running this script, make sure to set
|
| 14 |
-
# your OpenAI API key as an environment variable:
|
| 15 |
-
#
|
| 16 |
-
#Step 3: Building the Web Interface
|
| 17 |
-
# Finally, the web interface is created using Streamlit. Streamlit is a Python library
|
| 18 |
-
# that simplifies the process of creating web applications for data science and machine
|
| 19 |
-
# learning projects. This file combines the functionality of the other two scripts and
|
| 20 |
-
# provides a user-friendly interface.
|
| 21 |
-
#Bringing It All Together
|
| 22 |
-
# Extract Video ID and Metadata: When a user inputs a YouTube URL, the app extracts the
|
| 23 |
-
# video ID and retrieves the title and channel name using extract_metadata(url).
|
| 24 |
-
# Download Thumbnail: The app downloads the video’s thumbnail and displays it using
|
| 25 |
-
# get_thumbnail_from_url(url).
|
| 26 |
-
# Get and Summarize Transcript: The transcript is fetched using get_transcript_from_url(url)
|
| 27 |
-
# and summarized using summarize_transcript(transcript, language). The summary is displayed
|
| 28 |
-
# in the selected language.
|
| 29 |
-
#################################################################################################
|
| 30 |
-
# TO RUN, execute on the terminal:
|
| 31 |
-
#streamlit run C:\projects\ewp\ai-projects\ewp-sales-bots-repo\ewp-sales-bots-program\notebooks\summarize_mp4_video.py
|
| 32 |
-
#
|
| 33 |
|
| 34 |
import os
|
| 35 |
import subprocess
|
|
@@ -56,14 +28,13 @@ app=FastAPI()
|
|
| 56 |
# Initialize the text generation pipeline
|
| 57 |
#pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 58 |
|
| 59 |
-
|
| 60 |
@app.get("/")
|
| 61 |
def home():
|
| 62 |
return {"message":"Hello World"}
|
| 63 |
|
| 64 |
# Define a function to handle the GET request at `/generate`
|
| 65 |
-
|
| 66 |
-
|
| 67 |
@app.get("/generate")
|
| 68 |
def generate(text:str):
|
| 69 |
## use the pipeline to generate text from given input text
|
|
@@ -72,6 +43,12 @@ def generate(text:str):
|
|
| 72 |
## return the generate text in Json reposne
|
| 73 |
return {"output":output[0]['generated_text']}
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
@app.get("/transcribe")
|
| 76 |
def get_transcript_mp4(mp4_path):
|
| 77 |
#path_to_check = r"C:\Program Files\ffmpeg\bin"
|
|
@@ -117,7 +94,11 @@ def get_transcript_mp4(mp4_path):
|
|
| 117 |
else:
|
| 118 |
print("File NOT found! Check the path for: ", mp4_path)
|
| 119 |
return "Test summary"
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
@app.get("/summarize")
|
| 122 |
def summarize_text(text, lang='en'):
|
| 123 |
OpenAI.api_key = os.getenv("OPENAI_API_KEY")
|
|
@@ -150,155 +131,3 @@ def summarize_text(text, lang='en'):
|
|
| 150 |
return summary_text
|
| 151 |
|
| 152 |
|
| 153 |
-
import torch
|
| 154 |
-
|
| 155 |
-
import gradio as gr
|
| 156 |
-
import yt_dlp as youtube_dl
|
| 157 |
-
from transformers import pipeline
|
| 158 |
-
from transformers.pipelines.audio_utils import ffmpeg_read
|
| 159 |
-
|
| 160 |
-
import tempfile
|
| 161 |
-
import os
|
| 162 |
-
|
| 163 |
-
MODEL_NAME = "openai/whisper-large-v3"
|
| 164 |
-
BATCH_SIZE = 8
|
| 165 |
-
FILE_LIMIT_MB = 1000
|
| 166 |
-
YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
|
| 167 |
-
|
| 168 |
-
device = 0 if torch.cuda.is_available() else "cpu"
|
| 169 |
-
|
| 170 |
-
pipe = pipeline(
|
| 171 |
-
task="automatic-speech-recognition",
|
| 172 |
-
model=MODEL_NAME,
|
| 173 |
-
chunk_length_s=30,
|
| 174 |
-
device=device,
|
| 175 |
-
)
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
def transcribe(inputs, task):
|
| 179 |
-
if inputs is None:
|
| 180 |
-
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
| 181 |
-
|
| 182 |
-
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 183 |
-
return text
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
def _return_yt_html_embed(yt_url):
|
| 187 |
-
video_id = yt_url.split("?v=")[-1]
|
| 188 |
-
HTML_str = (
|
| 189 |
-
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
| 190 |
-
" </center>"
|
| 191 |
-
)
|
| 192 |
-
return HTML_str
|
| 193 |
-
|
| 194 |
-
def download_yt_audio(yt_url, filename):
|
| 195 |
-
info_loader = youtube_dl.YoutubeDL()
|
| 196 |
-
|
| 197 |
-
try:
|
| 198 |
-
info = info_loader.extract_info(yt_url, download=False)
|
| 199 |
-
except youtube_dl.utils.DownloadError as err:
|
| 200 |
-
raise gr.Error(str(err))
|
| 201 |
-
|
| 202 |
-
file_length = info["duration_string"]
|
| 203 |
-
file_h_m_s = file_length.split(":")
|
| 204 |
-
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
| 205 |
-
|
| 206 |
-
if len(file_h_m_s) == 1:
|
| 207 |
-
file_h_m_s.insert(0, 0)
|
| 208 |
-
if len(file_h_m_s) == 2:
|
| 209 |
-
file_h_m_s.insert(0, 0)
|
| 210 |
-
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
| 211 |
-
|
| 212 |
-
if file_length_s > YT_LENGTH_LIMIT_S:
|
| 213 |
-
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
| 214 |
-
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
| 215 |
-
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
| 216 |
-
|
| 217 |
-
ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
| 218 |
-
|
| 219 |
-
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
| 220 |
-
try:
|
| 221 |
-
ydl.download([yt_url])
|
| 222 |
-
except youtube_dl.utils.ExtractorError as err:
|
| 223 |
-
raise gr.Error(str(err))
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
| 227 |
-
html_embed_str = _return_yt_html_embed(yt_url)
|
| 228 |
-
|
| 229 |
-
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 230 |
-
filepath = os.path.join(tmpdirname, "video.mp4")
|
| 231 |
-
download_yt_audio(yt_url, filepath)
|
| 232 |
-
with open(filepath, "rb") as f:
|
| 233 |
-
inputs = f.read()
|
| 234 |
-
|
| 235 |
-
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
| 236 |
-
inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
| 237 |
-
|
| 238 |
-
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 239 |
-
|
| 240 |
-
return html_embed_str, text
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
demo = gr.Blocks()
|
| 244 |
-
|
| 245 |
-
mf_transcribe = gr.Interface(
|
| 246 |
-
fn=transcribe,
|
| 247 |
-
inputs=[
|
| 248 |
-
#gr.Audio(source="microphone", type="filepath", optional=True),
|
| 249 |
-
gr.Audio(sources="microphone", type="filepath"),
|
| 250 |
-
gr.Radio(["transcribe", "translate"], label="Task"),
|
| 251 |
-
],
|
| 252 |
-
outputs="text",
|
| 253 |
-
#layout="horizontal",
|
| 254 |
-
theme="huggingface",
|
| 255 |
-
title="Whisper Large V3: Transcribe Audio",
|
| 256 |
-
description=(
|
| 257 |
-
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
|
| 258 |
-
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
| 259 |
-
" of arbitrary length."
|
| 260 |
-
),
|
| 261 |
-
allow_flagging="never",
|
| 262 |
-
)
|
| 263 |
-
|
| 264 |
-
file_transcribe = gr.Interface(
|
| 265 |
-
fn=transcribe,
|
| 266 |
-
inputs=[
|
| 267 |
-
gr.Audio(sources="upload", type="filepath", label="Audio file"),
|
| 268 |
-
gr.Radio(["transcribe", "translate"], label="Task"),
|
| 269 |
-
],
|
| 270 |
-
outputs="text",
|
| 271 |
-
#layout="horizontal",
|
| 272 |
-
theme="huggingface",
|
| 273 |
-
title="Whisper Large V3: Transcribe Audio",
|
| 274 |
-
description=(
|
| 275 |
-
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
|
| 276 |
-
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
| 277 |
-
" of arbitrary length."
|
| 278 |
-
),
|
| 279 |
-
allow_flagging="never",
|
| 280 |
-
)
|
| 281 |
-
|
| 282 |
-
yt_transcribe = gr.Interface(
|
| 283 |
-
fn=yt_transcribe,
|
| 284 |
-
inputs=[
|
| 285 |
-
gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
| 286 |
-
gr.Radio(["transcribe", "translate"], label="Task")
|
| 287 |
-
],
|
| 288 |
-
outputs=["html", "text"],
|
| 289 |
-
#layout="horizontal",
|
| 290 |
-
theme="huggingface",
|
| 291 |
-
title="Whisper Large V3: Transcribe YouTube",
|
| 292 |
-
description=(
|
| 293 |
-
"Transcribe long-form YouTube videos with the click of a button! Demo uses the OpenAI Whisper checkpoint"
|
| 294 |
-
f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
|
| 295 |
-
" arbitrary length."
|
| 296 |
-
),
|
| 297 |
-
allow_flagging="never",
|
| 298 |
-
)
|
| 299 |
-
|
| 300 |
-
with demo:
|
| 301 |
-
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
| 302 |
-
|
| 303 |
-
#demo.launch(enable_queue=True)
|
| 304 |
-
demo.launch()
|
|
|
|
| 1 |
+
#####################################################################
|
| 2 |
+
#This application provides and esample of FASTAPI for
|
| 3 |
+
# transcribing an mp4 video and summarizing the transcription
|
| 4 |
+
######################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import subprocess
|
|
|
|
| 28 |
# Initialize the text generation pipeline
|
| 29 |
#pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 30 |
|
| 31 |
+
# This is default function
|
| 32 |
@app.get("/")
|
| 33 |
def home():
|
| 34 |
return {"message":"Hello World"}
|
| 35 |
|
| 36 |
# Define a function to handle the GET request at `/generate`
|
| 37 |
+
#
|
|
|
|
| 38 |
@app.get("/generate")
|
| 39 |
def generate(text:str):
|
| 40 |
## use the pipeline to generate text from given input text
|
|
|
|
| 43 |
## return the generate text in Json reposne
|
| 44 |
return {"output":output[0]['generated_text']}
|
| 45 |
|
| 46 |
+
# This function
|
| 47 |
+
# 1. takes mp4 video as input
|
| 48 |
+
# 2. creates mp3 file from mp4 file
|
| 49 |
+
# 3. creates wav file from mp3 file
|
| 50 |
+
# 4. transcribes to text from the wav file
|
| 51 |
+
|
| 52 |
@app.get("/transcribe")
|
| 53 |
def get_transcript_mp4(mp4_path):
|
| 54 |
#path_to_check = r"C:\Program Files\ffmpeg\bin"
|
|
|
|
| 94 |
else:
|
| 95 |
print("File NOT found! Check the path for: ", mp4_path)
|
| 96 |
return "Test summary"
|
| 97 |
+
|
| 98 |
+
# This function
|
| 99 |
+
# 1. takes text as input
|
| 100 |
+
# 2. summarizes the input text
|
| 101 |
+
|
| 102 |
@app.get("/summarize")
|
| 103 |
def summarize_text(text, lang='en'):
|
| 104 |
OpenAI.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 131 |
return summary_text
|
| 132 |
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|