File size: 10,224 Bytes
433f1c5
 
 
 
c4465c4
 
 
433f1c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4465c4
433f1c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4465c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
433f1c5
 
 
 
 
c4465c4
433f1c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from typing import List, Dict, Any
import os
import shutil
import uuid
import time
from functools import wraps
from fastapi import FastAPI, Request, UploadFile, File, HTTPException, status
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from starlette.middleware.sessions import SessionMiddleware
from starlette.responses import FileResponse
from starlette.background import BackgroundTask
from pydantic import BaseModel
from datasets import Dataset, Audio


# --- Pydantic Models for Data Validation/Serialization ---
class SaveAnnotationRequest(BaseModel):
    """Model for the POST request payload to save transcription."""
    index: int
    transcription: str
    speaker: str

class AudioDataResponse(BaseModel):
    """Model for the GET response when loading an audio row."""
    index: int
    filename: str
    transcription: str
    speaker: str
    max_index: int


# --- Configuration and Global State ---

# Directory to save user-uploaded audio files
UPLOAD_DIR = "./uploaded_audio"
# Data structure to hold the annotation state: {f'{user_id}': [{"filename": str, "speaker": str, "transcription": str}]}
ANNOTATION_DATA: Dict[str, List[Dict[str, Any]]] = {}
# Index of the audio file currently being displayed/annotated
current_index: Dict[str, int] = {}

# Ensure the upload directory exists
os.makedirs(UPLOAD_DIR, exist_ok=True)


# --- FastAPI Setup ---
app = FastAPI(title="Audio Annotation Tool with File Upload")
app.mount("/static", StaticFiles(directory="./static", html=True), name="static")
app.add_middleware(SessionMiddleware, secret_key="audio-annotator-application")
templates = Jinja2Templates(directory="./templates")


# --- Utility Functions ---
def load_data_for_index(user_id: int, index: int) -> Dict[str, Any]:
    """Helper to safely fetch data for a given index."""
    if not ANNOTATION_DATA[user_id]:
        return {"index": -1, "filename": "", "transcription": "No files uploaded yet.", "speaker": "No files uploaded yet.", "max_index": 0}
    if 0 <= index < len(ANNOTATION_DATA[user_id]):
        item = ANNOTATION_DATA[user_id][index]
        return {
            "index": index,
            "filename": item['filename'],
            "transcription": item['transcription'],
            "speaker": item['speaker'],
            "max_index": len(ANNOTATION_DATA[user_id])
        }
    else:
        # Wrap around if needed, or handle boundary cases
        raise IndexError("Index out of bounds.")

def get_user_directory(user_id):
    return os.path.join(UPLOAD_DIR, f'{user_id}')

def serve_index_html(request: Request):
    try:
        user_id = request.session.get('_id', None)
        if user_id is None:
            user_id = str(uuid.uuid4())
            request.session['_id'] = user_id
            # Ensure the upload directory exists
            # print(user_id)
            user_dir = get_user_directory(user_id)
            os.makedirs(user_dir, exist_ok=True)
            ANNOTATION_DATA[user_id] = []
            current_index[user_id] = -1

        return templates.TemplateResponse("index.html", context={"request": request})
    except FileNotFoundError:
        return HTMLResponse(content="<h1>Server Error: index.html not found.</h1>", status_code=500)

def rate_limited(max_calls: int, time_frame:int):
    """
    :param max_calls: Maximum number of calls allowed in the specified time frame.
    :param time_frame: The time frame (in seconds) for which the limit applies.
    :return: Decorator function.
    """
    def decorator(func):
        calls = []

        @wraps(func)
        async def wrapper(request: Request, *args, **kwargs):
            now = time.time()
            calls_in_time_frame = [call for call in calls if call > now - time_frame]
            if len(calls_in_time_frame) > max_calls:
                raise HTTPException(status_code=status.HTTP_429_TOO_MANY_REQUESTS, detail="Rate limit exceeded.")
            calls.append(now)
            return await func(request, *args, **kwargs)
        return wrapper
    return decorator

# --- Routes ---


@app.get("/")
@rate_limited(max_calls=10, time_frame=60)
async def index(request: Request):
    """Serves the main application page by reading index.html."""
    return serve_index_html(request)


@app.get("/annotate")
async def annotate(request: Request):
    """Serves the main application page by reading index.html."""
    return serve_index_html(request)


