File size: 20,559 Bytes
132ace4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d70912d
132ace4
 
 
 
 
 
d70912d
132ace4
 
 
 
 
 
d70912d
132ace4
d70912d
132ace4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d70912d
 
132ace4
 
 
 
 
 
 
 
 
 
 
d70912d
132ace4
 
 
 
 
d70912d
132ace4
 
 
d70912d
132ace4
 
 
d70912d
132ace4
 
 
 
 
 
 
d70912d
132ace4
 
 
 
d70912d
132ace4
 
 
d70912d
132ace4
 
d70912d
132ace4
 
 
d70912d
132ace4
 
 
 
 
 
 
 
 
d70912d
132ace4
 
 
d70912d
132ace4
 
d70912d
132ace4
 
 
 
d70912d
132ace4
 
 
 
d70912d
132ace4
 
 
 
 
 
 
d70912d
132ace4
 
 
 
 
d70912d
132ace4
 
 
d70912d
 
 
 
 
132ace4
 
d70912d
 
 
 
 
132ace4
d70912d
132ace4
 
d70912d
 
132ace4
 
 
d70912d
132ace4
 
 
 
 
 
d70912d
132ace4
 
 
 
d70912d
132ace4
 
 
 
 
 
 
 
 
 
 
 
d70912d
132ace4
 
 
d70912d
 
132ace4
 
 
d70912d
132ace4
 
d70912d
132ace4
 
 
 
d70912d
 
132ace4
d70912d
 
 
 
 
132ace4
 
 
d70912d
132ace4
 
 
 
 
 
 
d70912d
132ace4
 
d70912d
132ace4
 
 
 
 
 
 
 
 
d70912d
132ace4
 
 
 
 
d70912d
132ace4
 
 
 
 
 
 
 
 
d70912d
132ace4
 
d70912d
132ace4
 
 
 
 
 
 
 
 
d70912d
132ace4
 
 
 
d70912d
132ace4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d70912d
 
 
 
 
 
 
132ace4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d70912d
 
 
132ace4
 
 
 
 
 
 
 
 
 
d70912d
 
132ace4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d70912d
132ace4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d70912d
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import os
import logging
import shutil
import re
import zipfile
import uvicorn
import asyncio
import threading
import time
from fastapi import FastAPI, Request, BackgroundTasks, HTTPException
from fastapi.responses import HTMLResponse, StreamingResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
from typing import Dict, Set, Tuple, List, Optional
from io import BytesIO
from pathlib import Path

# Configure data directories for Hugging Face Spaces
# HF Spaces can have persistent storage in the /data directory
DATA_DIR = '/data'
# Import the existing telegram bot functionality
# Define these directories here to ensure they're created before importing app.py
PENDING_DIR = os.path.join(DATA_DIR, 'pending')
PENDING_VOICE_DIR = os.path.join(PENDING_DIR, 'voice')
PENDING_TEXT_DIR = os.path.join(PENDING_DIR, 'text')
PROCESSED_DIR = os.path.join(DATA_DIR, 'processed')
PROCESSED_VOICE_DIR = os.path.join(PROCESSED_DIR, 'voice')
PROCESSED_TEXT_DIR = os.path.join(PROCESSED_DIR, 'text')

# Create directories if they don't exist
os.makedirs(PENDING_VOICE_DIR, exist_ok=True)
os.makedirs(PENDING_TEXT_DIR, exist_ok=True)
os.makedirs(PROCESSED_VOICE_DIR, exist_ok=True)
os.makedirs(PROCESSED_TEXT_DIR, exist_ok=True)

# Import from app.py now that we've defined the directories
try:
    from app import extract_sequence_number, get_files_by_sequence_number
except Exception as e:
    # Define fallback functions if import fails
    def extract_sequence_number(filename: str) -> int:
        """Extract sequence number from filename, e.g., 'Sound 100.wav' -> 100."""
        match = re.search(r'(\d+)', filename)
        if match:
            return int(match.group(1))
        return None

    def get_files_by_sequence_number():
        """Create dictionaries mapping sequence numbers to filenames."""
        voice_files = {}
        text_files = {}

        # Map voice files to sequence numbers
        for filename in os.listdir(PENDING_VOICE_DIR):
            if filename.endswith('.wav'):
                seq_num = extract_sequence_number(filename)
                if seq_num is not None:
                    voice_files[seq_num] = filename

        # Map text files to sequence numbers
        for filename in os.listdir(PENDING_TEXT_DIR):
            if filename.endswith('.txt'):
                seq_num = extract_sequence_number(filename)
                if seq_num is not None:
                    text_files[seq_num] = filename

        return voice_files, text_files

    logging.error(f"Error importing from app.py: {str(e)}")

