Spaces:
Runtime error
Runtime error
add gradio_app
Browse files- requirements.txt +3 -1
- src/obs_eval_gradio.py +88 -0
requirements.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1 |
openai
|
2 |
opencv-python
|
3 |
-
python-dotenv
|
|
|
|
|
|
1 |
openai
|
2 |
opencv-python
|
3 |
+
python-dotenv
|
4 |
+
gradio
|
5 |
+
opencv-python-headless
|
src/obs_eval_gradio.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import base64
|
4 |
+
import openai
|
5 |
+
import tempfile
|
6 |
+
|
7 |
+
def process_video(video_file, api_key):
|
8 |
+
# Set the OpenAI API key
|
9 |
+
openai.api_key = api_key
|
10 |
+
|
11 |
+
# Read and process the video file
|
12 |
+
video = cv2.VideoCapture(video_file.name)
|
13 |
+
base64Frames = []
|
14 |
+
while video.isOpened():
|
15 |
+
success, frame = video.read()
|
16 |
+
if not success:
|
17 |
+
break
|
18 |
+
_, buffer = cv2.imencode(".jpg", frame)
|
19 |
+
base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
|
20 |
+
video.release()
|
21 |
+
|
22 |
+
# Instruction for narration generation
|
23 |
+
INSTRUCTION = " ".join([
|
24 |
+
"These are frames of a video.",
|
25 |
+
"Create a short voiceover script in the style of a super excited Brazilian sports narrator who is narrating his favorite match.",
|
26 |
+
"He is a big fan of Messi, the player who scores in this clip.",
|
27 |
+
"Use caps and exclamation marks where needed to communicate excitement.",
|
28 |
+
"Only include the narration, your output must be in English.",
|
29 |
+
"When the ball goes into the net, you must scream GOL either once or multiple times."
|
30 |
+
])
|
31 |
+
|
32 |
+
PROMPT_MESSAGES = [
|
33 |
+
{
|
34 |
+
"role": "user",
|
35 |
+
"content": [
|
36 |
+
INSTRUCTION,
|
37 |
+
*map(lambda x: {"image": x, "resize": 768}, base64Frames[0::10]),
|
38 |
+
],
|
39 |
+
},
|
40 |
+
]
|
41 |
+
|
42 |
+
try:
|
43 |
+
result = openai.ChatCompletion.create(
|
44 |
+
model="gpt-4-vision-preview",
|
45 |
+
messages=PROMPT_MESSAGES,
|
46 |
+
api_key=openai.api_key,
|
47 |
+
headers={"Openai-Version": "2020-11-07"},
|
48 |
+
max_tokens=500,
|
49 |
+
)
|
50 |
+
return result.choices[0].message.content
|
51 |
+
except Exception as e:
|
52 |
+
return f"Error: {e}"
|
53 |
+
|
54 |
+
# Define the Gradio app
|
55 |
+
def main():
|
56 |
+
with gr.Blocks() as app:
|
57 |
+
gr.Markdown("## Video Narration Generator")
|
58 |
+
with gr.Row():
|
59 |
+
with gr.Column():
|
60 |
+
api_key_input = gr.Textbox(label="Enter your OpenAI API Key")
|
61 |
+
video_upload = gr.File(label="Upload your video")
|
62 |
+
submit_button = gr.Button("Generate Script", elem_id="submit_button")
|
63 |
+
with gr.Column():
|
64 |
+
output_box = gr.Textbox(label="Generated Script", lines=10, interactive=False)
|
65 |
+
|
66 |
+
submit_button.click(fn=process_video, inputs=[video_upload, api_key_input], outputs=output_box)
|
67 |
+
|
68 |
+
app.launch()
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
main()
|
72 |
+
|
73 |
+
# # Define the Gradio app
|
74 |
+
# def main():
|
75 |
+
# with gr.Blocks() as app:
|
76 |
+
# gr.Markdown("## Video Narration Generator")
|
77 |
+
# with gr.Row():
|
78 |
+
# video_upload = gr.File(label="Upload your video")
|
79 |
+
# api_key_input = gr.Textbox(label="Enter your OpenAI API Key")
|
80 |
+
# submit_button = gr.Button("Generate Script")
|
81 |
+
# output_box = gr.Textbox(label="Generated Script", lines=10, interactive=False)
|
82 |
+
|
83 |
+
# submit_button.click(fn=process_video, inputs=[video_upload, api_key_input], outputs=output_box)
|
84 |
+
|
85 |
+
# app.launch()
|
86 |
+
|
87 |
+
# if __name__ == "__main__":
|
88 |
+
# main()
|