sreepathi-ravikumar commited on
Commit
d8536ff
·
verified ·
1 Parent(s): 3e20fac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -5
app.py CHANGED
@@ -1,3 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from flask import Flask, request, jsonify, send_file
2
  from flask_cors import CORS
3
  import os
@@ -239,14 +277,135 @@ def generate_video():
239
 
240
 
241
 
 
 
 
242
 
243
- raw_data = request.get_json()
244
- print(raw_data)
245
-
246
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
 
248
  # Return video file as blob
249
- return 0
 
 
 
 
 
250
 
251
  except subprocess.TimeoutExpired:
252
  print("Video rendering timeout")
@@ -261,4 +420,4 @@ def generate_video():
261
 
262
  if __name__ == '__main__':
263
  port = int(os.environ.get('PORT', 7860))
264
- app.run(host='0.0.0.0', port=port, debug=False)
 
1
+ Hugging Face's logo
2
+ Hugging Face
3
+ Models
4
+ Datasets
5
+ Spaces
6
+ Docs
7
+ Pricing
8
+
9
+
10
+ Spaces:
11
+ sreepathi-ravikumar
12
+ /
13
+ backendprocessmath
14
+
15
+ like
16
+ 0
17
+
18
+ App
19
+ Files
20
+ Community
21
+ Settings
22
+ backendprocessmath
23
+ /
24
+ app.py
25
+
26
+ sreepathi-ravikumar's picture
27
+ sreepathi-ravikumar
28
+ Update app.py
29
+ 3ec6d35
30
+ VERIFIED
31
+ 7 days ago
32
+ raw
33
+
34
+ Copy download link
35
+ history
36
+ blame
37
+
38
+ 14.4 kB
39
  from flask import Flask, request, jsonify, send_file
40
  from flask_cors import CORS
41
  import os
 
277
 
278
 
279
 
280
+ raw_body = request.data.decode("utf-8").strip()
281
+ print(f"Raw body length: {len(raw_body)}")
282
+ print(f"First 200 chars: {raw_body[:200]}")
283
 
 
 
 
284
 
285
+
286
+ # ... (your existing raw_body handling)
287
+
288
+
289
+
290
+ lst = raw_body.split("&&&&")
291
+ cleaned = re.sub(r'(\d)\s*\.\s*(\d)', r'\1.\2', lst[0])
292
+ nlist = ast.literal_eval(cleaned)
293
+ datalst=[]
294
+ for line in range(len(nlist)):
295
+ datalst.append({
296
+ "type": nlist[line][0].strip(),
297
+ "content": nlist[line][1].strip(),
298
+ "animation": nlist[line][2].strip().replace(" ",""),
299
+ "duration": nlist[line][3]
300
+ })
301
+
302
+ data={
303
+ "video_settings": {
304
+ "background_color": "#0f0f23",
305
+ "text_color": "WHITE",
306
+ "highlight_color": "YELLOW",
307
+ "font": "CMU Serif",
308
+ "text_size": 36,
309
+ "equation_size": 42,
310
+ "title_size": 48
311
+ },
312
+ "slides":datalst}
313
+ # Now proceed with video generation using 'data'
314
+ print(json.dumps(data, indent=2)) # For debugging
315
+ # ✅ Final validation
316
+ if "slides" not in data or not data["slides"]:
317
+ return jsonify({"error": "No slides provided in request"}), 400
318
+
319
+ print(f"✅ Parsed {len(data['slides'])} slides successfully.")
320
+
321
+ # Validate input
322
+ if "slides" not in data or not data["slides"]:
323
+ return jsonify({"error": "No slides provided in request"}), 400
324
+
325
+ print(f"Received request with {len(data['slides'])} slides")
326
+
327
+ # Create unique temporary directory
328
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
329
+ temp_work_dir = os.path.join(TEMP_DIR, f"manim_{timestamp}")
330
+ os.makedirs(temp_work_dir, exist_ok=True)
331
+
332
+ # Generate Manim script
333
+ script_path = os.path.join(temp_work_dir, "scene.py")
334
+ create_manim_script(data, script_path)
335
+ print(f"Created Manim script at {script_path}")
336
+
337
+ # Render video using subprocess
338
+ quality = request.args.get('quality', 'l') # l=low, m=medium, h=high
339
+ render_command = [
340
+ "manim",
341
+ f"-q{quality}",
342
+ "--disable_caching",
343
+ "--media_dir", temp_work_dir,
344
+ script_path,
345
+ "GeneratedMathScene"
346
+ ]
347
+
348
+ print(f"Running command: {' '.join(render_command)}")
349
+
350
+ result = subprocess.run(
351
+ render_command,
352
+ capture_output=True,
353
+ text=True,
354
+ cwd=temp_work_dir,
355
+ timeout=120
356
+ )
357
+
358
+ if result.returncode != 0:
359
+ error_msg = result.stderr or result.stdout
360
+ print(f"Manim rendering failed: {error_msg}")
361
+ return jsonify({
362
+ "error": "Manim rendering failed",
363
+ "details": error_msg
364
+ }), 500
365
+
366
+ print("Manim rendering completed successfully")
367
+
368
+ # Find generated video
369
+ quality_map = {'l': '480p15', 'm': '720p30', 'h': '1080p60'}
370
+ video_quality = quality_map.get(quality, '480p15')
371
+
372
+ video_path = os.path.join(
373
+ temp_work_dir,
374
+ "videos",
375
+ "scene",
376
+ video_quality,
377
+ "GeneratedMathScene.mp4"
378
+ )
379
+
380
+ if not os.path.exists(video_path):
381
+ print(f"Video not found at expected path: {video_path}")
382
+ return jsonify({
383
+ "error": "Video file not found after rendering",
384
+ "expected_path": video_path
385
+ }), 500
386
+
387
+ print(f"Video found at: {video_path}")
388
+
389
+ # Copy to media directory
390
+ output_filename = f"math_video_{timestamp}.mp4"
391
+ output_path = os.path.join(MEDIA_DIR, output_filename)
392
+ shutil.copy(video_path, output_path)
393
+ print(f"Video copied to: {output_path}")
394
+
395
+ # Clean up temp directory
396
+ try:
397
+ shutil.rmtree(temp_work_dir)
398
+ print("Cleaned up temp directory")
399
+ except Exception as e:
400
+ print(f"Failed to clean temp dir: {e}")
401
 
402
  # Return video file as blob
403
+ return send_file(
404
+ output_path,
405
+ mimetype='video/mp4',
406
+ as_attachment=False,
407
+ download_name=output_filename
408
+ )
409
 
410
  except subprocess.TimeoutExpired:
411
  print("Video rendering timeout")
 
420
 
421
  if __name__ == '__main__':
422
  port = int(os.environ.get('PORT', 7860))
423
+ app.run(host='0.0.0.0', port=port, debug=False)