awacke1 commited on
Commit
878ab12
1 Parent(s): f9040a4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from openai import OpenAI
3
+ import os
4
+ import base64
5
+ import cv2
6
+ from moviepy.editor import VideoFileClip
7
+
8
+ # Set the API key and model name
9
+ MODEL = "gpt-4o"
10
+ client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if not set as an env var>"))
11
+
12
+ def process_text():
13
+ text_input = st.text_input("Enter your text:")
14
+ if text_input:
15
+ completion = client.chat.completions.create(
16
+ model=MODEL,
17
+ messages=[
18
+ {"role": "system", "content": "You are a helpful assistant. Help me with my math homework!"},
19
+ {"role": "user", "content": f"Hello! Could you solve {text_input}?"}
20
+ ]
21
+ )
22
+ st.write("Assistant: " + completion.choices[0].message.content)
23
+
24
+ def process_image(image_input):
25
+ if image_input:
26
+ base64_image = base64.b64encode(image_input.read()).decode("utf-8")
27
+ response = client.chat.completions.create(
28
+ model=MODEL,
29
+ messages=[
30
+ {"role": "system", "content": "You are a helpful assistant that responds in Markdown. Help me with my math homework!"},
31
+ {"role": "user", "content": [
32
+ {"type": "text", "text": "What's the area of the triangle?"},
33
+ {"type": "image_url", "image_url": {
34
+ "url": f"data:image/png;base64,{base64_image}"}
35
+ }
36
+ ]}
37
+ ],
38
+ temperature=0.0,
39
+ )
40
+ st.markdown(response.choices[0].message.content)
41
+
42
+ def process_audio(audio_input):
43
+ if audio_input:
44
+ transcription = client.audio.transcriptions.create(
45
+ model="whisper-1",
46
+ file=audio_input,
47
+ )
48
+ response = client.chat.completions.create(
49
+ model=MODEL,
50
+ messages=[
51
+ {"role": "system", "content": "You are generating a transcript summary. Create a summary of the provided transcription. Respond in Markdown."},
52
+ {"role": "user", "content": [
53
+ {"type": "text", "text": f"The audio transcription is: {transcription.text}"}
54
+ ]},
55
+ ],
56
+ temperature=0,
57
+ )
58
+ st.markdown(response.choices[0].message.content)
59
+
60
+ def process_video(video_input):
61
+ if video_input:
62
+ base64Frames, audio_path = process_video_frames(video_input)
63
+ transcription = client.audio.transcriptions.create(
64
+ model="whisper-1",
65
+ file=open(audio_path, "rb"),
66
+ )
67
+ response = client.chat.completions.create(
68
+ model=MODEL,
69
+ messages=[
70
+ {"role": "system", "content": "You are generating a video summary. Create a summary of the provided video and its transcript. Respond in Markdown"},
71
+ {"role": "user", "content": [
72
+ "These are the frames from the video.",
73
+ *map(lambda x: {"type": "image_url",
74
+ "image_url": {"url": f'data:image/jpg;base64,{x}', "detail": "low"}}, base64Frames),
75
+ {"type": "text", "text": f"The audio transcription is: {transcription.text}"}
76
+ ]},
77
+ ],
78
+ temperature=0,
79
+ )
80
+ st.markdown(response.choices[0].message.content)
81
+
82
+ def process_video_frames(video_path, seconds_per_frame=2):
83
+ base64Frames = []
84
+ base_video_path, _ = os.path.splitext(video_path.name)
85
+ video = cv2.VideoCapture(video_path.name)
86
+ total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
87
+ fps = video.get(cv2.CAP_PROP_FPS)
88
+ frames_to_skip = int(fps * seconds_per_frame)
89
+ curr_frame = 0
90
+ while curr_frame < total_frames - 1:
91
+ video.set(cv2.CAP_PROP_POS_FRAMES, curr_frame)
92
+ success, frame = video.read()
93
+ if not success:
94
+ break
95
+ _, buffer = cv2.imencode(".jpg", frame)
96
+ base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
97
+ curr_frame += frames_to_skip
98
+ video.release()
99
+ audio_path = f"{base_video_path}.mp3"
100
+ clip = VideoFileClip(video_path.name)
101
+ clip.audio.write_audiofile(audio_path, bitrate="32k")
102
+ clip.audio.close()
103
+ clip.close()
104
+ return base64Frames, audio_path
105
+
106
+ def main():
107
+ st.title("Omni Demo")
108
+ option = st.selectbox("Select an option", ("Text", "Image", "Audio", "Video"))
109
+ if option == "Text":
110
+ process_text()
111
+ elif option == "Image":
112
+ image_input = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
113
+ process_image(image_input)
114
+ elif option == "Audio":
115
+ audio_input = st.file_uploader("Upload an audio file", type=["mp3", "wav"])
116
+ process_audio(audio_input)
117
+ elif option == "Video":
118
+ video_input = st.file_uploader("Upload a video file", type=["mp4"])
119
+ process_video(video_input)
120
+
121
+ if __name__ == "__main__":
122
+ main()