# Enable logging
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)
logger = logging.getLogger(__name__)

app = FastAPI(title="Algerian Darija Transcription Service")

# Create templates instance
templates = Jinja2Templates(directory="templates")

# Telegram bot process
bot_process = None
bot_running = False
bot_start_time = None

# Function to start the bot in a separate process instead of a thread


def start_bot_process():
    from multiprocessing import Process
    global bot_process, bot_running

    # Create a new process for the bot
    bot_process = Process(target=run_bot_process)
    bot_process.daemon = True  # Process will terminate when main process ends
    bot_process.start()
    bot_running = True
    return bot_process.pid


def run_bot_process():
    """Function that runs in a separate process to start the bot"""
    import sys
    import os
    import asyncio

    try:
        # Import and run the main function from app.py
        from app import main

        # Create a new event loop for this process
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

        # Run the bot
        loop.run_until_complete(main())
        loop.run_forever()
    except Exception as e:
        sys.stderr.write(f"Error in bot process: {str(e)}\n")
        sys.exit(1)


@app.get("/", response_class=HTMLResponse)
async def get_root(request: Request):
    return templates.TemplateResponse("index.html", {"request": request, "bot_running": bot_running})


@app.post("/start-bot")
async def start_bot_endpoint(background_tasks: BackgroundTasks):
    global bot_running, bot_start_time

    if bot_running:
        return {"status": "error", "message": "Bot is already running"}

    # Check if TELEGRAM_TOKEN is set
    if not os.environ.get("TELEGRAM_TOKEN"):
        return {"status": "error", "message": "TELEGRAM_TOKEN environment variable is not set. Please configure it in Hugging Face Space settings."}

    try:
        # Start bot in a separate process
        pid = start_bot_process()
        bot_start_time = time.time()
        return {"status": "success", "message": f"Bot started successfully with PID {pid}"}
    except Exception as e:
        logger.error(f"Error starting bot: {str(e)}")
        return {"status": "error", "message": f"Failed to start bot: {str(e)}"}


@app.post("/stop-bot")
async def stop_bot_endpoint():
    global bot_process, bot_running, bot_start_time

    if not bot_running or bot_process is None:
        return {"status": "error", "message": "Bot is not running"}

    try:
        # Terminate the process
        bot_process.terminate()
        bot_process.join(timeout=5)  # Wait for process to terminate

        # If process didn't terminate, force kill it
        if bot_process.is_alive():
            bot_process.kill()
            bot_process.join()

        bot_running = False
        bot_start_time = None
        return {"status": "success", "message": "Bot stopped successfully"}
    except Exception as e:
        logger.error(f"Error stopping bot: {str(e)}")
        return {"status": "error", "message": f"Failed to stop bot: {str(e)}"}


@app.get("/status")
async def get_status():
    # Check if directories exist and create if needed
    os.makedirs(PENDING_VOICE_DIR, exist_ok=True)
    os.makedirs(PENDING_TEXT_DIR, exist_ok=True)
    os.makedirs(PROCESSED_VOICE_DIR, exist_ok=True)
    os.makedirs(PROCESSED_TEXT_DIR, exist_ok=True)

    # Count pending files
    pending_voice_count = len(
        [f for f in os.listdir(PENDING_VOICE_DIR) if f.endswith('.wav')])
    pending_text_count = len(
        [f for f in os.listdir(PENDING_TEXT_DIR) if f.endswith('.txt')])

    # Get sequence numbers
    voice_files, text_files = get_files_by_sequence_number()
    matching_pairs = len(
        set(voice_files.keys()).intersection(set(text_files.keys())))

    # Count processed files - Only count text files now as voice files are no longer saved to processed
    processed_voice_count = 0  # Set to 0 since we no longer save voice files
    processed_text_count = 0

    # Count processed files by user
    user_stats = {}

    # Process text files only (voice files are no longer saved to processed directory)
    for root, dirs, files in os.walk(PROCESSED_TEXT_DIR):
        txt_files = [f for f in files if f.endswith('.txt')]
        processed_text_count += len(txt_files)

        # Get user ID from directory path
        if os.path.basename(root).startswith("user_"):
            user_id = os.path.basename(root)
            if user_id not in user_stats:
                user_stats[user_id] = {"voice": 0, "text": 0}
            user_stats[user_id]["text"] += len(txt_files)

    # Calculate uptime if bot is running
    uptime = None
    if bot_running and bot_start_time:
        uptime = int(time.time() - bot_start_time)

    return {
        "bot_running": bot_running,
        "uptime_seconds": uptime,
        "pending_voice_count": pending_voice_count,
        "pending_text_count": pending_text_count,
        "matching_pairs": matching_pairs,
        "processed_voice_count": processed_voice_count,
        "processed_text_count": processed_text_count,
        "user_stats": user_stats,
        "is_huggingface": True
    }


