yukee1992 commited on
Commit
d44f790
Β·
verified Β·
1 Parent(s): 0530dbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -30
app.py CHANGED
@@ -1,14 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import uuid
3
  import json
4
- import sys
5
- import os
6
  from datetime import datetime
7
  from supabase import create_client
8
 
9
- print("=" * 60)
10
- print("πŸš€ STARTING VIDEO PROJECT MANAGER WITH SUPABASE")
11
- print("=" * 60)
12
 
13
  # =============================================
14
  # SUPABASE CONNECTION
@@ -30,8 +60,11 @@ if SUPABASE_URL and SUPABASE_KEY:
30
  print("βœ… Supabase client initialized successfully!")
31
 
32
  # Test connection
33
- test_result = supabase.table("projects").select("count", count="exact").limit(1).execute()
34
- print(f"βœ… Database connected! Projects found: {test_result.count}")
 
 
 
35
  except Exception as e:
36
  print(f"❌ Failed to connect to Supabase: {e}")
37
  supabase = None
@@ -76,16 +109,25 @@ def create_project(title, content, tags, image_prompt, channel):
76
  result = supabase.table("projects").insert(project_data).execute()
77
  db_result = result.data[0] if result.data else None
78
  print(f"βœ… Successfully saved to Supabase! Record ID: {db_result.get('id') if db_result else 'unknown'}")
 
 
 
 
 
 
 
 
 
 
 
79
  except Exception as e:
80
  print(f"❌ Supabase save failed: {e}")
81
- # Still return success but note the database error
82
  return {
83
  "status": "partial_success",
84
  "project_id": project_id,
85
  "folder": folder_name,
86
  "title": title,
87
- "message": "Project created in memory but database save failed",
88
- "error": str(e),
89
  "timestamp": datetime.now().isoformat()
90
  }
91
  else:
@@ -99,20 +141,6 @@ def create_project(title, content, tags, image_prompt, channel):
99
  "timestamp": datetime.now().isoformat()
100
  }
101
 
102
- # Success response
103
- response = {
104
- "status": "success",
105
- "project_id": project_id,
106
- "folder": folder_name,
107
- "title": title,
108
- "message": "Project created and saved to database!",
109
- "database_id": db_result.get('id') if db_result else None,
110
- "timestamp": datetime.now().isoformat()
111
- }
112
-
113
- print(f"βœ… Returning success response")
114
- return response
115
-
116
  except Exception as e:
117
  print(f"❌ Error in create_project: {e}")
118
  import traceback
@@ -185,6 +213,8 @@ def health_check():
185
  # GRADIO INTERFACE
186
  # =============================================
187
 
 
 
188
  with gr.Blocks(title="Video Project Manager") as demo:
189
  gr.Markdown("# 🎬 Video Project Manager")
190
 
@@ -242,7 +272,8 @@ with gr.Blocks(title="Video Project Manager") as demo:
242
  # Debug Tab
243
  with gr.TabItem("πŸ”§ Debug"):
244
  gr.Markdown("### System Information")
245
- gr.JSON(label="Status", value=health_check)
 
246
 
247
  gr.Markdown("### Test API Commands")
248
  gr.Markdown(f"""
@@ -251,11 +282,6 @@ with gr.Blocks(title="Video Project Manager") as demo:
251
  curl -X POST "https://yukee1992-video-project-manager.hf.space/api/create" \\
252
  -H "Content-Type: application/json" \\
253
  -d '{{"data": ["Test", "Content", "tags", "prompt", "{{}}"]}}'
254
-
255
- # Test queue endpoint
256
- curl -X POST "https://yukee1992-video-project-manager.hf.space/queue/join" \\
257
- -H "Content-Type: application/json" \\
258
- -d '{{"fn_index": 0, "data": ["Test", "Content", "tags", "prompt", "{{}}"], "session_hash": "test123"}}'
259
  ```
260
  """)
261
 
