File size: 2,176 Bytes
10dbddc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, render_template, jsonify
import os, re
from whisper_tts import WhisperTTS
from ollama_chatbotTTS import OllamaChat
from text_to_speech import TextToSpeech
from sync_audio_video import AudioVideoSync

app = Flask(__name__, static_folder='.', static_url_path='')

THUMBNAILS_DIR = "thumbnails"
VIDEO_DIR = "sample_video"
UPLOAD_DIR = "uploads"

def get_thumbnail_images():
    if not os.path.exists(THUMBNAILS_DIR):
        return []
    return [os.path.splitext(f)[0] for f in os.listdir(THUMBNAILS_DIR) if f.lower().endswith((".png",".jpg",".jpeg"))]

@app.route('/')
def index():
    avatars = get_thumbnail_images()
    return render_template('index.html', avatars=avatars)

@app.route('/transcribe', methods=['POST'])
def transcribe():
    f = request.files.get('audio')
    if f:
        os.makedirs(UPLOAD_DIR, exist_ok=True)
        path = os.path.join(UPLOAD_DIR, f.filename)
        f.save(path)
        text = WhisperTTS().transcribe_audio(path)
        return jsonify(text=text)
    return jsonify(text="")

@app.route('/chat', methods=['POST'])
def chat():
    data = request.get_json()
    text = data.get('text', "")
    r = OllamaChat().get_response(text)
    resp = re.sub(r"<think>|</think>", "", r).strip()
    return jsonify(response=resp)

@app.route('/tts', methods=['POST'])
def tts():
    data = request.get_json()
    text = data.get('text', "")
    if not text:
        return jsonify(audio_url="")
    path = TextToSpeech().synthesize(text)
    return jsonify(audio_url="/" + path)

@app.route('/sync', methods=['POST'])
def sync_audio_video():
    data = request.get_json()
    avatar = data.get('avatar', "")
    audio_url = data.get('audio_url', "")
    audio_path = audio_url.lstrip('/')
    vid = None
    for v in os.listdir(VIDEO_DIR):
        if os.path.splitext(v)[0].lower() == avatar.lower():
            vid = os.path.join(VIDEO_DIR, v)
            break
    if not vid or not audio_path:
        return jsonify(video_url="")
    out = AudioVideoSync().sync_audio_video(vid, audio_path)
    return jsonify(video_url="/" + out)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860, debug=True)