Download flask_app.py from wegag/max_ai: direct link, hf CLI and curl.
- Browser
- Download file 3.77 kB
-
https://huggingface.co/spaces/wegag/max_ai/resolve/main/flask_app.py
- Command line
-
hf download hf://spaces/wegag/max_ai/flask_app.py
-
curl -L -o flask_app.py https://huggingface.co/spaces/wegag/max_ai/resolve/main/flask_app.py
3.77 kB
| from flask import Flask, jsonify, send_from_directory, url_for | |
| import threading | |
| import os | |
| import json | |
| app = Flask(__name__) | |
| # Directories for videos and metadata | |
| VIDEO_DIR = "static/videos" | |
| DATA_DIR = "static/data" | |
| os.makedirs(VIDEO_DIR, exist_ok=True) | |
| os.makedirs(DATA_DIR, exist_ok=True) | |
| DATA_FILE = os.path.join(DATA_DIR, "metadata.json") | |
| def background_generate(): | |
| try: | |
| from utils import clean_script, get_images_paths, delete_all_images | |
| from voice import generate_voice_and_speed_it | |
| from content import get_youtube_shorts_script | |
| from ai import generate_youtube_metedata | |
| from timestamp import generate_srt_file | |
| from image import generate_multiple_flux_images | |
| from video import generate_video | |
| from auth import generate_natural_reader_access_token | |
| from accounts import natural_reader_accounts_ref_tokens as ref_tokens | |
| # Multiply the reference tokens | |
| extended_ref_tokens = ref_tokens * 5 | |
| # Set the initial token index | |
| i = 2 | |
| # Generate Natural Reader access token | |
| ref_token = extended_ref_tokens[i] | |
| natural_readers_access_token = generate_natural_reader_access_token(ref_token, i) | |
| # Delete all existing images | |
| delete_all_images() | |
| # Get and clean the YouTube shorts script | |
| cleaned_script = clean_script(get_youtube_shorts_script()) | |
| print(i, "Got Script") | |
| # Generate voice-over | |
| generate_voice_and_speed_it(cleaned_script, natural_readers_access_token, i) | |
| print(i, "Got VoiceOver") | |
| # Generate images | |
| generate_multiple_flux_images(cleaned_script) | |
| print(i, "Generated Images") | |
| # Generate SRT file | |
| generate_srt_file() | |
| # Get YouTube metadata | |
| youtube_metadata = generate_youtube_metedata(cleaned_script) | |
| title = youtube_metadata['title'] | |
| description = youtube_metadata['description'] | |
| print(i, "Got Title and Description from AI") | |
| print("Title :", title) | |
| print("Description :", description) | |
| # Save metadata to a JSON file | |
| metadata = {"title": title, "description": description} | |
| with open(DATA_FILE, "w") as f: | |
| json.dump(metadata, f) | |
| print("Metadata saved to file.") | |
| # Generate video | |
| video_path = generate_video(get_images_paths(), title, description) | |
| print(i, "Generated Video") | |
| # Move video to the static/videos directory | |
| video_filename = os.path.basename(video_path) | |
| target_video_path = os.path.join(VIDEO_DIR, video_filename) | |
| os.rename(video_path, target_video_path) | |
| print("Video generation completed successfully.") | |
| except Exception as e: | |
| print(f"Error during video generation: {e}") | |
| def generate(): | |
| # Start the background thread for video generation | |
| thread = threading.Thread(target=background_generate) | |
| thread.start() | |
| # Return an immediate success response | |
| return jsonify({"status": "success", "message": "Video generation started in the background"}), 200 | |
| def get_metadata(): | |
| # Check if the metadata file exists | |
| if os.path.exists(DATA_FILE): | |
| with open(DATA_FILE, "r") as f: | |
| metadata = json.load(f) | |
| return jsonify({"status": "success", "metadata": metadata}), 200 | |
| else: | |
| return jsonify({"status": "error", "message": "Metadata not available"}), 404 | |
| def serve_video(filename): | |
| return send_from_directory(VIDEO_DIR, filename) | |
| def home(): | |
| return "Home" | |
| if __name__ == '__main__': | |
| app.run(debug=True, host="0.0.0.0", port=7860) | |