Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,9 @@ import os
|
|
| 2 |
import sys
|
| 3 |
import subprocess
|
| 4 |
import importlib.util
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
print("=" * 60)
|
| 7 |
print("π STARTING VIDEO PROJECT MANAGER")
|
|
@@ -12,11 +15,10 @@ def ensure_package(package_name, import_name=None):
|
|
| 12 |
if import_name is None:
|
| 13 |
import_name = package_name
|
| 14 |
|
| 15 |
-
# Check if package is installed
|
| 16 |
if importlib.util.find_spec(import_name) is None:
|
| 17 |
print(f"π¦ Installing {package_name}...")
|
| 18 |
try:
|
| 19 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package_name])
|
| 20 |
print(f"β
{package_name} installed successfully!")
|
| 21 |
return True
|
| 22 |
except Exception as e:
|
|
@@ -33,9 +35,6 @@ ensure_package("python-dotenv")
|
|
| 33 |
|
| 34 |
# Now import after ensuring they're installed
|
| 35 |
import gradio as gr
|
| 36 |
-
import uuid
|
| 37 |
-
import json
|
| 38 |
-
from datetime import datetime
|
| 39 |
from supabase import create_client
|
| 40 |
|
| 41 |
print("\n" + "=" * 60)
|
|
@@ -44,7 +43,6 @@ print("\n" + "=" * 60)
|
|
| 44 |
# SUPABASE CONNECTION
|
| 45 |
# =============================================
|
| 46 |
|
| 47 |
-
# Get Supabase credentials from environment variables
|
| 48 |
SUPABASE_URL = os.environ.get("SUPABASE_URL")
|
| 49 |
SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
|
| 50 |
|
|
@@ -78,17 +76,19 @@ else:
|
|
| 78 |
def create_project(title, content, tags, image_prompt, channel):
|
| 79 |
"""Create a new project and save to Supabase"""
|
| 80 |
print(f"\nπ Creating project: {title}")
|
|
|
|
|
|
|
| 81 |
|
| 82 |
try:
|
| 83 |
# Generate proper UUID
|
| 84 |
-
project_id = str(uuid.uuid4())
|
| 85 |
folder_name = title.lower().replace(' ', '_').replace('/', '-')[:20]
|
| 86 |
|
| 87 |
project_data = {
|
| 88 |
"project_id": project_id,
|
| 89 |
"folder_name": folder_name,
|
| 90 |
"title": title,
|
| 91 |
-
"content": content,
|
| 92 |
"tags": tags,
|
| 93 |
"image_prompt": image_prompt,
|
| 94 |
"channel_details": channel,
|
|
@@ -99,27 +99,39 @@ def create_project(title, content, tags, image_prompt, channel):
|
|
| 99 |
print(f"πΎ Saving project with UUID: {project_id}")
|
| 100 |
|
| 101 |
if supabase:
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
else:
|
|
|
|
| 113 |
return {
|
| 114 |
"status": "memory_only",
|
| 115 |
"project_id": project_id,
|
| 116 |
"folder": folder_name,
|
| 117 |
"title": title,
|
| 118 |
-
"message": "
|
| 119 |
}
|
| 120 |
|
| 121 |
except Exception as e:
|
| 122 |
print(f"β Error: {e}")
|
|
|
|
|
|
|
| 123 |
return {"status": "error", "message": str(e)}
|
| 124 |
|
| 125 |
def search_projects(term):
|
|
@@ -127,11 +139,9 @@ def search_projects(term):
|
|
| 127 |
print(f"\nπ Searching for: {term}")
|
| 128 |
|
| 129 |
if not supabase:
|
| 130 |
-
print("β οΈ No database connection - returning demo data")
|
| 131 |
return [
|
| 132 |
["Demo Project 1", "memory", datetime.now().strftime("%Y-%m-%d"), "demo_1"],
|
| 133 |
["How to Make Videos", "demo", datetime.now().strftime("%Y-%m-%d"), "tutorial"],
|
| 134 |
-
[term or "Search Demo", "demo", datetime.now().strftime("%Y-%m-%d"), "demo"]
|
| 135 |
]
|
| 136 |
|
| 137 |
try:
|
|
@@ -160,7 +170,6 @@ def search_projects(term):
|
|
| 160 |
p.get("folder_name", "")
|
| 161 |
])
|
| 162 |
|
| 163 |
-
print(f"β
Found {len(projects)} projects")
|
| 164 |
return projects if projects else [["No projects found", "", "", ""]]
|
| 165 |
|
| 166 |
except Exception as e:
|
|
@@ -172,14 +181,66 @@ def health_check():
|
|
| 172 |
return {
|
| 173 |
"status": "healthy",
|
| 174 |
"supabase_connected": bool(supabase),
|
| 175 |
-
"timestamp": datetime.now().isoformat()
|
| 176 |
-
"endpoints": {
|
| 177 |
-
"create": "/api/create",
|
| 178 |
-
"search": "/api/search",
|
| 179 |
-
"health": "/api/health"
|
| 180 |
-
}
|
| 181 |
}
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
# =============================================
|
| 184 |
# GRADIO INTERFACE
|
| 185 |
# =============================================
|
|
@@ -200,28 +261,11 @@ with gr.Blocks(title="Video Project Manager") as demo:
|
|
| 200 |
with gr.TabItem("π Create Project"):
|
| 201 |
with gr.Row():
|
| 202 |
with gr.Column():
|
| 203 |
-
title_input = gr.Textbox(
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
-
label="Content",
|
| 209 |
-
lines=3,
|
| 210 |
-
placeholder="Enter TTS content"
|
| 211 |
-
)
|
| 212 |
-
tags_input = gr.Textbox(
|
| 213 |
-
label="Tags",
|
| 214 |
-
placeholder="tag1, tag2, tag3"
|
| 215 |
-
)
|
| 216 |
-
image_input = gr.Textbox(
|
| 217 |
-
label="Image Prompt",
|
| 218 |
-
lines=2,
|
| 219 |
-
placeholder="Describe images"
|
| 220 |
-
)
|
| 221 |
-
channel_input = gr.Textbox(
|
| 222 |
-
label="Channel Details",
|
| 223 |
-
value='{"platform": "youtube"}'
|
| 224 |
-
)
|
| 225 |
|
| 226 |
create_btn = gr.Button("π Create Project", variant="primary")
|
| 227 |
|
|
@@ -230,73 +274,83 @@ with gr.Blocks(title="Video Project Manager") as demo:
|
|
| 230 |
|
| 231 |
# Search Tab
|
| 232 |
with gr.TabItem("π Search"):
|
| 233 |
-
search_input = gr.Textbox(
|
| 234 |
-
label="Search Term",
|
| 235 |
-
placeholder="Enter search term"
|
| 236 |
-
)
|
| 237 |
search_btn = gr.Button("π Search", variant="secondary")
|
| 238 |
results = gr.Dataframe(
|
| 239 |
headers=["Title", "Status", "Date", "Folder"],
|
| 240 |
label="Search Results"
|
| 241 |
)
|
| 242 |
|
| 243 |
-
#
|
| 244 |
-
with gr.TabItem("
|
| 245 |
-
gr.Markdown("
|
| 246 |
-
|
| 247 |
-
refresh_btn = gr.Button("π Refresh Status")
|
| 248 |
|
| 249 |
-
|
| 250 |
-
gr.Markdown(f"""
|
| 251 |
```bash
|
| 252 |
-
# Test create endpoint
|
| 253 |
curl -X POST "https://yukee1992-video-project-manager.hf.space/api/create" \\
|
| 254 |
-H "Content-Type: application/json" \\
|
| 255 |
-
-d '{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
```
|
| 257 |
""")
|
| 258 |
|
| 259 |
-
#
|
| 260 |
-
# EVENT HANDLERS
|
| 261 |
-
# =============================================
|
| 262 |
-
|
| 263 |
-
print("\nπ‘ Registering API endpoints...")
|
| 264 |
-
|
| 265 |
create_btn.click(
|
| 266 |
fn=create_project,
|
| 267 |
inputs=[title_input, content_input, tags_input, image_input, channel_input],
|
| 268 |
-
outputs=[output]
|
| 269 |
-
api_name="create"
|
| 270 |
)
|
| 271 |
-
print(" β
Registered: /api/create")
|
| 272 |
|
| 273 |
search_btn.click(
|
| 274 |
fn=search_projects,
|
| 275 |
inputs=[search_input],
|
| 276 |
-
outputs=[results]
|
| 277 |
-
api_name="search"
|
| 278 |
-
)
|
| 279 |
-
print(" β
Registered: /api/search")
|
| 280 |
-
|
| 281 |
-
refresh_btn.click(
|
| 282 |
-
fn=health_check,
|
| 283 |
-
inputs=[],
|
| 284 |
-
outputs=[status_json]
|
| 285 |
)
|
| 286 |
|
| 287 |
-
print("
|
| 288 |
|
| 289 |
# =============================================
|
| 290 |
-
#
|
| 291 |
# =============================================
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
| 297 |
demo.launch(
|
| 298 |
server_name="0.0.0.0",
|
| 299 |
server_port=7860,
|
| 300 |
share=False,
|
| 301 |
debug=True
|
| 302 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import sys
|
| 3 |
import subprocess
|
| 4 |
import importlib.util
|
| 5 |
+
import uuid
|
| 6 |
+
import json
|
| 7 |
+
from datetime import datetime
|
| 8 |
|
| 9 |
print("=" * 60)
|
| 10 |
print("π STARTING VIDEO PROJECT MANAGER")
|
|
|
|
| 15 |
if import_name is None:
|
| 16 |
import_name = package_name
|
| 17 |
|
|
|
|
| 18 |
if importlib.util.find_spec(import_name) is None:
|
| 19 |
print(f"π¦ Installing {package_name}...")
|
| 20 |
try:
|
| 21 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", package_name, "--quiet"])
|
| 22 |
print(f"β
{package_name} installed successfully!")
|
| 23 |
return True
|
| 24 |
except Exception as e:
|
|
|
|
| 35 |
|
| 36 |
# Now import after ensuring they're installed
|
| 37 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 38 |
from supabase import create_client
|
| 39 |
|
| 40 |
print("\n" + "=" * 60)
|
|
|
|
| 43 |
# SUPABASE CONNECTION
|
| 44 |
# =============================================
|
| 45 |
|
|
|
|
| 46 |
SUPABASE_URL = os.environ.get("SUPABASE_URL")
|
| 47 |
SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
|
| 48 |
|
|
|
|
| 76 |
def create_project(title, content, tags, image_prompt, channel):
|
| 77 |
"""Create a new project and save to Supabase"""
|
| 78 |
print(f"\nπ Creating project: {title}")
|
| 79 |
+
print(f" Content length: {len(content)}")
|
| 80 |
+
print(f" Tags: {tags}")
|
| 81 |
|
| 82 |
try:
|
| 83 |
# Generate proper UUID
|
| 84 |
+
project_id = str(uuid.uuid4())
|
| 85 |
folder_name = title.lower().replace(' ', '_').replace('/', '-')[:20]
|
| 86 |
|
| 87 |
project_data = {
|
| 88 |
"project_id": project_id,
|
| 89 |
"folder_name": folder_name,
|
| 90 |
"title": title,
|
| 91 |
+
"content": content[:5000],
|
| 92 |
"tags": tags,
|
| 93 |
"image_prompt": image_prompt,
|
| 94 |
"channel_details": channel,
|
|
|
|
| 99 |
print(f"πΎ Saving project with UUID: {project_id}")
|
| 100 |
|
| 101 |
if supabase:
|
| 102 |
+
try:
|
| 103 |
+
result = supabase.table("projects").insert(project_data).execute()
|
| 104 |
+
print(f"β
Saved to database! Record ID: {result.data[0]['id'] if result.data else 'unknown'}")
|
| 105 |
+
|
| 106 |
+
return {
|
| 107 |
+
"status": "success",
|
| 108 |
+
"project_id": project_id,
|
| 109 |
+
"folder": folder_name,
|
| 110 |
+
"title": title,
|
| 111 |
+
"message": "Project created successfully in database!"
|
| 112 |
+
}
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"β Database error: {e}")
|
| 115 |
+
return {
|
| 116 |
+
"status": "error",
|
| 117 |
+
"message": f"Database error: {str(e)}",
|
| 118 |
+
"project_id": project_id,
|
| 119 |
+
"folder": folder_name
|
| 120 |
+
}
|
| 121 |
else:
|
| 122 |
+
print("β οΈ No database connection - memory only")
|
| 123 |
return {
|
| 124 |
"status": "memory_only",
|
| 125 |
"project_id": project_id,
|
| 126 |
"folder": folder_name,
|
| 127 |
"title": title,
|
| 128 |
+
"message": "Project created in memory only (no database)"
|
| 129 |
}
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
print(f"β Error: {e}")
|
| 133 |
+
import traceback
|
| 134 |
+
traceback.print_exc()
|
| 135 |
return {"status": "error", "message": str(e)}
|
| 136 |
|
| 137 |
def search_projects(term):
|
|
|
|
| 139 |
print(f"\nπ Searching for: {term}")
|
| 140 |
|
| 141 |
if not supabase:
|
|
|
|
| 142 |
return [
|
| 143 |
["Demo Project 1", "memory", datetime.now().strftime("%Y-%m-%d"), "demo_1"],
|
| 144 |
["How to Make Videos", "demo", datetime.now().strftime("%Y-%m-%d"), "tutorial"],
|
|
|
|
| 145 |
]
|
| 146 |
|
| 147 |
try:
|
|
|
|
| 170 |
p.get("folder_name", "")
|
| 171 |
])
|
| 172 |
|
|
|
|
| 173 |
return projects if projects else [["No projects found", "", "", ""]]
|
| 174 |
|
| 175 |
except Exception as e:
|
|
|
|
| 181 |
return {
|
| 182 |
"status": "healthy",
|
| 183 |
"supabase_connected": bool(supabase),
|
| 184 |
+
"timestamp": datetime.now().isoformat()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
}
|
| 186 |
|
| 187 |
+
# =============================================
|
| 188 |
+
# FASTAPI ROUTES (Direct API endpoints)
|
| 189 |
+
# =============================================
|
| 190 |
+
|
| 191 |
+
from fastapi import FastAPI, Request
|
| 192 |
+
from fastapi.responses import JSONResponse
|
| 193 |
+
import uvicorn
|
| 194 |
+
import threading
|
| 195 |
+
|
| 196 |
+
# Create FastAPI app
|
| 197 |
+
fastapi_app = FastAPI()
|
| 198 |
+
|
| 199 |
+
@fastapi_app.post("/api/create")
|
| 200 |
+
async def api_create(request: Request):
|
| 201 |
+
"""Direct API endpoint for creating projects"""
|
| 202 |
+
try:
|
| 203 |
+
data = await request.json()
|
| 204 |
+
# Extract data from the request
|
| 205 |
+
if "data" in data and len(data["data"]) >= 5:
|
| 206 |
+
result = create_project(
|
| 207 |
+
data["data"][0],
|
| 208 |
+
data["data"][1],
|
| 209 |
+
data["data"][2],
|
| 210 |
+
data["data"][3],
|
| 211 |
+
data["data"][4]
|
| 212 |
+
)
|
| 213 |
+
return JSONResponse(content={"data": [result]})
|
| 214 |
+
else:
|
| 215 |
+
return JSONResponse(
|
| 216 |
+
status_code=400,
|
| 217 |
+
content={"error": "Invalid data format"}
|
| 218 |
+
)
|
| 219 |
+
except Exception as e:
|
| 220 |
+
return JSONResponse(
|
| 221 |
+
status_code=500,
|
| 222 |
+
content={"error": str(e)}
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
@fastapi_app.post("/api/health")
|
| 226 |
+
async def api_health():
|
| 227 |
+
"""Health check endpoint"""
|
| 228 |
+
return JSONResponse(content=health_check())
|
| 229 |
+
|
| 230 |
+
@fastapi_app.post("/api/search")
|
| 231 |
+
async def api_search(request: Request):
|
| 232 |
+
"""Search endpoint"""
|
| 233 |
+
try:
|
| 234 |
+
data = await request.json()
|
| 235 |
+
term = data.get("data", [""])[0] if data.get("data") else ""
|
| 236 |
+
result = search_projects(term)
|
| 237 |
+
return JSONResponse(content={"data": [result]})
|
| 238 |
+
except Exception as e:
|
| 239 |
+
return JSONResponse(
|
| 240 |
+
status_code=500,
|
| 241 |
+
content={"error": str(e)}
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
# =============================================
|
| 245 |
# GRADIO INTERFACE
|
| 246 |
# =============================================
|
|
|
|
| 261 |
with gr.TabItem("π Create Project"):
|
| 262 |
with gr.Row():
|
| 263 |
with gr.Column():
|
| 264 |
+
title_input = gr.Textbox(label="Title", placeholder="Enter video title")
|
| 265 |
+
content_input = gr.Textbox(label="Content", lines=3, placeholder="Enter TTS content")
|
| 266 |
+
tags_input = gr.Textbox(label="Tags", placeholder="tag1, tag2, tag3")
|
| 267 |
+
image_input = gr.Textbox(label="Image Prompt", lines=2, placeholder="Describe images")
|
| 268 |
+
channel_input = gr.Textbox(label="Channel Details", value='{"platform": "youtube"}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
create_btn = gr.Button("π Create Project", variant="primary")
|
| 271 |
|
|
|
|
| 274 |
|
| 275 |
# Search Tab
|
| 276 |
with gr.TabItem("π Search"):
|
| 277 |
+
search_input = gr.Textbox(label="Search Term", placeholder="Enter search term")
|
|
|
|
|
|
|
|
|
|
| 278 |
search_btn = gr.Button("π Search", variant="secondary")
|
| 279 |
results = gr.Dataframe(
|
| 280 |
headers=["Title", "Status", "Date", "Folder"],
|
| 281 |
label="Search Results"
|
| 282 |
)
|
| 283 |
|
| 284 |
+
# API Info Tab
|
| 285 |
+
with gr.TabItem("π API Info"):
|
| 286 |
+
gr.Markdown("""
|
| 287 |
+
## API Endpoints
|
|
|
|
| 288 |
|
| 289 |
+
### Create Project
|
|
|
|
| 290 |
```bash
|
|
|
|
| 291 |
curl -X POST "https://yukee1992-video-project-manager.hf.space/api/create" \\
|
| 292 |
-H "Content-Type: application/json" \\
|
| 293 |
+
-d '{"data": ["Title", "Content", "tags", "prompt", "{}"]}'
|
| 294 |
+
```
|
| 295 |
+
|
| 296 |
+
### Health Check
|
| 297 |
+
```bash
|
| 298 |
+
curl -X POST "https://yukee1992-video-project-manager.hf.space/api/health" \\
|
| 299 |
+
-H "Content-Type: application/json" \\
|
| 300 |
+
-d '{}'
|
| 301 |
+
```
|
| 302 |
+
|
| 303 |
+
### Search
|
| 304 |
+
```bash
|
| 305 |
+
curl -X POST "https://yukee1992-video-project-manager.hf.space/api/search" \\
|
| 306 |
+
-H "Content-Type: application/json" \\
|
| 307 |
+
-d '{"data": ["search term"]}'
|
| 308 |
```
|
| 309 |
""")
|
| 310 |
|
| 311 |
+
# Gradio event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
create_btn.click(
|
| 313 |
fn=create_project,
|
| 314 |
inputs=[title_input, content_input, tags_input, image_input, channel_input],
|
| 315 |
+
outputs=[output]
|
|
|
|
| 316 |
)
|
|
|
|
| 317 |
|
| 318 |
search_btn.click(
|
| 319 |
fn=search_projects,
|
| 320 |
inputs=[search_input],
|
| 321 |
+
outputs=[results]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
)
|
| 323 |
|
| 324 |
+
print("β
Gradio interface created")
|
| 325 |
|
| 326 |
# =============================================
|
| 327 |
+
# RUN BOTH FASTAPI AND GRADIO
|
| 328 |
# =============================================
|
| 329 |
+
|
| 330 |
+
def run_fastapi():
|
| 331 |
+
"""Run FastAPI server in a separate thread"""
|
| 332 |
+
uvicorn.run(fastapi_app, host="0.0.0.0", port=8000)
|
| 333 |
+
|
| 334 |
+
def run_gradio():
|
| 335 |
+
"""Run Gradio interface"""
|
| 336 |
demo.launch(
|
| 337 |
server_name="0.0.0.0",
|
| 338 |
server_port=7860,
|
| 339 |
share=False,
|
| 340 |
debug=True
|
| 341 |
+
)
|
| 342 |
+
|
| 343 |
+
if __name__ == "__main__":
|
| 344 |
+
print("\n" + "=" * 60)
|
| 345 |
+
print("π Launching servers...")
|
| 346 |
+
print(" - Gradio UI: http://0.0.0.0:7860")
|
| 347 |
+
print(" - FastAPI: http://0.0.0.0:8000")
|
| 348 |
+
print(" - API endpoints: /api/create, /api/health, /api/search")
|
| 349 |
+
print("=" * 60)
|
| 350 |
+
|
| 351 |
+
# Start FastAPI in a thread
|
| 352 |
+
fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
|
| 353 |
+
fastapi_thread.start()
|
| 354 |
+
|
| 355 |
+
# Start Gradio in main thread
|
| 356 |
+
run_gradio()
|