2nzi commited on
Commit
3a13dbf
·
verified ·
1 Parent(s): 0bc0490

update app.py url linked to frontend for analyze_video

Browse files
Files changed (1) hide show
  1. app.py +73 -26
app.py CHANGED
@@ -11,6 +11,7 @@ from fastapi.responses import HTMLResponse
11
  from huggingface_hub import HfApi
12
  import os
13
  from dotenv import load_dotenv
 
14
 
15
  # Charger les variables d'environnement, y compris la clé API Hugging Face
16
  load_dotenv()
@@ -109,41 +110,87 @@ def convertir_sequences_en_json(dataframe):
109
  events.append(event)
110
  return events
111
 
112
- # Endpoint pour analyser la vidéo et uploader sur Hugging Face
113
- @app.post("/analyze_video/")
114
- async def analyze_video(user_name: str, file: UploadFile = File(...)):
115
- try:
116
- # Sauvegarder la vidéo temporairement
117
- temp_file_path = f"/tmp/{file.filename}"
118
- with open(temp_file_path, "wb") as buffer:
119
- shutil.copyfileobj(file.file, buffer)
120
 
121
- # Uploader la vidéo sur Hugging Face Hub
122
- dataset_name = "2nzi/Video-Sequence-Labeling"
123
- target_path_in_repo = f"{user_name}/raw/{file.filename}"
124
 
125
- hf_api.upload_file(
126
- path_or_fileobj=temp_file_path,
127
- path_in_repo=target_path_in_repo,
128
- repo_id=dataset_name,
129
- repo_type="dataset",
130
- token=api_key
131
- )
132
 
133
- # Analyser la vidéo pour trouver des séquences "Surfing"
134
- dataframe_sequences = identifier_sequences_surfing(temp_file_path, intervalle=1)
135
- json_result = convertir_sequences_en_json(dataframe_sequences)
 
 
 
136
 
137
- # Supprimer le fichier temporaire après l'upload
138
- os.remove(temp_file_path)
 
139
 
140
- return {"message": "Video uploaded and analyzed successfully!",
141
- "file_url": f"https://huggingface.co/datasets/{dataset_name}/resolve/main/{target_path_in_repo}",
142
- "analysis": json_result}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  except Exception as e:
145
  raise HTTPException(status_code=500, detail=f"Failed to upload or analyze video: {str(e)}")
146
 
 
147
  # Fonction pour uploader une vidéo vers un dataset Hugging Face
148
  def upload_to_hf_dataset(user_name: str, video_path: str):
149
  dataset_name = "2nzi/Video-Sequence-Labeling"
 
11
  from huggingface_hub import HfApi
12
  import os
13
  from dotenv import load_dotenv
14
+ from typing import Optional
15
 
16
  # Charger les variables d'environnement, y compris la clé API Hugging Face
17
  load_dotenv()
 
110
  events.append(event)
111
  return events
112
 
113
+ # # Endpoint pour analyser la vidéo et uploader sur Hugging Face
114
+ # @app.post("/analyze_video/")
115
+ # async def analyze_video(user_name: str, file: UploadFile = File(...)):
116
+ # try:
117
+ # # Sauvegarder la vidéo temporairement
118
+ # temp_file_path = f"/tmp/{file.filename}"
119
+ # with open(temp_file_path, "wb") as buffer:
120
+ # shutil.copyfileobj(file.file, buffer)
121
 
122
+ # # Uploader la vidéo sur Hugging Face Hub
123
+ # dataset_name = "2nzi/Video-Sequence-Labeling"
124
+ # target_path_in_repo = f"{user_name}/raw/{file.filename}"
125
 
126
+ # hf_api.upload_file(
127
+ # path_or_fileobj=temp_file_path,
128
+ # path_in_repo=target_path_in_repo,
129
+ # repo_id=dataset_name,
130
+ # repo_type="dataset",
131
+ # token=api_key
132
+ # )
133
 
134
+ # # Analyser la vidéo pour trouver des séquences "Surfing"
135
+ # dataframe_sequences = identifier_sequences_surfing(temp_file_path, intervalle=1)
136
+ # json_result = convertir_sequences_en_json(dataframe_sequences)
137
+
138
+ # # Supprimer le fichier temporaire après l'upload
139
+ # os.remove(temp_file_path)
140
 
141
+ # return {"message": "Video uploaded and analyzed successfully!",
142
+ # "file_url": f"https://huggingface.co/datasets/{dataset_name}/resolve/main/{target_path_in_repo}",
143
+ # "analysis": json_result}
144
 
145
+ # except Exception as e:
146
+ # raise HTTPException(status_code=500, detail=f"Failed to upload or analyze video: {str(e)}")
147
+
148
+
149
+ @app.post("/analyze_video/")
150
+ async def analyze_video(user_name: str, file: Optional[UploadFile] = File(None), video_url: Optional[str] = None):
151
+ try:
152
+ # Vérifier si la vidéo est fournie sous forme de fichier ou d'URL
153
+ if file:
154
+ # Sauvegarder la vidéo temporairement
155
+ temp_file_path = f"/tmp/{file.filename}"
156
+ with open(temp_file_path, "wb") as buffer:
157
+ shutil.copyfileobj(file.file, buffer)
158
+
159
+ # Uploader la vidéo sur Hugging Face Hub
160
+ dataset_name = "2nzi/Video-Sequence-Labeling"
161
+ target_path_in_repo = f"{user_name}/raw/{file.filename}"
162
+
163
+ hf_api.upload_file(
164
+ path_or_fileobj=temp_file_path,
165
+ path_in_repo=target_path_in_repo,
166
+ repo_id=dataset_name,
167
+ repo_type="dataset",
168
+ token=os.getenv("HUGGINGFACE_WRITE_API_KEY")
169
+ )
170
+
171
+ # URL de la vidéo sur Hugging Face
172
+ video_url = f"https://huggingface.co/datasets/{dataset_name}/resolve/main/{target_path_in_repo}"
173
+ # Supprimer le fichier temporaire après l'upload
174
+ os.remove(temp_file_path)
175
+
176
+ # Assurez-vous d'avoir une URL valide à ce stade
177
+ if not video_url:
178
+ raise HTTPException(status_code=400, detail="No valid video URL or file provided.")
179
+
180
+ # Analyser la vidéo via l'URL
181
+ dataframe_sequences = identifier_sequences_surfing(video_url, intervalle=1)
182
+ json_result = convertir_sequences_en_json(dataframe_sequences)
183
+
184
+ return {
185
+ "message": "Video uploaded and analyzed successfully!",
186
+ "file_url": video_url,
187
+ "analysis": json_result
188
+ }
189
 
190
  except Exception as e:
191
  raise HTTPException(status_code=500, detail=f"Failed to upload or analyze video: {str(e)}")
192
 
193
+
194
  # Fonction pour uploader une vidéo vers un dataset Hugging Face
195
  def upload_to_hf_dataset(user_name: str, video_path: str):
196
  dataset_name = "2nzi/Video-Sequence-Labeling"