Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify, send_from_directory
|
| 2 |
+
from diffusers import DiffusionPipeline
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Load the AI text-to-video model
|
| 8 |
+
pipe = DiffusionPipeline.from_pretrained("ali-vilab/text-to-video-ms-1.7b")
|
| 9 |
+
|
| 10 |
+
# Ensure static folder exists for saving videos
|
| 11 |
+
os.makedirs("static", exist_ok=True)
|
| 12 |
+
|
| 13 |
+
@app.route("/")
|
| 14 |
+
def home():
|
| 15 |
+
return render_template("index.html")
|
| 16 |
+
|
| 17 |
+
@app.route("/generate", methods=["POST"])
|
| 18 |
+
def generate():
|
| 19 |
+
data = request.json
|
| 20 |
+
prompt = data.get("prompt", "")
|
| 21 |
+
|
| 22 |
+
if not prompt:
|
| 23 |
+
return jsonify({"error": "No prompt provided"}), 400
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Generate video using AI model
|
| 27 |
+
video = pipe(prompt).videos[0]
|
| 28 |
+
video_path = "static/generated_video.mp4"
|
| 29 |
+
video.save(video_path)
|
| 30 |
+
|
| 31 |
+
return jsonify({"video_url": video_path})
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return jsonify({"error": str(e)}), 500
|
| 34 |
+
|
| 35 |
+
@app.route("/static/<path:filename>")
|
| 36 |
+
def serve_static(filename):
|
| 37 |
+
return send_from_directory("static", filename)
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
app.run(debug=True)
|
| 41 |
+
|