VeuReu commited on
Commit
7ed091e
verified
1 Parent(s): 90784d2

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +61 -0
api.py CHANGED
@@ -23,6 +23,8 @@ from character_detection import detect_characters_from_video
23
 
24
  from pipelines.audiodescription import generate as ad_generate
25
 
 
 
26
  app = FastAPI(title="Veureu Engine API", version="0.2.0")
27
  app.add_middleware(
28
  CORSMiddleware,
@@ -40,6 +42,8 @@ VIDEOS_ROOT = Path("/tmp/data/videos")
40
  VIDEOS_ROOT.mkdir(parents=True, exist_ok=True)
41
  IDENTITIES_ROOT = Path("/tmp/characters")
42
  IDENTITIES_ROOT.mkdir(parents=True, exist_ok=True)
 
 
43
 
44
  # Sistema de jobs as铆ncronos
45
  class JobStatus(str, Enum):
@@ -50,6 +54,63 @@ class JobStatus(str, Enum):
50
 
51
  jobs: Dict[str, dict] = {}
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  def describe_image_with_svision(image_path: str, is_face: bool = True) -> tuple[str, str]:
54
  """
55
  Llama al space svision para describir una imagen (usado en generaci贸n de AD).
 
23
 
24
  from pipelines.audiodescription import generate as ad_generate
25
 
26
+ from storage.files.file_manager import FileManager
27
+
28
  app = FastAPI(title="Veureu Engine API", version="0.2.0")
29
  app.add_middleware(
30
  CORSMiddleware,
 
42
  VIDEOS_ROOT.mkdir(parents=True, exist_ok=True)
43
  IDENTITIES_ROOT = Path("/tmp/characters")
44
  IDENTITIES_ROOT.mkdir(parents=True, exist_ok=True)
45
+ MEDIA_ROOT = Path("/data/media")
46
+ file_manager = FileManager(MEDIA_ROOT)
47
 
48
  # Sistema de jobs as铆ncronos
49
  class JobStatus(str, Enum):
 
54
 
55
  jobs: Dict[str, dict] = {}
56
 
57
+
58
+ @app.post("/upload_video", tags=["Media Upload"])
59
+ async def upload_video(video: UploadFile = File(...)):
60
+ """
61
+ Saves an uploaded video by hashing it with SHA1 and placing it under:
62
+ /data/media/<sha1>/clip/<original_filename>
63
+
64
+ Steps:
65
+ - Compute SHA1 of the uploaded video.
66
+ - Ensure /data/media exists.
67
+ - Create folder /data/media/<sha1> if missing.
68
+ - Create folder /data/media/<sha1>/clip if missing.
69
+ - Save the video inside /data/media/<sha1>/clip/.
70
+ """
71
+
72
+ # Read content into memory (needed to compute hash twice)
73
+ file_bytes = await video.read()
74
+
75
+ # Create an in-memory file handler
76
+ import io
77
+ file_handler = io.BytesIO(file_bytes)
78
+
79
+ # Compute SHA1 using your FileManager method
80
+ try:
81
+ sha1 = file_manager.compute_sha1(file_handler)
82
+ except Exception as exc:
83
+ raise HTTPException(status_code=500, detail=f"SHA1 computation failed: {exc}")
84
+
85
+ # Ensure /data/media exists
86
+ MEDIA_ROOT.mkdir(parents=True, exist_ok=True)
87
+
88
+ # Path: /data/media/<sha1>
89
+ video_root = MEDIA_ROOT / sha1
90
+ video_root.mkdir(parents=True, exist_ok=True)
91
+
92
+ # Path: /data/media/<sha1>/clip
93
+ clip_dir = video_root / "clip"
94
+ clip_dir.mkdir(parents=True, exist_ok=True)
95
+
96
+ # Final file path
97
+ final_path = clip_dir / video.filename
98
+
99
+ # Save file using your FileManager.upload_file
100
+ save_result = file_manager.upload_file(io.BytesIO(file_bytes), final_path)
101
+
102
+ if not save_result["operation_success"]:
103
+ raise HTTPException(status_code=500, detail=save_result["error"])
104
+
105
+ return JSONResponse(
106
+ status_code=200,
107
+ content={
108
+ "status": "ok",
109
+ "sha1": sha1,
110
+ "saved_to": str(final_path)
111
+ }
112
+ )
113
+
114
  def describe_image_with_svision(image_path: str, is_face: bool = True) -> tuple[str, str]:
115
  """
116
  Llama al space svision para describir una imagen (usado en generaci贸n de AD).