MingGatsby commited on
Commit
e7d4e4b
·
verified ·
1 Parent(s): d8fe757

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from moviepy.editor import VideoFileClip
3
+ import cv2
4
+ import base64
5
+ from openai import OpenAI
6
+ import os
7
+
8
+ # 參考: https://cookbook.openai.com/examples/gpt4o/introduction_to_gpt4o
9
+ def process_video(video_path, seconds_per_frame=2):
10
+ base64Frames = []
11
+ base_video_path, _ = os.path.splitext(video_path)
12
+ video = cv2.VideoCapture(video_path)
13
+ total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
14
+ fps = video.get(cv2.CAP_PROP_FPS)
15
+ frames_to_skip = int(fps * seconds_per_frame)
16
+ curr_frame = 0
17
+
18
+ while curr_frame < total_frames - 1:
19
+ video.set(cv2.CAP_PROP_POS_FRAMES, curr_frame)
20
+ success, frame = video.read()
21
+ if not success:
22
+ break
23
+ _, buffer = cv2.imencode(".jpg", frame)
24
+ base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
25
+ curr_frame += frames_to_skip
26
+ video.release()
27
+
28
+ audio_path = f"{base_video_path}.mp3"
29
+ clip = VideoFileClip(video_path)
30
+ clip.audio.write_audiofile(audio_path, bitrate="32k")
31
+ clip.audio.close()
32
+ clip.close()
33
+
34
+ return base64Frames, audio_path
35
+
36
+
37
+ def summarize_video(api_key, file_path):
38
+ client = OpenAI(api_key=api_key)
39
+
40
+ # 抽取幀和音頻(每0.5秒一幀)
41
+ base64Frames, audio_path = process_video(file_path, seconds_per_frame=0.5)
42
+
43
+ # 使用Whisper進行音頻轉錄
44
+ transcription = client.audio.transcriptions.create(
45
+ model="whisper-1", file=open(audio_path, "rb")
46
+ )
47
+
48
+ # 使用GPT-4o生成摘要
49
+ response = client.chat.completions.create(
50
+ model="gpt-4o",
51
+ messages=[
52
+ {
53
+ "role": "system",
54
+ "content": """您是一名優秀的摘要專家,請根據提供的影片和其轉錄內容生成Markdown格式的摘要。""",
55
+ },
56
+ {
57
+ "role": "user",
58
+ "content": [
59
+ "以下是從影片中提取的幀畫面",
60
+ *map(
61
+ lambda x: {
62
+ "type": "image_url",
63
+ "image_url": {
64
+ "url": f"data:image/jpg;base64,{x}",
65
+ "detail": "low",
66
+ },
67
+ },
68
+ base64Frames,
69
+ ),
70
+ {
71
+ "type": "text",
72
+ "text": f"這是影片的轉錄內容: {transcription.text}",
73
+ },
74
+ ],
75
+ },
76
+ ],
77
+ temperature=0,
78
+ )
79
+
80
+ return response.choices[0].message.content
81
+
82
+ demo = gr.Interface(
83
+ fn=summarize_video,
84
+ inputs=[gr.Textbox(label="OpenAI API Key"), gr.File(label="上傳影片 (mp4)")],
85
+ outputs="markdown",
86
+ title="影片摘要生成器",
87
+ description="上傳影片,將生成影片的摘要。",
88
+ )
89
+
90
+ if __name__ == "__main__":
91
+ demo.launch()