@app.get("/list-users")
async def list_users():
    users = set()

    # Get users from text directories only (since voice files are no longer saved)
    for item in os.listdir(PROCESSED_TEXT_DIR):
        if item.startswith("user_"):
            users.add(item)

    return {"users": sorted(list(users))}


@app.get("/download/{user_id}")
async def download_processed_data(user_id: str):
    if not user_id.startswith("user_"):
        raise HTTPException(status_code=400, detail="Invalid user ID format")

    # Check if user exists - only check text directory now
    text_dir = os.path.join(PROCESSED_TEXT_DIR, user_id)

    if not os.path.exists(text_dir):
        raise HTTPException(
            status_code=404, detail=f"No data found for user {user_id}")

    # Create a zip file in memory
    zip_buffer = BytesIO()
    with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
        # Add text files only
        if os.path.exists(text_dir):
            for file_name in os.listdir(text_dir):
                file_path = os.path.join(text_dir, file_name)
                if os.path.isfile(file_path) and file_name.endswith('.txt'):
                    # Read the file and add it to the zip
                    with open(file_path, 'rb') as f:
                        zip_file.writestr(f"text/{file_name}", f.read())

    # Reset buffer position
    zip_buffer.seek(0)

    # Return the zip file as a response
    return StreamingResponse(
        zip_buffer,
        media_type="application/zip",
        headers={
            "Content-Disposition": f"attachment; filename={user_id}_processed_data.zip"
        }
    )


@app.get("/download-all")
async def download_all_processed_data():
    # Create a zip file in memory
    zip_buffer = BytesIO()
    with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
        # Add all processed text files only
        for root, dirs, files in os.walk(PROCESSED_TEXT_DIR):
            for file_name in files:
                if file_name.endswith('.txt'):
                    file_path = os.path.join(root, file_name)
                    # Get the relative path from the PROCESSED_TEXT_DIR
                    rel_path = os.path.relpath(file_path, PROCESSED_TEXT_DIR)
                    # Read the file and add it to the zip
                    with open(file_path, 'rb') as f:
                        zip_file.writestr(f"text/{rel_path}", f.read())

    # Reset buffer position
    zip_buffer.seek(0)

    # Return the zip file as a response
    return StreamingResponse(
        zip_buffer,
        media_type="application/zip",
        headers={
            "Content-Disposition": "attachment; filename=all_processed_data.zip"
        }
    )


