Spaces:
Sleeping
Sleeping
Uploaded files
Browse files- Dockerfile +26 -0
- app.py +150 -0
- requirements.txt +151 -0
Dockerfile
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# For more information, please refer to https://aka.ms/vscode-docker-python
|
| 2 |
+
FROM python:3-slim
|
| 3 |
+
|
| 4 |
+
EXPOSE 8000
|
| 5 |
+
|
| 6 |
+
# Keeps Python from generating .pyc files in the container
|
| 7 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 8 |
+
|
| 9 |
+
# Turns off buffering for easier container logging
|
| 10 |
+
ENV PYTHONUNBUFFERED=1
|
| 11 |
+
WORKDIR /app
|
| 12 |
+
# Install pip requirements
|
| 13 |
+
COPY requirements.txt ./app/requirements.txt
|
| 14 |
+
#COPY summarize_mp4_video.py /app/app.py
|
| 15 |
+
RUN python -m pip install -r requirements.txt
|
| 16 |
+
RUN apt-get update && apt-get install -y ffmpeg
|
| 17 |
+
|
| 18 |
+
# COPY . /app
|
| 19 |
+
|
| 20 |
+
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
|
| 21 |
+
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
|
| 22 |
+
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
|
| 23 |
+
USER appuser
|
| 24 |
+
|
| 25 |
+
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
|
| 26 |
+
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "-k", "uvicorn.workers.UvicornWorker", "setup:app"]
|
app.py
ADDED
|
@@ -0,0 +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
|
requirements.txt
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# This file is autogenerated by pip-compile with Python 3.13
|
| 3 |
+
# by the following command:
|
| 4 |
+
#
|
| 5 |
+
# pip-compile --output-file=requirements.txt requirements.in
|
| 6 |
+
#
|
| 7 |
+
annotated-types==0.7.0
|
| 8 |
+
# via pydantic
|
| 9 |
+
anyio==4.8.0
|
| 10 |
+
# via
|
| 11 |
+
# httpx
|
| 12 |
+
# openai
|
| 13 |
+
# starlette
|
| 14 |
+
asttokens==3.0.0
|
| 15 |
+
# via stack-data
|
| 16 |
+
certifi==2025.1.31
|
| 17 |
+
# via
|
| 18 |
+
# httpcore
|
| 19 |
+
# httpx
|
| 20 |
+
# requests
|
| 21 |
+
charset-normalizer==3.4.1
|
| 22 |
+
# via requests
|
| 23 |
+
colorama==0.4.6
|
| 24 |
+
# via
|
| 25 |
+
# ipython
|
| 26 |
+
# tqdm
|
| 27 |
+
decorator==5.1.1
|
| 28 |
+
# via
|
| 29 |
+
# ipython
|
| 30 |
+
# moviepy
|
| 31 |
+
distro==1.9.0
|
| 32 |
+
# via openai
|
| 33 |
+
executing==2.2.0
|
| 34 |
+
# via stack-data
|
| 35 |
+
fastapi==0.115.8
|
| 36 |
+
# via -r requirements.in
|
| 37 |
+
ffmpeg-python==0.2.0
|
| 38 |
+
# via -r requirements.in
|
| 39 |
+
filelock==3.17.0
|
| 40 |
+
# via
|
| 41 |
+
# huggingface-hub
|
| 42 |
+
# transformers
|
| 43 |
+
fsspec==2025.2.0
|
| 44 |
+
# via huggingface-hub
|
| 45 |
+
future==1.0.0
|
| 46 |
+
# via ffmpeg-python
|
| 47 |
+
h11==0.14.0
|
| 48 |
+
# via httpcore
|
| 49 |
+
httpcore==1.0.7
|
| 50 |
+
# via httpx
|
| 51 |
+
httpx==0.28.1
|
| 52 |
+
# via openai
|
| 53 |
+
huggingface-hub==0.28.1
|
| 54 |
+
# via
|
| 55 |
+
# tokenizers
|
| 56 |
+
# transformers
|
| 57 |
+
idna==3.10
|
| 58 |
+
# via
|
| 59 |
+
# anyio
|
| 60 |
+
# httpx
|
| 61 |
+
# requests
|
| 62 |
+
imageio==2.37.0
|
| 63 |
+
# via moviepy
|
| 64 |
+
ipython==8.32.0
|
| 65 |
+
# via -r requirements.in
|
| 66 |
+
jedi==0.19.2
|
| 67 |
+
# via ipython
|
| 68 |
+
jiter==0.8.2
|
| 69 |
+
# via openai
|
| 70 |
+
matplotlib-inline==0.1.7
|
| 71 |
+
# via ipython
|
| 72 |
+
moviepy==0.2.3.1
|
| 73 |
+
# via -r requirements.in
|
| 74 |
+
numpy==1.26.4
|
| 75 |
+
# via
|
| 76 |
+
# imageio
|
| 77 |
+
# moviepy
|
| 78 |
+
# transformers
|
| 79 |
+
openai==1.63.0
|
| 80 |
+
# via -r requirements.in
|
| 81 |
+
packaging==24.2
|
| 82 |
+
# via
|
| 83 |
+
# huggingface-hub
|
| 84 |
+
# transformers
|
| 85 |
+
parso==0.8.4
|
| 86 |
+
# via jedi
|
| 87 |
+
pillow==11.1.0
|
| 88 |
+
# via imageio
|
| 89 |
+
prompt-toolkit==3.0.50
|
| 90 |
+
# via ipython
|
| 91 |
+
pure-eval==0.2.3
|
| 92 |
+
# via stack-data
|
| 93 |
+
pydantic==2.10.6
|
| 94 |
+
# via
|
| 95 |
+
# fastapi
|
| 96 |
+
# openai
|
| 97 |
+
pydantic-core==2.27.2
|
| 98 |
+
# via pydantic
|
| 99 |
+
pygments==2.19.1
|
| 100 |
+
# via ipython
|
| 101 |
+
python-dotenv==1.0.1
|
| 102 |
+
# via -r requirements.in
|
| 103 |
+
pyyaml==6.0.2
|
| 104 |
+
# via
|
| 105 |
+
# huggingface-hub
|
| 106 |
+
# transformers
|
| 107 |
+
regex==2024.11.6
|
| 108 |
+
# via transformers
|
| 109 |
+
requests==2.32.3
|
| 110 |
+
# via
|
| 111 |
+
# huggingface-hub
|
| 112 |
+
# transformers
|
| 113 |
+
safetensors==0.5.3
|
| 114 |
+
# via transformers
|
| 115 |
+
six==1.17.0
|
| 116 |
+
# via whisper
|
| 117 |
+
sniffio==1.3.1
|
| 118 |
+
# via
|
| 119 |
+
# anyio
|
| 120 |
+
# openai
|
| 121 |
+
stack-data==0.6.3
|
| 122 |
+
# via ipython
|
| 123 |
+
starlette==0.45.3
|
| 124 |
+
# via fastapi
|
| 125 |
+
tokenizers==0.21.0
|
| 126 |
+
# via transformers
|
| 127 |
+
tqdm==4.67.1
|
| 128 |
+
# via
|
| 129 |
+
# huggingface-hub
|
| 130 |
+
# moviepy
|
| 131 |
+
# openai
|
| 132 |
+
# transformers
|
| 133 |
+
traitlets==5.14.3
|
| 134 |
+
# via
|
| 135 |
+
# ipython
|
| 136 |
+
# matplotlib-inline
|
| 137 |
+
transformers==4.50.3
|
| 138 |
+
# via -r requirements.in
|
| 139 |
+
typing-extensions==4.12.2
|
| 140 |
+
# via
|
| 141 |
+
# fastapi
|
| 142 |
+
# huggingface-hub
|
| 143 |
+
# openai
|
| 144 |
+
# pydantic
|
| 145 |
+
# pydantic-core
|
| 146 |
+
urllib3==2.3.0
|
| 147 |
+
# via requests
|
| 148 |
+
wcwidth==0.2.13
|
| 149 |
+
# via prompt-toolkit
|
| 150 |
+
whisper==1.1.10
|
| 151 |
+
# via -r requirements.in
|