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 
 
 
 
 
 
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
from main import *
from tts_api import *
from stt_api import *
from sentiment_api import *
from imagegen_api import *
from musicgen_api import *
from translation_api import *
from codegen_api import *
from text_to_video_api import *
from summarization_api import *
from image_to_3d_api import *
from flask import Flask, request, jsonify, Response, send_file, stream_with_context
from flask_cors import CORS
import torch
import torch.nn.functional as F
import torchaudio
import numpy as np
from PIL import Image
import io
import tempfile
import queue
import json
import base64
from markupsafe import Markup
from markupsafe import escape

app = Flask(__name__)
CORS(app)

html_code = """<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Conversational Avatar</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"/>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f0f0f0;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
        }
        .container {
            width: 95%;
            max-width: 900px;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            margin-top: 20px;
            margin-bottom: 20px;
            display: flex;
            flex-direction: column;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
        }
        .header h1 {
            font-size: 2em;
            color: #333;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group textarea, .form-group input[type="text"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
            box-sizing: border-box;
        }
        button, #recordButton, #stopButton {
            padding: 10px 15px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: white;
            font-size: 18px;
            cursor: pointer;
            transition: background-color 0.3s ease;
            margin-right: 5px;
        }
        button:hover, #recordButton:hover, #stopButton:hover {
            background-color: #0056b3;
        }
        #output {
            margin-top: 20px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f9f9f9;
            white-space: pre-wrap;
            word-break: break-word;
            overflow-y: auto;
            max-height: 300px;
        }
        #videoOutput {
            margin-top: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            overflow: hidden;
        }
        #videoOutput video {
            width: 100%;
            display: block;
        }
        #animatedText {
            position: fixed;
            top: 20px;
            left: 20px;
            font-size: 1.5em;
            color: rgba(0, 0, 0, 0.1);
            pointer-events: none;
            z-index: -1;
        }
        #transcriptionOutput {
            margin-top: 10px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f9f9f9;
            font-size: 14px;
            word-break: break-word;
        }
        @media (max-width: 768px) {
            .container {
                width: 98%;
                margin-top: 10px;
                margin-bottom: 10px;
                padding: 15px;
            }
            .header h1 {
                font-size: 1.8em;
            }
            .form-group textarea, .form-group input[type="text"] {
                font-size: 14px;
                padding: 8px;
            }
            button, #recordButton, #stopButton {
                font-size: 16px;
                padding: 8px 12px;
            }
            #output, #transcriptionOutput {
                font-size: 14px;
                padding: 10px;
                margin-top: 15px;
            }
        }
    </style>
</head>
<body>
<div id="animatedText" class="animated-text animate__animated animate__fadeIn animate__infinite infinite">AI POWERED</div>
<div class="container">
    <div class="header animate__animated animate__fadeInDown">
        <h1>Conversational Avatar</h1>
    </div>
    <div class="form-group animate__animated animate__fadeInLeft">
        <textarea id="textInput" rows="3" placeholder="Or type your request here"></textarea>
    </div>
    <div class="form-group animate__animated animate__fadeInRight" style="text-align: center;">
        <button onclick="generateResponse()" class="animate__animated animate__fadeInUp">Generate Avatar Response</button>
    </div>

    <div style="text-align: center; margin-bottom: 15px;">
        <button id="recordButton" class="animate__animated animate__fadeInUp"><i class="fas fa-microphone"></i> Start Recording</button>
        <button id="stopButton" class="animate__animated animate__fadeInUp" disabled><i class="fas fa-stop-circle"></i> Stop Recording</button>
    </div>

    <div id="transcriptionOutput" class="animate__animated animate__fadeIn">
        <strong>Transcription:</strong>
        <span id="transcriptionText"></span>
    </div>

    <div id="output" class="animate__animated animate__fadeIn">
        <strong>Response:</strong><br>
        <span id="responseText"></span>
    </div>

    <div id="videoOutput" class="animate__animated animate__fadeIn">
        <video id="avatarVideo" controls></video>
    </div>
</div>

<script>
    let mediaRecorder;
    let audioChunks = [];
    let lastResponse = "";
    let accumulatedText = "";
    let eventSource = null;
    let audioURL;

    const recordButton = document.getElementById('recordButton');
    const stopButton = document.getElementById('stopButton');
    const transcriptionTextSpan = document.getElementById('transcriptionText');
    const responseTextSpan = document.getElementById('responseText');
    const avatarVideoPlayer = document.getElementById('avatarVideo');
    const textInputField = document.getElementById('textInput');

    recordButton.onclick = async () => {
        try {
            const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
            mediaRecorder = new MediaRecorder(stream);
            audioChunks = [];

            mediaRecorder.ondataavailable = event => {
                audioChunks.push(event.data);
            };

            mediaRecorder.onstop = async () => {
                const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
                const formData = new FormData();
                formData.append('audio', audioBlob, 'recording.wav');

                transcriptionTextSpan.innerText = "Transcribing...";
                responseTextSpan.innerText = "";
                avatarVideoPlayer.src = "";

                try {
                    const sttResponse = await fetch('/api/v1/stt', {
                        method: 'POST',
                        body: formData
                    });

                    if (!sttResponse.ok) {
                        throw new Error(\`HTTP error! status: ${sttResponse.status}\`);
                    }

                    const sttData = await sttResponse.json();
                    const transcribedText = sttData.text;
                    transcriptionTextSpan.innerText = transcribedText || "Transcription failed.";

                    if (transcribedText) {
                        await generateAvatarVideoResponse(transcribedText);
                    }

                } catch (error) {
                    console.error("STT or subsequent error:", error);
                    transcriptionTextSpan.innerText = "Transcription error.";
                    responseTextSpan.innerText = "Error processing audio.";
                } finally {
                    recordButton.disabled = false;
                    stopButton.disabled = true;
                }
            };

            recordButton.disabled = true;
            stopButton.disabled = false;
            transcriptionTextSpan.innerText = "Recording...";
            mediaRecorder.start();

        } catch (error) {
            console.error("Error accessing microphone:", error);
            transcriptionTextSpan.innerText = "Microphone access denied or error.";
            recordButton.disabled = false;
            stopButton.disabled = true;
        }
    };

    stopButton.onclick = () => {
        if (mediaRecorder && mediaRecorder.state === "recording") {
            transcriptionTextSpan.innerText = "Processing...";
            mediaRecorder.stop();
            recordButton.disabled = true;
            stopButton.disabled = true;
        }
    };

    async function generateResponse() {
        const inputText = textInputField.value;
        if (!inputText.trim()) {
            alert("Please enter text or record audio.");
            return;
        }
        transcriptionTextSpan.innerText = inputText;
        await generateAvatarVideoResponse(inputText);
    }


    async function generateAvatarVideoResponse(inputText) {
        responseTextSpan.innerText = "Generating response...";
        avatarVideoPlayer.src = "";
        accumulatedText = "";
        lastResponse = "";

        const temp = 0.7;
        const top_k_val = 40;
        const top_p_val = 0.0;
        const repetition_penalty_val = 1.2;

        const requestData = {
            text: inputText,
            temp: temp,
            top_k: top_k_val,
            top_p: top_p_val,
            reppenalty: repetition_penalty_val
        };

        if (eventSource) {
            eventSource.close();
        }

        eventSource = new EventSource('/api/v1/generate_stream?' + new URLSearchParams(requestData).toString());

        eventSource.onmessage = async function(event) {
            if (event.data === "<END_STREAM>") {
                eventSource.close();
                const currentResponse = accumulatedText.replace("<|endoftext|>", "").replace(/\s+(?=[.,,。])/g, '').trim();
                if (currentResponse === lastResponse.trim()) {
                    accumulatedText = "**Response is repetitive. Please try again or rephrase your query.**";
                } else {
                    lastResponse = currentResponse;
                }
                responseTextSpan.innerHTML = marked.parse(accumulatedText);

                try {
                    const ttsResponse = await fetch('/api/v1/tts', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ text: currentResponse })
                    });

                    if (!ttsResponse.ok) {
                        throw new Error(\`TTS HTTP error! status: ${ttsResponse.status}\`);
                    }

                    const ttsBlob = await ttsResponse.blob();
                    audioURL = URL.createObjectURL(ttsBlob);


                    const sadTalkerResponse = await fetch('/api/v1/sadtalker', {
                        method: 'POST',
                        body: new URLSearchParams({
                            'source_image': './examples/source_image/full_body_female.png',
                            'driven_audio': audioURL,
                            'preprocess': 'full',
                            'still_mode': false,
                            'use_enhancer': true
                        })
                    });

                    if (!sadTalkerResponse.ok) {
                        throw new Error(\`SadTalker HTTP error! status: ${sadTalkerResponse.status}\`);
                    }

                    const sadTalkerData = await sadTalkerResponse.json();
                    const videoURL = sadTalkerData.video_url;
                    avatarVideoPlayer.src = videoURL;


                } catch (ttsError) {
                    console.error("TTS or SadTalker error:", ttsError);
                    responseTextSpan.innerHTML += "<br><br>Error generating audio or video avatar.";
                }

                return;
            }
            accumulatedText += event.data;
            let partialText = accumulatedText.replace("<|endoftext|>", "").replace(/\s+(?=[.,,。])/g, '').trim();
            responseTextSpan.innerHTML = marked.parse(partialText);
        };

        eventSource.onerror = function(error) {
            console.error("SSE error", error);
            eventSource.close();
            responseTextSpan.innerText = "Error generating response stream.";
        };

        const outputDiv = document.getElementById("output");
        outputDiv.classList.add("show");
    }


</script>
</body>
</html>
"""

