Merge branch 'main' of https://huggingface.co/spaces/gamora/ShortsAI
Browse files- .gitignore +2 -1
- grok_analyze.py +0 -2
- main.py +101 -0
- vercel.json +14 -0
.gitignore
CHANGED
|
@@ -24,4 +24,5 @@ resources_info.json
|
|
| 24 |
|
| 25 |
.DS_Store
|
| 26 |
|
| 27 |
-
/edited_videos/
|
|
|
|
|
|
| 24 |
|
| 25 |
.DS_Store
|
| 26 |
|
| 27 |
+
/edited_videos/
|
| 28 |
+
.vercel
|
grok_analyze.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import base64
|
| 3 |
|
| 4 |
-
from torch.cuda import temperature
|
| 5 |
-
|
| 6 |
from xai_sdk import Client
|
| 7 |
from xai_sdk.chat import user, image
|
| 8 |
from dotenv import load_dotenv
|
|
|
|
| 1 |
import os
|
| 2 |
import base64
|
| 3 |
|
|
|
|
|
|
|
| 4 |
from xai_sdk import Client
|
| 5 |
from xai_sdk.chat import user, image
|
| 6 |
from dotenv import load_dotenv
|
main.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
+
|
| 3 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from typing import List
|
| 6 |
+
|
| 7 |
+
from extract_metadata import get_metadata, get_scenes_metadata
|
| 8 |
+
from grok_analyze import create_caps_with_grok, get_story_with_grok
|
| 9 |
+
from create_captions import create_caps, add_captions_to_video
|
| 10 |
+
from transcripts_editing import get_dialog
|
| 11 |
+
from video_editing_ffmpeg import concatenate_videos
|
| 12 |
+
|
| 13 |
+
import shutil
|
| 14 |
+
import os
|
| 15 |
+
import uuid
|
| 16 |
+
|
| 17 |
+
app = FastAPI(title="Video Storyline Generator API")
|
| 18 |
+
|
| 19 |
+
# Directory to temporarily store uploaded videos
|
| 20 |
+
UPLOAD_DIR = "uploads"
|
| 21 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 22 |
+
|
| 23 |
+
@app.post("/generatestoryline")
|
| 24 |
+
async def generate_storyline(
|
| 25 |
+
videos: List[UploadFile] = File(...),
|
| 26 |
+
date: str = Form(...)
|
| 27 |
+
):
|
| 28 |
+
"""
|
| 29 |
+
Generate a storyline from uploaded videos and a date.
|
| 30 |
+
Returns a selected video filename and a list of generated captions.
|
| 31 |
+
"""
|
| 32 |
+
try:
|
| 33 |
+
# Validate date format
|
| 34 |
+
try:
|
| 35 |
+
input_date = datetime.strptime(date, "%Y-%m-%d")
|
| 36 |
+
except ValueError:
|
| 37 |
+
return JSONResponse(
|
| 38 |
+
status_code=400,
|
| 39 |
+
content={"error": "Invalid date format. Use YYYY-MM-DD"}
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if input_date:
|
| 43 |
+
dialog= get_dialog(date)
|
| 44 |
+
# Save uploaded videos temporarily
|
| 45 |
+
saved_videos = []
|
| 46 |
+
directory=f"{uuid.uuid4()}"
|
| 47 |
+
if not os.path.exists(os.path.join(UPLOAD_DIR,directory)):
|
| 48 |
+
os.makedirs(os.path.join(UPLOAD_DIR,directory))
|
| 49 |
+
for video in videos:
|
| 50 |
+
print(video.filename)
|
| 51 |
+
|
| 52 |
+
file_extension = video.filename.split(".")[-1]
|
| 53 |
+
file_name = f"{uuid.uuid4()}.{file_extension}"
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
file_path = os.path.join(UPLOAD_DIR,directory, file_name)
|
| 58 |
+
|
| 59 |
+
with open(file_path, "wb") as buffer:
|
| 60 |
+
shutil.copyfileobj(video.file, buffer)
|
| 61 |
+
|
| 62 |
+
saved_videos.append(file_path)
|
| 63 |
+
|
| 64 |
+
print("HERE")
|
| 65 |
+
metadata, filename= get_metadata(saved_videos)
|
| 66 |
+
print(metadata)
|
| 67 |
+
|
| 68 |
+
metadata=get_scenes_metadata(metadata)
|
| 69 |
+
|
| 70 |
+
steps=get_story_with_grok(metadata,"",dialog)
|
| 71 |
+
print("here are the steps", steps)
|
| 72 |
+
|
| 73 |
+
caps=create_caps(steps)
|
| 74 |
+
print("the caps:",caps)
|
| 75 |
+
final_video=concatenate_videos(steps)
|
| 76 |
+
|
| 77 |
+
final_video=add_captions_to_video(final_video,False,caps)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
return FileResponse(
|
| 81 |
+
path=final_video,
|
| 82 |
+
media_type="video/mp4", # Adjust based on your video format
|
| 83 |
+
filename="final_storyline_video.mp4" # Name for the downloaded file
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return JSONResponse(
|
| 88 |
+
status_code=500,
|
| 89 |
+
content={"error": f"An error occurred: {str(e)}"}
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
@app.on_event("shutdown")
|
| 93 |
+
async def cleanup():
|
| 94 |
+
"""Clean up uploaded files on shutdown"""
|
| 95 |
+
for file in os.listdir(UPLOAD_DIR):
|
| 96 |
+
file_path = os.path.join(UPLOAD_DIR, file)
|
| 97 |
+
try:
|
| 98 |
+
if os.path.isfile(file_path):
|
| 99 |
+
os.unlink(file_path)
|
| 100 |
+
except Exception:
|
| 101 |
+
pass
|
vercel.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"builds": [
|
| 3 |
+
{
|
| 4 |
+
"src": "main.py",
|
| 5 |
+
"use": "@vercel/python"
|
| 6 |
+
}
|
| 7 |
+
],
|
| 8 |
+
"routes": [
|
| 9 |
+
{
|
| 10 |
+
"src": "/(.*)",
|
| 11 |
+
"dest": "main.py"
|
| 12 |
+
}
|
| 13 |
+
]
|
| 14 |
+
}
|