Update main.py
Browse files196 unmodified lines1971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372381 unmodified line240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276196 unmodified linessemaphore = asyncio.Semaphore(1)# Функция для отправки видео на транскрипцию# async def send_video_for_transcription(file: UploadFile, hash_path: str):# async with semaphore:# try:# logger.info(f"Background task: sending video {file.filename} for transcription")# run_time = api.get_space_runtime(repo_id=repo_id)# if run_time.stage != 'RUNNING':# restart_and_check_space(repo_id) # Перезапуск транскрибационного сервиса, если он не запущен# else:# logger.info('Transcription space is running, sending file')# async with httpx.AsyncClient() as client:# files = {# 'file': (file.filename, file.file, file.content_type)# }# data = {# 'final_url': "https://sabbah13-video-call-recording-and-transcription-9729a08.hf.space/transcript/video-file/",# 'max_speakers': 10,# 'min_speakers': 1,# "hash_path": hash_path# }# headers = {# "Authorization": f'Bearer {os.environ.get("HF_TOKEN")}'# }# # Отправляем видео на сервер транскрибации# response = await client.post(transcribation_url, files=files, data=data, headers=headers)# if response.status_code == 200:# logger.info(f"Video successfully sent for transcription {file.filename}")# else:# logger.error(f"Error sending video for transcription: {response.text}")# except Exception as e:# logger.error(f"Error in background task: {e}")@app.post("/upload/")1 unmodified linetry:logger.info(f"Received video: {file.filename}, hash_path: {hash_path}")run_time = api.get_space_runtime(repo_id=repo_id)if run_time.stage != 'RUNNING':await restart_and_check_space(repo_id) # Перезапуск транскрибационного сервиса, если он не запущенelse:logger.info('Transcription space is running, sending file')async with httpx.AsyncClient() as client:files = {'file': (file.filename, file.file, file.content_type)}data = {'final_url': "https://sabbah13-video-call-recording-and-transcription-9729a08.hf.space/transcript/video-file/",'max_speakers': 10,'min_speakers': 1,"hash_path": hash_path}headers = {"Authorization": f'Bearer {os.environ.get("HF_TOKEN")}'}# Отправляем видео на сервер транскрибацииresponse = await client.post(transcribation_url, files=files, data=data, headers=headers)if response.status_code == 200:logger.info(f"Video successfully sent for transcription {file.filename}")else:logger.error(f"Error sending video for transcription: {response.text}")# background_tasks.add_task(send_video_for_transcription, file, hash_path)return JSONResponse(content={"status": "success", "message": "Video received. Processing will start soon."}, status_code=200)196 unmodified lines1971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372381 unmodified line240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276196 unmodified linessemaphore = asyncio.Semaphore(1)Функция для отправки видео на транскрипциюasync def send_video_for_transcription(file: UploadFile, hash_path: str):# async with semaphore:try:logger.info(f"Background task: sending video {file.filename} for transcription")run_time = api.get_space_runtime(repo_id=repo_id)if run_time.stage != 'RUNNING':await restart_and_check_space(repo_id) # Перезапуск транскрибационного сервиса, если он не запущенelse:logger.info('Transcription space is running, sending file')async with httpx.AsyncClient() as client:files = {'file': (file.filename, file.file, file.content_type)}data = {'final_url': "https://sabbah13-video-call-recording-and-transcription-9729a08.hf.space/transcript/video-file/",'max_speakers': 10,'min_speakers': 1,"hash_path": hash_path}headers = {"Authorization": f'Bearer {os.environ.get("HF_TOKEN")}'}# Отправляем видео на сервер транскрибацииresponse = await client.post(transcribation_url, files=files, data=data, headers=headers)if response.status_code == 200:logger.info(f"Video successfully sent for transcription {file.filename}")else:logger.error(f"Error sending video for transcription: {response.text}")except Exception as e:logger.error(f"Error in background task: {e}")@app.post("/upload/")1 unmodified linetry:logger.info(f"Received video: {file.filename}, hash_path: {hash_path}")# run_time = api.get_space_runtime(repo_id=repo_id)# if run_time.stage != 'RUNNING':# await restart_and_check_space(repo_id) # Перезапуск транскрибационно��о сервиса, если он не запущен# else:# logger.info('Transcription space is running, sending file')# async with httpx.AsyncClient() as client:# files = {# 'file': (file.filename, file.file, file.content_type)# }# data = {# 'final_url': "https://sabbah13-video-call-recording-and-transcription-9729a08.hf.space/transcript/video-file/",# 'max_speakers': 10,# 'min_speakers': 1,# "hash_path": hash_path# }# headers = {# "Authorization": f'Bearer {os.environ.get("HF_TOKEN")}'# }# # Отправляем видео на сервер транскрибации# response = await client.post(transcribation_url, files=files, data=data, headers=headers)# if response.status_code == 200:# logger.info(f"Video successfully sent for transcription {file.filename}")# else:# logger.error(f"Error sending video for transcription: {response.text}")background_tasks.add_task(send_video_for_transcription, file, hash_path)return JSONResponse(content={"status": "success", "message": "Video received. Processing will start soon."}, status_code=200)
main.py+56-56