MingGatsby
commited on
Commit
•
1115e59
1
Parent(s):
809a7cb
Delete app.py
Browse files
app.py
DELETED
@@ -1,117 +0,0 @@
|
|
1 |
-
import warnings
|
2 |
-
|
3 |
-
# 過濾 SyntaxWarning 警告
|
4 |
-
warnings.filterwarnings("ignore", category=SyntaxWarning)
|
5 |
-
|
6 |
-
import gradio as gr
|
7 |
-
from moviepy.editor import VideoFileClip
|
8 |
-
import cv2
|
9 |
-
import base64
|
10 |
-
from openai import OpenAI
|
11 |
-
import os
|
12 |
-
|
13 |
-
def extract_frames(video_path, seconds_per_frame):
|
14 |
-
base64Frames = []
|
15 |
-
video = cv2.VideoCapture(video_path)
|
16 |
-
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
17 |
-
fps = video.get(cv2.CAP_PROP_FPS)
|
18 |
-
frames_to_skip = int(fps * seconds_per_frame)
|
19 |
-
curr_frame = 0
|
20 |
-
|
21 |
-
while curr_frame < total_frames - 1:
|
22 |
-
video.set(cv2.CAP_PROP_POS_FRAMES, curr_frame)
|
23 |
-
success, frame = video.read()
|
24 |
-
if not success:
|
25 |
-
break
|
26 |
-
_, buffer = cv2.imencode(".jpg", frame)
|
27 |
-
base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
|
28 |
-
curr_frame += frames_to_skip
|
29 |
-
video.release()
|
30 |
-
return base64Frames
|
31 |
-
|
32 |
-
def extract_audio(video_path):
|
33 |
-
base_video_path, _ = os.path.splitext(video_path)
|
34 |
-
audio_path = f"{base_video_path}.mp3"
|
35 |
-
clip = VideoFileClip(video_path)
|
36 |
-
clip.audio.write_audiofile(filename=audio_path, codec="mp3", bitrate="32k")
|
37 |
-
# Remove the line that closes the audio
|
38 |
-
clip.close()
|
39 |
-
return audio_path
|
40 |
-
|
41 |
-
def process_video(video_path, seconds_per_frame=2):
|
42 |
-
base64Frames = extract_frames(video_path, seconds_per_frame)
|
43 |
-
audio_path = extract_audio(video_path)
|
44 |
-
return base64Frames, audio_path
|
45 |
-
|
46 |
-
def get_summary_prompt(summary_detail):
|
47 |
-
if summary_detail == "簡短":
|
48 |
-
return "生成一個簡短的摘要,不超過50字。"
|
49 |
-
elif summary_detail == "中等":
|
50 |
-
return "生成一個中等長度的摘要,概述影片的主要內容。"
|
51 |
-
else: # 詳細
|
52 |
-
return "生成一個詳細的摘要,包括影片的主要內容和關鍵細節。"
|
53 |
-
|
54 |
-
def summarize_video(file_path, summary_detail):
|
55 |
-
api_key = "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"
|
56 |
-
# api_key = os.getenv("OPENAI_API_KEY")
|
57 |
-
client = OpenAI(api_key=api_key)
|
58 |
-
|
59 |
-
# 抽取幀和音頻(每0.5秒一幀)
|
60 |
-
base64Frames, audio_path = process_video(file_path, seconds_per_frame=int(0.5))
|
61 |
-
|
62 |
-
# 使用Whisper進行音頻轉錄
|
63 |
-
transcription = client.audio.transcriptions.create(
|
64 |
-
model="whisper-1", file=open(audio_path, "rb")
|
65 |
-
)
|
66 |
-
|
67 |
-
# 根據用戶選擇的詳細程度設置提示
|
68 |
-
summary_prompt = get_summary_prompt(summary_detail)
|
69 |
-
|
70 |
-
# 使用GPT-4o生成摘要
|
71 |
-
response = client.chat.completions.create(
|
72 |
-
model="gpt-4o",
|
73 |
-
messages=[
|
74 |
-
{
|
75 |
-
"role": "system",
|
76 |
-
"content": f"""您是一名優秀的摘要專家,請根據提供的影片和其轉錄內容生成Markdown格式的摘要。{summary_prompt}""",
|
77 |
-
},
|
78 |
-
{
|
79 |
-
"role": "user",
|
80 |
-
"content": [
|
81 |
-
"以下是從影片中提取的幀畫面",
|
82 |
-
*map(
|
83 |
-
lambda x: {
|
84 |
-
"type": "image_url",
|
85 |
-
"image_url": {
|
86 |
-
"url": f"data:image/jpg;base64,{x}",
|
87 |
-
"detail": "low",
|
88 |
-
},
|
89 |
-
},
|
90 |
-
base64Frames,
|
91 |
-
),
|
92 |
-
{
|
93 |
-
"type": "text",
|
94 |
-
"text": f"這是影片的轉錄內容: {transcription.text}",
|
95 |
-
},
|
96 |
-
],
|
97 |
-
},
|
98 |
-
],
|
99 |
-
temperature=0,
|
100 |
-
)
|
101 |
-
|
102 |
-
return response.choices[0].message.content
|
103 |
-
|
104 |
-
demo = gr.Interface(
|
105 |
-
fn=summarize_video,
|
106 |
-
inputs=[
|
107 |
-
gr.File(label="上傳影片 (mp4)"),
|
108 |
-
gr.Radio(["簡短", "中等", "詳細"], label="摘要詳細程度", value="中等")
|
109 |
-
],
|
110 |
-
outputs="markdown",
|
111 |
-
title="影片摘要生成器",
|
112 |
-
description="上傳影片,將生成影片的摘要。",
|
113 |
-
flagging_dir="" # 這會移除 Flag 功能
|
114 |
-
)
|
115 |
-
|
116 |
-
if __name__ == "__main__":
|
117 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|