-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
132 lines (103 loc) · 4.4 KB
/
app.py
File metadata and controls
132 lines (103 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import tempfile
from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename
from dotenv import load_dotenv
from flask_cors import CORS
import logging
from itertools import cycle
import threading
from main import detection_in_video, detection_in_video_batched, transcribe_audio
app = Flask(__name__)
CORS(app, origins=['*'])
@app.route("/detect", methods=["POST"])
def detect():
if "video" not in request.files or "description" not in request.form:
return jsonify({"error": "Missing video or description"}), 400
video_file = request.files["video"]
description = request.form["description"]
if video_file.filename == "":
return jsonify({"error": "Empty filename"}), 400
try:
every_n_seconds = float(request.form.get("every_n_seconds", 2.0))
max_frames = int(request.form.get("max_frames", 20))
except ValueError:
return jsonify({"error": "Invalid number format for 'every_n_seconds' or 'max_frames'"}), 400
try:
# Securely save the uploaded video temporarily
if video_file.filename is None:
raise Exception("Video filename is none")
filename = secure_filename(video_file.filename)
# with key_lock:
# key = next(api_key_cycle)
with tempfile.TemporaryDirectory() as tmpdir:
filepath = os.path.join(tmpdir, filename)
video_file.save(filepath)
timestamps = detection_in_video(
video_path=filepath,
prompt_input=description,
method="detection",
every_n_seconds=every_n_seconds,
max_frames=max_frames,
model="meta-llama/llama-4-scout-17b-16e-instruct",
)
return jsonify({"timestamps": timestamps})
except Exception as e:
logger.exception(f"{str(e)}")
return jsonify({"error": str(e)}), 500
logger = logging.getLogger(__name__)
@app.route("/detect_batched", methods=["POST"])
def detect_batched():
if "video" not in request.files or "description" not in request.form:
return jsonify({"error": "Missing video or description"}), 400
video_file = request.files["video"]
description = request.form["description"]
if video_file.filename == "":
return jsonify({"error": "Empty filename"}), 400
try:
every_n_seconds = float(request.form.get("every_n_seconds", 2.0))
max_frames = int(request.form.get("max_frames", 20))
batch_size = int(request.form.get("batch_size", 4))
except ValueError:
return jsonify({"error": "Invalid number format for 'every_n_seconds' or 'max_frames' or 'batch_size'"}), 400
try:
# Securely save the uploaded video temporarily
if video_file.filename is None:
raise Exception("Video filename is none")
filename = secure_filename(video_file.filename)
with tempfile.TemporaryDirectory() as tmpdir:
filepath = os.path.join(tmpdir, filename)
video_file.save(filepath)
timestamps = detection_in_video_batched(
video_path=filepath,
prompt_input=description,
method="detection",
every_n_seconds=every_n_seconds,
max_frames=max_frames,
batch_size=batch_size
)
return jsonify({"timestamps": timestamps})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/transcribe", methods=["POST"])
def transcribe():
if "video" not in request.files or "description" not in request.form:
return jsonify({"error": "Missing video or description"}), 400
video_file = request.files["video"]
if video_file.filename == "":
return jsonify({"error": "Empty filename"}), 400
try:
# Securely save the uploaded video temporarily
if video_file.filename is None:
raise Exception("Video filename is none")
filename = secure_filename(video_file.filename)
with tempfile.TemporaryDirectory() as tmpdir:
filepath = os.path.join(tmpdir, filename)
video_file.save(filepath)
# segments = transcribe_audio(filepath)
segments = []
return jsonify({"segments": segments})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True, port=5000)