awacke1 commited on
Commit
a117011
·
verified ·
1 Parent(s): 2eae422

Delete backup-app1-app.py

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