-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
232 lines (187 loc) · 7.07 KB
/
app.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from flask import Flask, render_template, request, jsonify, send_file, send_from_directory
from twelvelabs import TwelveLabs
import os
from dotenv import load_dotenv
import uuid
from functools import wraps
import requests
from report_generator import ReportGenerator
app = Flask(__name__)
load_dotenv()
API_KEY = os.getenv('API_KEY')
INDEX_ID = os.getenv('INDEX_ID')
BASE_URL = "https://api.twelvelabs.io/v1.2"
REPORTS_DIR = os.path.join('static', 'reports')
print(INDEX_ID)
os.makedirs(REPORTS_DIR, exist_ok=True)
client = TwelveLabs(api_key=API_KEY)
def get_video_url(video_id):
headers = {
"x-api-key": API_KEY,
"Content-Type": "application/json"
}
response = requests.get(
f"{BASE_URL}/indexes/{INDEX_ID}/videos/{video_id}",
headers=headers
)
response.raise_for_status()
data = response.json()
return data.get('hls', {}).get('video_url')
def handle_errors(f):
@wraps(f)
def decorated_function(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
app.logger.error(f"Error: {str(e)}")
return jsonify({
'error': 'An error occurred processing your request',
'details': str(e)
}), 500
return decorated_function
@app.route('/')
def index():
return render_template('index.html')
SAMPLE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'sample')
@app.route('/sample/<path:filename>')
def serve_sample_video(filename):
try:
return send_from_directory(SAMPLE_DIR, filename)
except Exception as e:
app.logger.error(f"Error serving video: {str(e)}")
return f"Error: {str(e)}", 404
print(f"Sample directory path: {SAMPLE_DIR}")
@app.route('/search', methods=['POST'])
@handle_errors
def search():
if not request.is_json:
return jsonify({'error': 'Request must be JSON'}), 400
data = request.get_json()
query = data.get('query')
if not query:
return jsonify({'error': 'Query parameter is required'}), 400
try:
search_results = client.search.query(
index_id=INDEX_ID,
query_text=query,
options=["visual"]
)
formatted_results = []
for clip in search_results.data:
try:
headers = {
"x-api-key": API_KEY,
"Content-Type": "application/json"
}
print(headers)
print(f"{BASE_URL}/indexes/{INDEX_ID}/videos/{clip.video_id}")
url_response = requests.get(
f"{BASE_URL}/indexes/{INDEX_ID}/videos/{clip.video_id}",
headers=headers
)
video_data = url_response.json()
video_url = video_data.get('hls', {}).get('video_url')
video_duration = video_data.get('metadata', {}).get('duration', 0)
formatted_results.append({
'video_id': clip.video_id,
'score': clip.score,
'confidence': 'High' if clip.score > 0.7 else 'Medium',
'start': clip.start,
'end': clip.end,
'duration': video_duration,
'video_url': video_url
})
except Exception as e:
app.logger.error(f"Error getting video URL: {str(e)}")
continue
return jsonify(formatted_results)
except Exception as e:
app.logger.error(f"Search Error: {str(e)}")
return jsonify({'error': 'Search failed', 'details': str(e)}), 500
@app.route('/analyze/<video_id>')
@handle_errors
def analyze(video_id):
"""Handle video analysis requests"""
try:
app.logger.info(f"Starting analysis for video: {video_id}")
try:
# 1. Get video URL
headers = {
"x-api-key": API_KEY,
"Content-Type": "application/json"
}
url_response = requests.get(
f"{BASE_URL}/indexes/{INDEX_ID}/videos/{video_id}",
headers=headers
)
url_data = url_response.json()
video_url = url_data.get('hls', {}).get('video_url')
# 2. Generate analysis
analysis_response = client.generate.text(
video_id=video_id,
prompt="Provide a detailed analysis of the key events, actions, and notable elements in this video."
)
# Extract and clean analysis text
analysis_text = str(analysis_response.data) if hasattr(analysis_response, 'data') else ''
analysis_text = analysis_text.strip().strip('"\'')
response_data = {
'video_url': video_url,
'analysis': analysis_text or "No analysis available."
}
app.logger.info("Analysis completed successfully")
return jsonify(response_data)
except Exception as e:
app.logger.error(f"Error in analysis: {str(e)}")
raise
except Exception as e:
app.logger.error(f"Analysis Error: {str(e)}")
return jsonify({
'error': 'Analysis failed',
'details': str(e)
}), 500
@app.route('/generate-report', methods=['POST'])
@handle_errors
def generate_report():
if not request.is_json:
return jsonify({'error': 'Request must be JSON'}), 400
try:
analysis_text = request.json.get('analysis')
if not analysis_text:
return jsonify({'error': 'Analysis text is required'}), 400
report_id = str(uuid.uuid4())[:8]
filename = f"report_{report_id}.pdf"
filepath = os.path.join(REPORTS_DIR, filename)
report_gen = ReportGenerator()
report_gen.generate_report(
report_id=report_id,
report_text=analysis_text,
output_filename=filepath
)
return jsonify({
'success': True,
'report_url': f'/download-report/{filename}'
})
except Exception as e:
app.logger.error(f"Report Generation Error: {str(e)}")
return jsonify({
'error': 'Failed to generate report',
'details': str(e)
}), 500
@app.route('/download-report/<filename>')
@handle_errors
def download_report(filename):
try:
filepath = os.path.join(REPORTS_DIR, filename)
if not os.path.exists(filepath):
return jsonify({'error': 'Report not found'}), 404
return send_file(
filepath,
as_attachment=True,
download_name=f"video_analysis_report_{filename}",
mimetype='application/pdf'
)
except Exception as e:
app.logger.error(f"Download Error: {str(e)}")
return jsonify({'error': 'Failed to download report', 'details': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)