@@ -281,6 +307,12 @@ with gr.Blocks(title="Video Project Manager") as demo:
281
  )
282
  print(" βœ… Registered: /api/search")
283
 
 
 
 
 
 
 
284
  print("πŸ“‘ API endpoints registered successfully!")
285
 
286
  # =============================================
 
1
+ import os
2
+ import sys
3
+ import subprocess
4
+ import importlib.util
5
+
6
+ print("=" * 60)
7
+ print("πŸš€ STARTING VIDEO PROJECT MANAGER")
8
+ print("=" * 60)
9
+
10
+ # Function to check and install packages
11
+ 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:
23
+ print(f"❌ Failed to install {package_name}: {e}")
24
+ return False
25
+ else:
26
+ print(f"βœ… {package_name} already installed")
27
+ return True
28
+
29
+ # Install required packages
30
+ ensure_package("gradio")
31
+ ensure_package("supabase")
32
+ 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)
 
 
42
 
43
  # =============================================
44
  # SUPABASE CONNECTION
 
60
  print("βœ… Supabase client initialized successfully!")
61
 
62
  # Test connection
63
+ try:
64
+ test_result = supabase.table("projects").select("count", count="exact").limit(1).execute()
65
+ print(f"βœ… Database connected! Projects found: {test_result.count}")
66
+ except Exception as e:
67
+ print(f"⚠️ Database connection test failed: {e}")
68
  except Exception as e:
69
  print(f"❌ Failed to connect to Supabase: {e}")
70
  supabase = None
 
109
  result = supabase.table("projects").insert(project_data).execute()
110
  db_result = result.data[0] if result.data else None
111
  print(f"βœ… Successfully saved to Supabase! Record ID: {db_result.get('id') if db_result else 'unknown'}")
112
+
113
+ return {
114
+ "status": "success",
115
+ "project_id": project_id,
116
+ "folder": folder_name,
117
+ "title": title,
118
+ "message": "Project created and saved to database!",
119
+ "database_id": db_result.get('id') if db_result else None,
120
+ "timestamp": datetime.now().isoformat()
121
+ }
122
+
123
  except Exception as e:
124
  print(f"❌ Supabase save failed: {e}")
 
125
  return {
126
  "status": "partial_success",
127
  "project_id": project_id,
128
  "folder": folder_name,
129
  "title": title,
130
+ "message": f"Project created but database save failed: {str(e)}",
 
131
  "timestamp": datetime.now().isoformat()
132
  }
133
  else:
 
141
  "timestamp": datetime.now().isoformat()
142
  }
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  except Exception as e:
145
  print(f"❌ Error in create_project: {e}")
146
  import traceback
 
213
  # GRADIO INTERFACE
214
  # =============================================
215
 
216
+ print("\nπŸ“ Creating Gradio interface...")
217
+
218
  with gr.Blocks(title="Video Project Manager") as demo:
219
  gr.Markdown("# 🎬 Video Project Manager")
220
 
 
272
  # Debug Tab
273
  with gr.TabItem("πŸ”§ Debug"):
274
  gr.Markdown("### System Information")
275
+ status_json = gr.JSON(label="Status", value=health_check)
276
+ refresh_btn = gr.Button("πŸ”„ Refresh Status")
277
 
278
  gr.Markdown("### Test API Commands")
279
  gr.Markdown(f"""
 
282
  curl -X POST "https://yukee1992-video-project-manager.hf.space/api/create" \\
283
  -H "Content-Type: application/json" \\
284
  -d '{{"data": ["Test", "Content", "tags", "prompt", "{{}}"]}}'
 
 
 
 
 
285
  ```
286
  """)
287
 
 
307
  )
308
  print(" βœ… Registered: /api/search")
309
 
310
+ refresh_btn.click(
311
+ fn=health_check,
312
+ inputs=[],
313
+ outputs=[status_json]
314
+ )
315
+
316
  print("πŸ“‘ API endpoints registered successfully!")
317
 
318
  # =============================================