共计 3338 个字符,预计需要花费 9 分钟才能阅读完成。
本文提供了应用 Streamlit 和 OpenAI 创立的视频摘要应用程序的概述。该程序为视频的每个片段创立简洁的摘要,并总结视频的残缺内容。
要运行应用程序,须要装置以下依赖项:
- Python(3.7 或更高版本)
- Streamlit
- OpenAI API 密钥
- llama_index
- youtube_transcript_api
- html2image
- langchain
搭建环境
首先,须要设置咱们的开发环境,能够应用以下代码片段将 API 密钥设置为环境变量:
import os | |
os.environ["OPENAI_API_KEY"] = '{your_Api_Key}' |
而后导入所有的包
from llama_index import StorageContext, load_index_from_storage | |
from llama_index import VectorStoreIndex | |
import streamlit as st | |
from llama_index import download_loader | |
from llama_index import GPTVectorStoreIndex | |
from llama_index import LLMPredictor, GPTVectorStoreIndex, PromptHelper, ServiceContext | |
from langchain import OpenAI | |
from langchain.chat_models import ChatOpenAI | |
from youtube_transcript_api import YouTubeTranscriptApi | |
from youtube_transcript_api.formatters import JSONFormatter | |
import json | |
import datetime | |
from html2image import Html2Image |
解决用户输出和 YouTube 视频检索
以下代码是 Streamlit 的按钮和事件:咱们提醒用户输出一个 YouTube 视频链接。应用 st.text_input 捕捉输出,并将其存储在 youtube_link 变量中。按钮的名字为“Summarize!”,当单击该按钮时将触发咱们的处理过程。上面是相干的代码片段:
youtube_link = st.text_input("Youtube link:") | |
st.button("Summarize!", on_click=send_click) |
获取视频文本和预处理
应用 YouTubeTranscriptApi 能够取得视频文本。而后将转录本格式化为 JSON 并保留到文件中。而后再应用 Html2Image 库捕捉 YouTube 视频的屏幕截图:
srt = YouTubeTranscriptApi.get_transcript(st.session_state.video_id, languages=['en']) | |
formatter = JSONFormatter() | |
json_formatted = formatter.format_transcript(srt) | |
with open(transcript_file, 'w') as f: | |
f.write(json_formatted) | |
hti = Html2Image() | |
hti.screenshot(url=f"https://www.youtube.com/watch?v={st.session_state.video_id}", save_as=youtube_img) |
建设索引和查询语言模型
上面就是对下面获取文本的解决,应用 llama_index 库中的 VectorStoreIndex 类创立索引。索引是依据视频文本构建的,另外还定义了 LLMPredictor 和 ServiceContext 来解决语言模型交互。上面是相干的代码片段:
documents = loader.load_data() | |
# define LLM | |
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="gpt-3.5-turbo", max_tokens=500)) | |
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor) | |
# Create and load the index | |
index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context) | |
index.storage_context.persist() | |
# Load the index from storage | |
storage_context = StorageContext.from_defaults(persist_dir=index_file) | |
index = load_index_from_storage(storage_context, service_context=service_context) | |
# Create a query engine for the index | |
query_engine = index.as_query_engine() |
生成视频摘要
这一步遍历视频文本并为视频的每个局部生成摘要。通过应用 query_engine 从视频局部结构的提醒来查询语言模型。生成的摘要存储在 section_response 变量中:
section_response = '' | |
for d in transcript: | |
# ... | |
if d["start"] <= (section_start_s + 300) and transcript.index(d) != len(transcript) - 1: | |
section_texts += '' + d["text"] | |
else: | |
end_text = d["text"] | |
prompt = f"summarize this article from \"{start_text}\"to \"{end_text}\", limited to 100 words, start with \"This section of video\"" | |
response = query_engine.query(prompt) | |
start_time = str(datetime.timedelta(seconds=section_start_s)) | |
end_time = str(datetime.timedelta(seconds=int(d['start']))) | |
section_start_s += 300 | |
start_text = d["text"] | |
section_texts = ''section_response += f"**{start_time} - {end_time}:**\n\r{response}\n\r" |
生成最终总结
在解决完所有视频片段后,,生成整个视频的最终摘要。生成的摘要存储在响应变量中:
prompt = "Summarize this article of a video, start with \"This Video\", the article is:" + section_response | |
response = query_engine.query(prompt) |
显示后果
最初,咱们应用 streamlit 显示生成的摘要和局部详细信息:
st.subheader("Summary:") | |
st.success(response, icon="🤖") | |
with st.expander("Section Details:"): | |
st.write(section_response) | |
st.session_state.video_id = '' | |
st.stop() |
总结
本文演示了如何创立一个基于 python 的视频摘要程序。应用 youtube_transcript_apto 间接获取视频的文本,并通过 OpenAI 的语言模型来提供摘要。
作者没有个残缺代码地址,所以有趣味请与原文作者分割:
https://avoid.overfit.cn/post/a2ca634c772d4bcead5e2b72f3042b1e
作者:Abhijeetas