feedback_queue = queue.Queue()


@app.route("/")
def index():
    return html_code

@app.route("/api/v1/generate_stream", methods=["GET"])
def generate_stream():
    text = request.args.get("text", "")
    temp = float(request.args.get("temp", 0.7))
    top_k = int(request.args.get("top_k", 40))
    top_p = float(request.args.get("top_p", 0.0))
    reppenalty = float(request.args.get("reppenalty", 1.2))
    response_queue = queue.Queue()
    reasoning_queue.put({
        'text_input': text,
        'temperature': temp,
        'top_k': top_k,
        'top_p': top_p,
        'repetition_penalty': reppenalty,
        'response_queue': response_queue
    })
    @stream_with_context
    def event_stream():
        while True:
            output = response_queue.get()
            if "error" in output:
                yield "data: <ERROR>\n\n"
                break
            text_chunk = output.get("text")
            if text_chunk:
                for word in text_chunk.split(' '):
                    clean_word = word.strip()
                    if clean_word:
                        yield "data: " + clean_word + "\n\n"
                yield "data: <END_STREAM>\n\n"
                break
    return Response(event_stream(), mimetype="text/event-stream")

@app.route("/api/v1/generate", methods=["POST"])
def generate():
    data = request.get_json()
    text = data.get("text", "")
    temp = float(data.get("temp", 0.7))
    top_k = int(data.get("top_k", 40))
    top_p = float(data.get("top_p", 0.0))
    reppenalty = float(data.get("reppenalty", 1.2))
    response_queue = queue.Queue()
    reasoning_queue.put({
        'text_input': text,
        'temperature': temp,
        'top_k': top_k,
        'top_p': top_p,
        'repetition_penalty': reppenalty,
        'response_queue': response_queue
    })
    output = response_queue.get()
    if "error" in output:
        return jsonify({"error": output["error"]}), 500
    result_text = output.get("text", "").strip()
    return jsonify({"response": result_text})

