File size: 6,150 Bytes
726ec90
3d7f69e
97e7d36
 
726ec90
 
3d7f69e
 
2ec9baa
 
726ec90
 
6831f1f
3d7f69e
97e7d36
2ec9baa
 
 
 
 
 
 
 
 
 
97e7d36
6831f1f
97e7d36
726ec90
6831f1f
726ec90
97e7d36
 
 
 
 
 
 
 
 
 
 
 
3d7f69e
97e7d36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
726ec90
6831f1f
 
 
 
 
 
 
 
 
 
 
 
726ec90
 
6831f1f
726ec90
 
 
 
 
 
 
6831f1f
726ec90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d7f69e
726ec90
 
 
 
 
 
 
6831f1f
726ec90
 
3d7f69e
97e7d36
 
 
 
 
 
 
 
 
 
 
 
2ec9baa
97e7d36
 
 
726ec90
6831f1f
726ec90
 
6831f1f
3d7f69e
 
 
 
 
6831f1f
 
726ec90
97e7d36
 
 
 
 
2ec9baa
 
 
 
97e7d36
 
 
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
import io
from flask import Flask, Response, send_from_directory, jsonify, request, abort
import os
from flask_cors import CORS
from multiprocessing import Queue
import base64
from typing import Any, List, Dict, Tuple
from multiprocessing import Queue
import logging
import sys

from server.AudioTranscriber import AudioTranscriber
from server.ActionProcessor import ActionProcessor
from server.StandaloneApplication import StandaloneApplication

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    handlers=[logging.StreamHandler(sys.stdout)],
)

# Get a logger for your app
logger = logging.getLogger(__name__)

# Use a directory in the user's home folder for static files
STATIC_DIR = "/app/server/static" if os.getenv("DEBUG") != "true" else "./server"

audio_queue: "Queue[io.BytesIO]" = Queue()
text_queue: "Queue[str]" = Queue()
action_queue: "Queue[str]" = Queue()

app = Flask(__name__, static_folder=STATIC_DIR)

_ = CORS(
    app,
    origins=["*"],
    methods=["GET", "POST", "OPTIONS"],
    allow_headers=["Content-Type", "Authorization"],
)


@app.after_request
def add_header(response: Response):
    # Add permissive CORS headers
    response.headers["Access-Control-Allow-Origin"] = "*"
    response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS"
    response.headers["Access-Control-Allow-Headers"] = "*"  # Allow all headers
    # Cross-origin isolation headers
    response.headers["Cross-Origin-Embedder-Policy"] = "require-corp"
    response.headers["Cross-Origin-Opener-Policy"] = "same-origin"
    response.headers["Cross-Origin-Resource-Policy"] = "cross-origin"
    return response


@app.route("/")
def serve_index():
    # Handle logs=container query parameter
    if request.args.get("logs") == "container":
        files = (
            os.listdir(app.static_folder) if os.path.exists(app.static_folder) else []
        )
        return jsonify(
            {
                "static_folder": app.static_folder,
                "exists": os.path.exists(app.static_folder),
                "files": files,
                "pwd": os.getcwd(),
                "user": os.getenv("USER"),
            }
        )

    try:
        response = send_from_directory(app.static_folder, "index.html")
        response.headers["Cross-Origin-Opener-Policy"] = "same-origin"
        response.headers["Cross-Origin-Embedder-Policy"] = "require-corp"
        return response
    except FileNotFoundError:
        abort(
            404,
            description=f"Static folder or index.html not found. Static folder: {app.static_folder}",
        )


@app.route("/api/data", methods=["GET"])
def get_data():
    return jsonify({"message": "Voici vos données", "status": "success"})


@app.route("/api/process", methods=["POST"])
def process_data():
    try:
        # Check content type
        content_type = request.headers.get("Content-Type", "")

        # Handle different content types
        if "application/json" in content_type:
            data = request.get_json()
            audio_base64 = data.get("audio_chunk")
        elif "multipart/form-data" in content_type:
            audio_base64 = request.form.get("audio_chunk")
        else:
            # Try to get raw data
            audio_base64 = request.get_data().decode("utf-8")

        # Validate the incoming data
        if not audio_base64:
            return (
                jsonify({"error": "Missing audio_chunk in request", "status": "error"}),
                400,
            )

        # Decode the base64 audio chunk
        try:
            audio_chunk = base64.b64decode(audio_base64)
        except Exception as e:
            return (
                jsonify(
                    {
                        "error": f"Failed to decode audio chunk: {str(e)}",
                        "status": "error",
                    }
                ),
                400,
            )

        # Put the audio chunk in the queue for processing
        audio_queue.put(io.BytesIO(audio_chunk))

        return jsonify(
            {
                "message": "Audio chunk received and queued for processing",
                "status": "success",
            }
        )
    except Exception as e:
        return (
            jsonify(
                {"error": f"Failed to process request: {str(e)}", "status": "error"}
            ),
            500,
        )


@app.route("/api/actions", methods=["GET"])
def get_actions() -> Tuple[Response, int]:
    """Retrieve and clear all pending actions from the queue"""
    actions: List[Dict[str, Any]] = []

    # Drain the queue into our actions list
    while not action_queue.empty():
        try:
            actions.append(action_queue.get_nowait())
        except Exception:
            break

    return jsonify({"actions": actions, "status": "success"}), 200


@app.route("/<path:path>")
def serve_static(path: str):
    try:
        return send_from_directory(app.static_folder, path)
    except FileNotFoundError:
        abort(404, description=f"File {path} not found in static folder")


if __name__ == "__main__":
    if os.path.exists(app.static_folder):
        logger.info(f"Static folder contents: {os.listdir(app.static_folder)}")

    os.makedirs(app.static_folder, exist_ok=True)

    # Start the audio transcriber thread
    transcriber = AudioTranscriber(audio_queue, text_queue)
    transcriber.start()

    # Start the action processor thread
    MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
    if not MISTRAL_API_KEY:
        raise ValueError("MISTRAL_API_KEY is not set")

    action_processor = ActionProcessor(text_queue, action_queue, MISTRAL_API_KEY)
    action_processor.start()

    options: Any = {
        "bind": "0.0.0.0:7860",
        "workers": 3,
        "worker_class": "sync",
        "timeout": 120,
        "forwarded_allow_ips": "*",
        "accesslog": "-",  # Log to stdout
        "errorlog": "-",  # Log to stderr
        "capture_output": True,
        "enable_stdio_inheritance": True,
    }

    StandaloneApplication(app, options).run()