Spaces:
Sleeping
Sleeping
Commented th epipeline statement
Browse files
app.py
CHANGED
|
@@ -1,150 +1,150 @@
|
|
| 1 |
-
# ########## Overview ################################################################
|
| 2 |
-
# The application is divided into three main components:
|
| 3 |
-
# Extracts metadata, transcripts, and thumbnails from YouTube videos.
|
| 4 |
-
# Summarizes the extracted transcripts using the OpenAI API.
|
| 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
|
| 36 |
-
from moviepy.video.io.VideoFileClip import VideoFileClip
|
| 37 |
-
from moviepy.audio.io.AudioFileClip import AudioFileClip
|
| 38 |
-
import whisper
|
| 39 |
-
from openai import OpenAI
|
| 40 |
-
from fastapi import FastAPI
|
| 41 |
-
from transformers import pipeline
|
| 42 |
-
|
| 43 |
-
#import os
|
| 44 |
-
#os.system("apt-get update && apt-get install -y ffmpeg")
|
| 45 |
-
|
| 46 |
-
#result = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True)
|
| 47 |
-
#print(result.stdout)
|
| 48 |
-
|
| 49 |
-
#input_file = "input.mp4"
|
| 50 |
-
#output_file = "output.mp3"
|
| 51 |
-
#subprocess.run(["ffmpeg", "-i", input_file, output_file])
|
| 52 |
-
|
| 53 |
-
## create a new FASTAPI app instance
|
| 54 |
-
app=FastAPI()
|
| 55 |
-
|
| 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
|
| 70 |
-
output=pipe(text)
|
| 71 |
-
|
| 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(self,mp4_path):
|
| 77 |
-
#path_to_check = r"C:\Program Files\ffmpeg\bin"
|
| 78 |
-
#if path_to_check in os.environ["PATH"]:
|
| 79 |
-
#print(f"{path_to_check} is in the PATH")
|
| 80 |
-
#else:
|
| 81 |
-
#print(f"{path_to_check} is NOT in the PATH")
|
| 82 |
-
#os.environ["PATH"] += os.pathsep + path_to_check
|
| 83 |
-
ffmpeg_version = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True)
|
| 84 |
-
print(ffmpeg_version.stdout)
|
| 85 |
-
# Assuming the mp4 file has a transcript available
|
| 86 |
-
# This is a placeholder function. You would need to implement actual transcript extraction from mp4.
|
| 87 |
-
# For now, we will return a dummy transcript.
|
| 88 |
-
if os.path.exists(mp4_path):
|
| 89 |
-
print("File exists!: ", mp4_path)
|
| 90 |
-
# construct mp3 path
|
| 91 |
-
# Replace the .mp4 extension with .mp3
|
| 92 |
-
# Get the file name without extension
|
| 93 |
-
base_name = os.path.splitext(mp4_path)[0]
|
| 94 |
-
mp3_path = f"{base_name}.mp3" # Append .mp3 extension
|
| 95 |
-
#construct wav file path
|
| 96 |
-
wav_path = f"{base_name}.wav" # Append .mp3 extension
|
| 97 |
-
# construct the file to store the transcript
|
| 98 |
-
transcript_path = f"{base_name}_transcript.txt"
|
| 99 |
-
video = VideoFileClip(mp4_path)
|
| 100 |
-
video.audio.write_audiofile(mp3_path)
|
| 101 |
-
print("Conversion to mp3 complete!")
|
| 102 |
-
audio = AudioFileClip(mp3_path)
|
| 103 |
-
audio.write_audiofile(wav_path)
|
| 104 |
-
print("Conversion to wav complete!")
|
| 105 |
-
# Initialize the Whisper model (small model is a good balance for speed and accuracy)
|
| 106 |
-
print("Transcribing audio...")
|
| 107 |
-
model = whisper.load_model("tiny.en") # Options: "tiny", "base", "small", "medium", "large"
|
| 108 |
-
# Transcribe the audio
|
| 109 |
-
|
| 110 |
-
result = model.transcribe(wav_path)
|
| 111 |
-
print("Transcription completed!")
|
| 112 |
-
meet_data=result["text"]
|
| 113 |
-
with open(transcript_path, "w") as file:
|
| 114 |
-
file.write(meet_data)
|
| 115 |
-
print("Txt written to file: ", transcript_path)
|
| 116 |
-
return meet_data
|
| 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(self,text, lang='en'):
|
| 123 |
-
OpenAI.api_key = os.getenv("OPENAI_API_KEY")
|
| 124 |
-
client = OpenAI(api_key=OpenAI.api_key)
|
| 125 |
-
|
| 126 |
-
prompt = f"""
|
| 127 |
-
The following text is in its original language. Provide the output in this language: {lang}.
|
| 128 |
-
Format the output as follows:
|
| 129 |
-
|
| 130 |
-
Summary:
|
| 131 |
-
short summary of the video
|
| 132 |
-
|
| 133 |
-
Key Takeaways:
|
| 134 |
-
succinct bullet point list of key takeaways
|
| 135 |
-
|
| 136 |
-
Detailed important points:
|
| 137 |
-
succinct bullet point list of feature details
|
| 138 |
-
|
| 139 |
-
input text: {text}
|
| 140 |
-
"""
|
| 141 |
-
|
| 142 |
-
response = client.chat.completions.create(
|
| 143 |
-
messages=[{"role": "user", "content": prompt}],
|
| 144 |
-
model="gpt-4o",
|
| 145 |
-
temperature=0,
|
| 146 |
-
#model="gpt-4-turbo", # better performance, slower inference
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
summary_text = response.to_dict()['choices'][0]['message']['content']
|
| 150 |
-
return summary_text
|
|
|
|
| 1 |
+
# ########## Overview ################################################################
|
| 2 |
+
# The application is divided into three main components:
|
| 3 |
+
# Extracts metadata, transcripts, and thumbnails from YouTube videos.
|
| 4 |
+
# Summarizes the extracted transcripts using the OpenAI API.
|
| 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
|
| 36 |
+
from moviepy.video.io.VideoFileClip import VideoFileClip
|
| 37 |
+
from moviepy.audio.io.AudioFileClip import AudioFileClip
|
| 38 |
+
import whisper
|
| 39 |
+
from openai import OpenAI
|
| 40 |
+
from fastapi import FastAPI
|
| 41 |
+
from transformers import pipeline
|
| 42 |
+
|
| 43 |
+
#import os
|
| 44 |
+
#os.system("apt-get update && apt-get install -y ffmpeg")
|
| 45 |
+
|
| 46 |
+
#result = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True)
|
| 47 |
+
#print(result.stdout)
|
| 48 |
+
|
| 49 |
+
#input_file = "input.mp4"
|
| 50 |
+
#output_file = "output.mp3"
|
| 51 |
+
#subprocess.run(["ffmpeg", "-i", input_file, output_file])
|
| 52 |
+
|
| 53 |
+
## create a new FASTAPI app instance
|
| 54 |
+
app=FastAPI()
|
| 55 |
+
|
| 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
|
| 70 |
+
output=pipe(text)
|
| 71 |
+
|
| 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(self,mp4_path):
|
| 77 |
+
#path_to_check = r"C:\Program Files\ffmpeg\bin"
|
| 78 |
+
#if path_to_check in os.environ["PATH"]:
|
| 79 |
+
#print(f"{path_to_check} is in the PATH")
|
| 80 |
+
#else:
|
| 81 |
+
#print(f"{path_to_check} is NOT in the PATH")
|
| 82 |
+
#os.environ["PATH"] += os.pathsep + path_to_check
|
| 83 |
+
ffmpeg_version = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True)
|
| 84 |
+
print(ffmpeg_version.stdout)
|
| 85 |
+
# Assuming the mp4 file has a transcript available
|
| 86 |
+
# This is a placeholder function. You would need to implement actual transcript extraction from mp4.
|
| 87 |
+
# For now, we will return a dummy transcript.
|
| 88 |
+
if os.path.exists(mp4_path):
|
| 89 |
+
print("File exists!: ", mp4_path)
|
| 90 |
+
# construct mp3 path
|
| 91 |
+
# Replace the .mp4 extension with .mp3
|
| 92 |
+
# Get the file name without extension
|
| 93 |
+
base_name = os.path.splitext(mp4_path)[0]
|
| 94 |
+
mp3_path = f"{base_name}.mp3" # Append .mp3 extension
|
| 95 |
+
#construct wav file path
|
| 96 |
+
wav_path = f"{base_name}.wav" # Append .mp3 extension
|
| 97 |
+
# construct the file to store the transcript
|
| 98 |
+
transcript_path = f"{base_name}_transcript.txt"
|
| 99 |
+
video = VideoFileClip(mp4_path)
|
| 100 |
+
video.audio.write_audiofile(mp3_path)
|
| 101 |
+
print("Conversion to mp3 complete!")
|
| 102 |
+
audio = AudioFileClip(mp3_path)
|
| 103 |
+
audio.write_audiofile(wav_path)
|
| 104 |
+
print("Conversion to wav complete!")
|
| 105 |
+
# Initialize the Whisper model (small model is a good balance for speed and accuracy)
|
| 106 |
+
print("Transcribing audio...")
|
| 107 |
+
model = whisper.load_model("tiny.en") # Options: "tiny", "base", "small", "medium", "large"
|
| 108 |
+
# Transcribe the audio
|
| 109 |
+
|
| 110 |
+
result = model.transcribe(wav_path)
|
| 111 |
+
print("Transcription completed!")
|
| 112 |
+
meet_data=result["text"]
|
| 113 |
+
with open(transcript_path, "w") as file:
|
| 114 |
+
file.write(meet_data)
|
| 115 |
+
print("Txt written to file: ", transcript_path)
|
| 116 |
+
return meet_data
|
| 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(self,text, lang='en'):
|
| 123 |
+
OpenAI.api_key = os.getenv("OPENAI_API_KEY")
|
| 124 |
+
client = OpenAI(api_key=OpenAI.api_key)
|
| 125 |
+
|
| 126 |
+
prompt = f"""
|
| 127 |
+
The following text is in its original language. Provide the output in this language: {lang}.
|
| 128 |
+
Format the output as follows:
|
| 129 |
+
|
| 130 |
+
Summary:
|
| 131 |
+
short summary of the video
|
| 132 |
+
|
| 133 |
+
Key Takeaways:
|
| 134 |
+
succinct bullet point list of key takeaways
|
| 135 |
+
|
| 136 |
+
Detailed important points:
|
| 137 |
+
succinct bullet point list of feature details
|
| 138 |
+
|
| 139 |
+
input text: {text}
|
| 140 |
+
"""
|
| 141 |
+
|
| 142 |
+
response = client.chat.completions.create(
|
| 143 |
+
messages=[{"role": "user", "content": prompt}],
|
| 144 |
+
model="gpt-4o",
|
| 145 |
+
temperature=0,
|
| 146 |
+
#model="gpt-4-turbo", # better performance, slower inference
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
summary_text = response.to_dict()['choices'][0]['message']['content']
|
| 150 |
+
return summary_text
|