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