File size: 4,301 Bytes
a1320d8
134c911
 
 
242a970
a1320d8
4e1bce5
a1320d8
134c911
9f2a24a
134c911
9f2a24a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134c911
a1320d8
 
 
 
 
 
 
 
 
 
 
f22d3ca
a1320d8
 
242a970
 
a1320d8
242a970
 
a1320d8
242a970
7543760
242a970
 
 
 
 
 
 
 
 
4e1bce5
242a970
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f22d3ca
242a970
 
 
 
 
 
 
 
 
a1320d8
 
9f2a24a
 
 
 
 
 
 
 
 
 
 
 
 
7543760
9f2a24a
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
import os
import sys
sys.path.append('/app/SadTalker/src')

from flask import Flask, render_template, request, jsonify, send_from_directory

app = Flask(__name__)

# Initialize SadTalker with proper import
sadtalker = None
try:
    # Try different import methods for SadTalker
    sys.path.append('/app/SadTalker')
    from src.test_audio2coeff import Audio2Coeff
    from src.facerender.animate import AnimateFromCoeff
    from src.generate_batch import get_data
    from src.generate_facerender_batch import get_facerender_data
    from src.utils.preprocess import CropAndExtract
    
    print("SadTalker components imported successfully")
    # Create a simple wrapper class
    class SimpleSadTalker:
        def __init__(self):
            self.initialized = True
            
        def generate(self, source_image, driven_audio, result_dir, **kwargs):
            # Placeholder implementation - you may need to implement actual SadTalker logic
            print(f"Generating video from {source_image} and {driven_audio}")
            # For now, just copy the source image as a placeholder
            import shutil
            output_path = os.path.join(result_dir, 'output.mp4')
            # Create a dummy video file for testing
            with open(output_path, 'w') as f:
                f.write("dummy video content")
            return output_path
    
    sadtalker = SimpleSadTalker()
    print("SadTalker initialized successfully")
    
except Exception as e:
    print(f"Warning: SadTalker not properly initialized: {e}")
    sadtalker = None

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/generate', methods=['POST'])
def generate():
    if 'image' not in request.files:
        return jsonify({"error": "No image uploaded"}), 400
    
    image = request.files['image']
    # text 
    text = request.form.get('text', '')
    
    if not text.strip():
        return jsonify({"error": "No text provided"}), 400
    
    if not image.filename:
        return jsonify({"error": "No image selected"}), 400
    
    try:
        #make sure that file exist
        # Save files
        img_path = os.path.join('static/uploads', image.filename)
        audio_path = os.path.join('static/uploads', 'audio.wav')
        output_path = os.path.join('static/uploads', 'output.mp4')
        
        image.save(img_path)
        # Text-to-Speech (using gTTS)
        from gtts import gTTS
        tts = gTTS(text=text, lang='en')

        tts.save(audio_path)

        # Generate video (CPU optimized)
        if sadtalker:
            sadtalker.generate(
                source_image=img_path,
                driven_audio=audio_path,
                result_dir='static/uploads',
                still=True,
                preprocess='crop',
                enhancer='none'  # Disable for CPU
            )
        else:
            return jsonify({"error": "SadTalker not initialized"}), 500
        
        return jsonify({
            "video": f"/static/uploads/{os.path.basename(output_path)}"
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

# Debug route to check static files
@app.route('/debug/static')
def debug_static():
    static_files = []

    for root, dirs, files in os.walk('static'):
        for file in files:
            static_files.append(os.path.join(root, file))
    return jsonify({"static_files": static_files})

# Explicit static file route for debugging
@app.route('/static/<path:filename>')
def static_files(filename):
    return send_from_directory(app.static_folder, filename)

if __name__ == '__main__':
    # Create static directory with proper error handling
    try:
        os.makedirs('/app/static/uploads', exist_ok=True)
        print("Static directories created successfully")
    except PermissionError as e:
        print(f"Permission error creating directories: {e}")
        # Try alternative approach
        try:
            import subprocess
            subprocess.run(['mkdir', '-p', '/app/static/uploads'], check=True)
            subprocess.run(['chmod', '-R', '777', '/app/static'], check=True)
        except Exception as mkdir_error:
            print(f"Alternative directory creation failed: {mkdir_error}")

    app.run(host='0.0.0.0', port=7860, debug=True)