@app.on_event("startup")
async def setup_app():
    # Ensure templates directory exists
    os.makedirs("templates", exist_ok=True)

    # Only create index.html if it doesn't exist
    if not os.path.exists("templates/index.html"):
        # Create a simple default template if needed
        with open("templates/index.html", "w") as f:
            f.write("""

<!DOCTYPE html>

<html>

<head>

    <title>Algerian Darija Transcription Service</title>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>

        body {

            font-family: Arial, sans-serif;

            max-width: 800px;

            margin: 0 auto;

            padding: 20px;

            line-height: 1.6;

        }

        .container {

            border: 1px solid #ddd;

            border-radius: 8px;

            padding: 20px;

            margin-bottom: 20px;

        }

        button {

            background-color: #4CAF50;

            color: white;

            padding: 8px 16px;

            border: none;

            border-radius: 4px;

            cursor: pointer;

            margin-right: 10px;

        }

        button.stop {

            background-color: #f44336;

        }

        button:disabled {

            background-color: #cccccc;

            cursor: not-allowed;

        }

        .status {

            margin-top: 20px;

        }

        table {

            width: 100%;

            border-collapse: collapse;

            margin-top: 10px;

        }

        th, td {

            border: 1px solid #ddd;

            padding: 8px;

            text-align: left;

        }

        th {

            background-color: #f2f2f2;

        }

        .users-container {

            margin-top: 20px;

        }

        .user-item {

            display: flex;

            justify-content: space-between;

            padding: 8px;

            border-bottom: 1px solid #eee;

        }

        .note {

            background-color: #f0f8ff;

            border-left: 4px solid #1e90ff;

            padding: 10px;

            margin: 10px 0;

            font-style: italic;

        }

    </style>

</head>

<body>

    <h1>Algerian Darija Transcription Service</h1>

    

    <div class="container">

        <h2>Bot Control</h2>

        <p>Start or stop the Telegram bot service.</p>

        <button id="startBot" onclick="startBot()">Start Bot</button>

        <button id="stopBot" class="stop" onclick="stopBot()" disabled>Stop Bot</button>

        <div id="botMessage" class="status"></div>

    </div>

    

    <div class="container">

        <h2>Status</h2>

        <div class="note">

            Note: Only corrected text files are saved to the processed directory. Voice files are removed after processing to save storage space.

        </div>

        <div id="statusInfo" class="status">Loading status...</div>

    </div>

    

    <div class="container">

        <h2>Users Data</h2>

        <div id="users" class="users-container">Loading users...</div>

    </div>

    

    <div class="container">

        <h2>Data Download</h2>

        <p>Download all processed corrected text files or data for specific users.</p>

        <button onclick="downloadAll()">Download All Text Data</button>

    </div>



    <script>

        // Check status when page loads

        document.addEventListener('DOMContentLoaded', function() {

            updateStatus();

            updateUsers();

            setInterval(updateStatus, 10000); // Update status every 10 seconds

        });

        

        function updateStatus() {

            fetch('/status')

                .then(response => response.json())

                .then(data => {

                    console.log('Status data:', data);

                    

                    // Update bot status

                    const botRunning = data.bot_running;

                    document.getElementById('startBot').disabled = botRunning;

                    document.getElementById('stopBot').disabled = !botRunning;

                    

                    // Format uptime

                    let uptimeDisplay = 'Not running';

                    if (data.uptime_seconds) {

                        const hours = Math.floor(data.uptime_seconds / 3600);

                        const minutes = Math.floor((data.uptime_seconds % 3600) / 60);

                        const seconds = data.uptime_seconds % 60;

                        uptimeDisplay = `${hours}h ${minutes}m ${seconds}s`;

                    }

                    

                    // Update status table

                    const statusHtml = `

                        <table>

                            <tr>

                                <th>Metric</th>

                                <th>Value</th>

                            </tr>

                            <tr>

                                <td>Bot Status</td>

                                <td>${botRunning ? '✅ Running' : '❌ Stopped'}</td>

                            </tr>

                            <tr>

                                <td>Uptime</td>

                                <td>${uptimeDisplay}</td>

                            </tr>

                            <tr>

                                <td>Pending Voice Files</td>

                                <td>${data.pending_voice_count}</td>

                            </tr>

                            <tr>

                                <td>Pending Text Files</td>

                                <td>${data.pending_text_count}</td>

                            </tr>

                            <tr>

                                <td>Matching Pending Pairs</td>

                                <td>${data.matching_pairs}</td>

                            </tr>

                            <tr>

                                <td>Processed Text Files</td>

                                <td>${data.processed_text_count}</td>

                            </tr>

                        </table>

                    `;

                    

                    document.getElementById('statusInfo').innerHTML = statusHtml;

                })

                .catch(error => {

                    console.error('Error fetching status:', error);

                    document.getElementById('statusInfo').innerHTML = 'Error fetching status';

                });

        }

        

        function updateUsers() {

            fetch('/list-users')

                .then(response => response.json())

                .then(data => {

                    console.log('Users data:', data);

                    const users = data.users;

                    

                    if (users.length === 0) {

                        document.getElementById('users').innerHTML = 'No users found.';

                        return;

                    }

                    

                    let usersHtml = '';

                    users.forEach(user => {

                        usersHtml += `

                            <div class="user-item">

                                <span>${user}</span>

                                <button onclick="downloadUser('${user}')">Download Text Data</button>

                            </div>

                        `;

                    });

                    

                    document.getElementById('users').innerHTML = usersHtml;

                })

                .catch(error => {

                    console.error('Error fetching users:', error);

                    document.getElementById('users').innerHTML = 'Error fetching users';

                });

        }

        

        function startBot() {

            document.getElementById('botMessage').innerHTML = 'Starting bot...';

            

            fetch('/start-bot', {

                method: 'POST',

            })

                .then(response => response.json())

                .then(data => {

                    document.getElementById('botMessage').innerHTML = data.message;

                    updateStatus();

                })

                .catch(error => {

                    console.error('Error starting bot:', error);

                    document.getElementById('botMessage').innerHTML = 'Error starting bot: ' + error;

                });

        }

        

        function stopBot() {

            document.getElementById('botMessage').innerHTML = 'Stopping bot...';

            

            fetch('/stop-bot', {

                method: 'POST',

            })

                .then(response => response.json())

                .then(data => {

                    document.getElementById('botMessage').innerHTML = data.message;

                    updateStatus();

                })

                .catch(error => {

                    console.error('Error stopping bot:', error);

                    document.getElementById('botMessage').innerHTML = 'Error stopping bot: ' + error;

                });

        }

        

        function downloadUser(userId) {

            window.location.href = `/download/${userId}`;

        }

        

        function downloadAll() {

            window.location.href = '/download-all';

        }

    </script>

</body>

</html>

            """)

# Entrypoint for running the server
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)