Spaces:
Paused
Paused
Update app.py
Browse files123452378910111213141516171819202122232425262728293031456347893710113940411213434445461415164849505152171854551920212223242557585960616263646566676869702627282930313272333435363777787980818283848586383940414243899091929394959697989910010110210310410544454647481071081091101111121131141151161174950515211912012112212312412553541271281295556575859601311321331341351366113813962import gradio as grimport torchimport subprocessimport osfrom pathlib import Pathfrom gradio_client import Client, handle_fileimport shutil
def setup_hallo(): """Setup Hallo2 on first run""" if Path("hallo2_installed.txt").exists(): return True try: print("Installing Hallo2...") # Clone repo subprocess.run("git clone https://github.com/fudan-generative-vision/hallo2.git /tmp/hallo2", shell=True, check=True) # Install requirements subprocess.run("pip install -q diffusers[torch] transformers av insightface onnxruntime-gpu", shell=True, check=True) # Download models subprocess.run("huggingface-cli download fudan-generative-ai/hallo2 --local-dir /tmp/hallo2/pretrained_models", shell=True, check=True) Path("hallo2_installed.txt").touch() print("β
Hallo2 setup complete!") return True except Exception as e: print(f"Setup error: {e}") return Falsefrom pathlib import Path
def generate_video(image, audio): """Generate lip-synced video""" """Use existing HF Space API""" try: if not image or not audio: return None, "β Please upload both image and audio!" return None, "β Please upload both files!" # Setup on first run if not setup_hallo(): return None, "β Setup failed" print("π Connecting to API...") # Prepare paths output_dir = Path("/tmp/outputs") output_dir.mkdir(exist_ok=True) output_file = output_dir / "result.mp4" # Use the working MuseTalk space client = Client("TMElyralab/MuseTalk") # Copy inputs img_path = "/tmp/input_img.jpg" aud_path = "/tmp/input_audio.wav" shutil.copy(image, img_path) shutil.copy(audio, aud_path) print("π€ Uploading files...") # Run inference print("π¬ Generating video...") result = client.predict( audio_path=handle_file(audio), video_path=None, bbox_shift=0, api_name="/predict" ) cmd = f"""cd /tmp/hallo2 && python scripts/inference.py \ --source_image {img_path} \ --driving_audio {aud_path} \ --output {output_file} \ --pose_weight 1.0 \ --face_weight 1.0 \ --lip_weight 1.0""" result = subprocess.run(cmd, shell=True, capture_output=True, text=True) if output_file.exists(): return str(output_file), "β
Video generated successfully!" # Result is a file path if result and Path(result).exists(): # Copy to local output output = "result.mp4" shutil.copy(result, output) return output, "β
Video generated successfully!" else: return None, f"β Generation failed. Error: {result.stderr[:200]}" return None, "β API returned no result" except Exception as e: return None, f"β Error: {str(e)}"
# Gradio Interfacewith gr.Blocks(theme=gr.themes.Soft(), title="AI Lip Sync") as app: gr.Markdown(""" # π€ AI Lip Sync Generator Upload a portrait image and audio to create a realistic lip-synced video! **β‘ Fast generation on T4 GPU (~30-60 seconds)** """)# Gradio UIwith gr.Blocks(theme=gr.themes.Soft()) as app: gr.Markdown("# π¬ AI Lip Sync Generator") gr.Markdown("Upload a face image and audio to create lip-synced video") with gr.Row(): with gr.Column(scale=1): gr.Markdown("### π€ Upload Files") image_input = gr.Image( type="filepath", label="Portrait Image (JPG/PNG)", height=300 ) audio_input = gr.Audio( type="filepath", label="Audio File (WAV/MP3)" ) generate_btn = gr.Button( "π Generate Lip-Synced Video", variant="primary", size="lg" ) with gr.Column(): img = gr.Image(type="filepath", label="π· Face Image") aud = gr.Audio(type="filepath", label="π΅ Audio File") btn = gr.Button("π Generate Video", variant="primary") with gr.Column(scale=1): gr.Markdown("### πΉ Output") video_output = gr.Video( label="Generated Video", height=400 ) status_output = gr.Textbox( label="Status", lines=3, interactive=False ) with gr.Column(): vid = gr.Video(label="πΉ Result") status = gr.Textbox(label="Status", lines=2) gr.Markdown(""" --- ### π‘ Tips: - Use clear, front-facing portrait images - Best resolution: 512x512 or higher - Audio length: Up to 60 seconds recommended - First generation will download models (~2GB) btn.click(generate_video, [img, aud], [vid, status]) ### β±οΈ Performance: - First run: ~3-5 minutes (model download) - Subsequent runs: ~30-60 seconds per video gr.Markdown(""" ### π‘ Notes: - Uses MuseTalk API (no local installation needed) - Processing time: 30-90 seconds - Best with clear front-facing images """) generate_btn.click( fn=generate_video, inputs=[image_input, audio_input], outputs=[video_output, status_output] )
if __name__ == "__main__": app.launch()app.launch()app.py+39-116