Spaces:
Sleeping
Sleeping
| import os | |
| import sys | |
| sys.path.append('/app/SadTalker/src') | |
| from flask import Flask, render_template, request, jsonify, send_from_directory | |
| app = Flask(__name__, static_folder='static', static_url_path='/static') | |
| # Initialize SadTalker with proper import | |
| try: | |
| from inference import SadTalker | |
| sadtalker = SadTalker( | |
| checkpoint_path="/app/SadTalker/checkpoints", | |
| config_path="/app/SadTalker/src/config", | |
| device="cpu" | |
| ) | |
| except ImportError: | |
| print("Warning: SadTalker not properly initialized") | |
| sadtalker = None | |
| def home(): | |
| return render_template('index.html') | |
| def generate(): | |
| if 'image' not in request.files: | |
| return jsonify({"error": "No image uploaded"}), 400 | |
| image = request.files['image'] | |
| 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: | |
| # 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 | |
| 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 | |
| def static_files(filename): | |
| return send_from_directory(app.static_folder, filename) | |
| if __name__ == '__main__': | |
| os.makedirs('static/uploads', exist_ok=True) | |
| app.run(host='0.0.0.0', port=7860) |