@app.route("/api/v1/feedback", methods=["POST"])
def feedback():
    data = request.get_json()
    feedback_text = data.get("feedback_text")
    correct_category = data.get("correct_category")
    if feedback_text and correct_category:
        feedback_queue.put((feedback_text, correct_category))
        return jsonify({"status": "feedback received"})
    return jsonify({"status": "feedback failed"}), 400

@app.route("/api/v1/tts", methods=["POST"])
def tts_api():
    return tts_route()

@app.route("/api/v1/stt", methods=["POST"])
def stt_api():
    return stt_route()

@app.route("/api/v1/sentiment", methods=["POST"])
def sentiment_api():
    return sentiment_route()

@app.route("/api/v1/imagegen", methods=["POST"])
def imagegen_api():
    return imagegen_route()

@app.route("/api/v1/musicgen", methods=["POST"])
def musicgen_api():
    return musicgen_route()

@app.route("/api/v1/translation", methods=["POST"])
def translation_api():
    return translation_route()

@app.route("/api/v1/codegen", methods=["POST"])
def codegen_api():
    return codegen_route()

@app.route("/api/v1/text_to_video", methods=["POST"])
def text_to_video_api():
    return text_to_video_route()

@app.route("/api/v1/summarization", methods=["POST"])
def summarization_api():
    return summarization_route()

@app.route("/api/v1/image_to_3d", methods=["POST"])
def image_to_3d_api():
    return image_to_3d_route()

@app.route("/api/v1/sadtalker", methods=["POST"])
def sadtalker():
    from sadtalker_api import router as sadtalker_router
    return sadtalker_router.create_video()

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)