@app.post("/upload_audio")
async def upload_audio(request: Request, audio_files: List[UploadFile] = File(...)):
    """Handles multiple audio file uploads from the client."""
    global ANNOTATION_DATA, current_index
    new_files_count = 0
    user_id = request.session.get('_id', None)

    # Reset index if this is the first upload
    if not ANNOTATION_DATA[user_id]:
        current_index[user_id] = 0

    for file in audio_files:
        # Construct the full path
        user_dir = get_user_directory(user_id)
        file_path = os.path.join(user_dir, file.filename)

        # Save the file to disk
        try:
            with open(file_path, "wb") as buffer:
                # Read the file chunk by chunk to handle large files
                shutil.copyfileobj(file.file, buffer)

            # Update the annotation data structure
            ANNOTATION_DATA[user_id].append({
                "filename": file.filename,
                "transcription": "",  # Initialize transcription as empty,
                "speaker": ""  # Initialize speaker as empty
            })
            new_files_count += 1
        except Exception as e:
            print(f"Error saving file {file.filename}: {e}")
            raise HTTPException(status_code=500, detail=f"Failed to save file: {file.filename}")

    return JSONResponse({
        "message": f"Successfully uploaded {new_files_count} files.",
        "total_files": len(ANNOTATION_DATA[user_id])
    })


@app.post("/save_annotation")
async def save_annotation(request: Request, data: SaveAnnotationRequest):
    """Saves the transcription for the current index."""
    try:
        user_id = request.session.get('_id', None)
        index_to_save = data.index

        if 0 <= index_to_save < len(ANNOTATION_DATA[user_id]):
            # Update the transcription text
            ANNOTATION_DATA[user_id][index_to_save]["transcription"] = data.transcription
            ANNOTATION_DATA[user_id][index_to_save]["speaker"] = data.speaker
            return JSONResponse({"success": True, "message": f"Row {index_to_save + 1} saved."})
        else:
            raise HTTPException(status_code=400, detail="Invalid index for saving.")

    except Exception as e:
        print(f"Error during save: {e}")
        raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")


@app.get("/load_audio_data/{direction}")
async def load_audio_data(request: Request, direction: str):
    """Loads the audio data and increments/decrements the current_index."""
    global current_index
    user_id = request.session.get('_id', None)

    if not ANNOTATION_DATA[user_id]:
        return JSONResponse(load_data_for_index(user_id, -1))

    new_index = current_index[user_id]
    max_len = len(ANNOTATION_DATA[user_id])

    if direction == 'next':
        new_index = (current_index[user_id] + 1) % max_len
    elif direction == 'prev':
        # Handles wrapping from 0 back to the last index
        new_index = (current_index[user_id] - 1 + max_len) % max_len
    else:
        # 'current' direction is used for initial load or after upload
        pass

    try:
        data = load_data_for_index(user_id, new_index)
        # Only update the global index if navigation was successful
        current_index[user_id] = new_index
        return JSONResponse(data)
    except IndexError:
        raise HTTPException(status_code=404, detail="No more audio files to load.")


@app.get("/audio_file/{filename}")
async def serve_audio_file(request: Request, filename: str):
    """Streams the requested audio file from the upload directory."""
    user_id = request.session.get('_id', None)
    user_dir = get_user_directory(user_id)
    file_path = os.path.join(user_dir, filename)

    if os.path.exists(file_path):
        # FileResponse sends the file directly, optimized for binary streams
        return FileResponse(file_path, media_type="audio/wav") # Assume WAV for simplicity, use relevant type if required

    raise HTTPException(status_code=404, detail="Audio file not found.")


@app.get("/download_annotations")
async def download_annotations(request: Request):
    """Returns the entire annotated dataset as a downloadable JSON file."""
    global current_index, ANNOTATION_DATA
    user_id = request.session.get('_id', None)

    if not ANNOTATION_DATA[user_id]:
        raise HTTPException(status_code=404, detail="No annotations available to download.")

    user_dir = get_user_directory(user_id)
    # Convert the dataset to Dataset
    data = {"audio": [], "transcription": [], "speaker": []}
    for item in ANNOTATION_DATA[user_id]:
        data['audio'].append(os.path.join(user_dir, item['filename']))
        data['transcription'].append(item['transcription'])
        data['speaker'].append(item['speaker'])
    # print(data)
    ds = Dataset.from_dict(data).cast_column('audio', Audio(sampling_rate=16000))
    dataset_dir = os.path.join(user_dir, 'dataset')
    ds.save_to_disk(dataset_dir)

    # Write the content to a temporary file
    zip_dir = os.path.join(user_dir, 'final')
    shutil.make_archive(zip_dir, 'zip', dataset_dir)
    # Create a temporary file path
    temp_file = f'{zip_dir}.zip'

    def cleanup_file():
        try:
            shutil.rmtree(user_dir)
            os.makedirs(user_dir)
        except Exception as e:
            print(f"Error deleting directory {user_dir}: {e}")
        ANNOTATION_DATA[user_id] = []
        current_index[user_id] = -1

    # Return the file, which will be deleted after being sent
    return FileResponse(
        path=temp_file,
        filename='annotated_data.zip',
        media_type="application/zip",
        background=BackgroundTask(cleanup_file)
    )