Spaces:
Running
on
Zero
Running
on
Zero
liuyang
commited on
Commit
·
06b904d
1
Parent(s):
0724301
add upload
Browse files- app.py +56 -1
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
import spaces
|
|
|
|
|
|
|
2 |
|
3 |
import os, pathlib
|
4 |
|
@@ -51,7 +54,53 @@ try:
|
|
51 |
except OSError as e:
|
52 |
sys.exit(f"❌ Could not load {cnn_so} : {e}")
|
53 |
|
|
|
|
|
|
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
from huggingface_hub import snapshot_download
|
56 |
MODEL_REPO = "deepdml/faster-whisper-large-v3-turbo-ct2" # CT2 format
|
57 |
LOCAL_DIR = f"{CACHE_ROOT}/whisper_turbo"
|
@@ -667,7 +716,7 @@ class WhisperTranscriber:
|
|
667 |
transcription_results = self.group_segments_by_speaker(transcription_results)
|
668 |
|
669 |
# Step 6: Return results
|
670 |
-
|
671 |
"segments": transcription_results,
|
672 |
"language": detected_language,
|
673 |
"num_speakers": detected_num_speakers,
|
@@ -675,6 +724,12 @@ class WhisperTranscriber:
|
|
675 |
"batch_size": batch_size,
|
676 |
"speaker_embeddings": speaker_embeddings,
|
677 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
678 |
|
679 |
except Exception as e:
|
680 |
import traceback
|
|
|
1 |
import spaces
|
2 |
+
import boto3
|
3 |
+
from botocore.exceptions import NoCredentialsError, ClientError
|
4 |
+
from botocore.client import Config
|
5 |
|
6 |
import os, pathlib
|
7 |
|
|
|
54 |
except OSError as e:
|
55 |
sys.exit(f"❌ Could not load {cnn_so} : {e}")
|
56 |
|
57 |
+
S3_ENDPOINT = os.getenv("S3_ENDPOINT")
|
58 |
+
S3_ACCESS_KEY = os.getenv("S3_ACCESS_KEY")
|
59 |
+
S3_SECRET_KEY = os.getenv("S3_SECRET_KEY")
|
60 |
|
61 |
+
|
62 |
+
|
63 |
+
# Function to upload file to Cloudflare R2
|
64 |
+
def upload_to_r2(file_path, bucket_name, object_name):
|
65 |
+
"""
|
66 |
+
Upload a file to a Cloudflare R2 bucket.
|
67 |
+
|
68 |
+
:param file_path: Path to the file to upload.
|
69 |
+
:param bucket_name: Name of the R2 bucket.
|
70 |
+
:param object_name: Name of the object to save in the bucket.
|
71 |
+
:param r2_access_key: Cloudflare R2 access key.
|
72 |
+
:param r2_secret_key: Cloudflare R2 secret key.
|
73 |
+
:param r2_account_id: Cloudflare R2 account ID.
|
74 |
+
:return: True if file was uploaded, else False.
|
75 |
+
"""
|
76 |
+
try:
|
77 |
+
# Initialize a session using Cloudflare R2 credentials
|
78 |
+
session = boto3.session.Session()
|
79 |
+
s3 = session.client('s3',
|
80 |
+
endpoint_url=f'https://{S3_ENDPOINT}',
|
81 |
+
aws_access_key_id=S3_ACCESS_KEY,
|
82 |
+
aws_secret_access_key=S3_SECRET_KEY,
|
83 |
+
config = Config(s3={"addressing_style": "virtual"}, signature_version='s3v4'),
|
84 |
+
#region_name = 'auto'
|
85 |
+
)
|
86 |
+
|
87 |
+
# Upload the file to R2 bucket
|
88 |
+
s3.upload_file(file_path, bucket_name, object_name)
|
89 |
+
print(f"File '{file_path}' uploaded to R2 bucket '{bucket_name}' as '{object_name}'")
|
90 |
+
return True
|
91 |
+
except FileNotFoundError:
|
92 |
+
print(f"The file {file_path} was not found")
|
93 |
+
return False
|
94 |
+
except NoCredentialsError:
|
95 |
+
print("Credentials not available")
|
96 |
+
return False
|
97 |
+
except ClientError as e:
|
98 |
+
print(f"Failed to upload file to R2 bucket: {e}")
|
99 |
+
return False
|
100 |
+
except Exception as e:
|
101 |
+
print(f"An unexpected error occurred: {e}")
|
102 |
+
return False
|
103 |
+
|
104 |
from huggingface_hub import snapshot_download
|
105 |
MODEL_REPO = "deepdml/faster-whisper-large-v3-turbo-ct2" # CT2 format
|
106 |
LOCAL_DIR = f"{CACHE_ROOT}/whisper_turbo"
|
|
|
716 |
transcription_results = self.group_segments_by_speaker(transcription_results)
|
717 |
|
718 |
# Step 6: Return results
|
719 |
+
result = {
|
720 |
"segments": transcription_results,
|
721 |
"language": detected_language,
|
722 |
"num_speakers": detected_num_speakers,
|
|
|
724 |
"batch_size": batch_size,
|
725 |
"speaker_embeddings": speaker_embeddings,
|
726 |
}
|
727 |
+
filekey = f"ai-transcribe/split/{task_json["job_id"]}-{task_json["task_id"]["chunk"]["idx"]}.json"
|
728 |
+
ret = upload_to_r2(json.dumps(result), "intermediate", filekey)
|
729 |
+
if ret:
|
730 |
+
return {"filekey": filekey}
|
731 |
+
else:
|
732 |
+
return {"error": "Failed to upload to R2"}
|
733 |
|
734 |
except Exception as e:
|
735 |
import traceback
|
requirements.txt
CHANGED
@@ -20,4 +20,5 @@ soundfile>=0.12.0
|
|
20 |
ffmpeg-python>=0.2.0
|
21 |
requests>=2.28.0
|
22 |
nvidia-cudnn-cu12==9.1.0.70 # any 9.1.x that pip can find is fine
|
23 |
-
webrtcvad>=2.0.10
|
|
|
|
20 |
ffmpeg-python>=0.2.0
|
21 |
requests>=2.28.0
|
22 |
nvidia-cudnn-cu12==9.1.0.70 # any 9.1.x that pip can find is fine
|
23 |
+
webrtcvad>=2.0.10
|
24 |
+
boto3
|