Spaces:
Running
Running
File size: 1,628 Bytes
a1320d8 134c911 a1320d8 134c911 a1320d8 b94cede a1320d8 |
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 |
import os
import sys
sys.path.append('/app/SadTalker/src')
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
# 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
@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 = request.form.get('text', '')
# 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 )
sadtalker.generate(
source_image=img_path,
driven_audio=audio_path,
result_dir='static/uploads',
still=True,
preprocess='crop',
enhancer='none' # Disable for CPU
)
return jsonify({
"video": f"/static/uploads/{os.path.basename(output_path)}"
})
if __name__ == '__main__':
os.makedirs('static/uploads', exist_ok=True)
app.run(host='0.0.0.0', port=7860) |