Spaces:
Sleeping
Sleeping
Factor Studios
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,267 +1,220 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
"
|
| 70 |
-
"
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
"
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
"
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
"
|
| 169 |
-
"
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
return
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
"
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
try:
|
| 223 |
-
with open(file_path, "r") as f:
|
| 224 |
-
data = json.load(f)
|
| 225 |
-
|
| 226 |
-
# Get basic statistics
|
| 227 |
-
frame_analyses = data.get("frame_analyses", [])
|
| 228 |
-
summary = data.get("summary", {})
|
| 229 |
-
|
| 230 |
-
# Count frames with descriptions
|
| 231 |
-
frames_with_descriptions = len([f for f in frame_analyses if f.get("description")])
|
| 232 |
-
|
| 233 |
-
file_stats = os.stat(file_path)
|
| 234 |
-
|
| 235 |
-
return {
|
| 236 |
-
"filename": filename,
|
| 237 |
-
"file_size_bytes": file_stats.st_size,
|
| 238 |
-
"modified_time": time.ctime(file_stats.st_mtime),
|
| 239 |
-
"total_frames": len(frame_analyses),
|
| 240 |
-
"frames_with_descriptions": frames_with_descriptions,
|
| 241 |
-
"summary": summary,
|
| 242 |
-
"steps": summary.get("steps", []),
|
| 243 |
-
"high_level_goal": summary.get("high_level_goal", ""),
|
| 244 |
-
"final_goal": summary.get("final_goal", "")
|
| 245 |
-
}
|
| 246 |
-
|
| 247 |
-
except json.JSONDecodeError:
|
| 248 |
-
raise HTTPException(status_code=500, detail=f"Invalid JSON in file {filename}")
|
| 249 |
-
except Exception as e:
|
| 250 |
-
raise HTTPException(status_code=500, detail=f"Error reading file {filename}: {str(e)}")
|
| 251 |
-
|
| 252 |
-
if __name__ == "__main__":
|
| 253 |
-
# Start the FastAPI server
|
| 254 |
-
print("Starting Video Analysis FastAPI Server...")
|
| 255 |
-
print("API Documentation will be available at: http://localhost:8000/docs")
|
| 256 |
-
print("API Root endpoint: http://localhost:8000/")
|
| 257 |
-
|
| 258 |
-
# Ensure the analysis output folder exists
|
| 259 |
-
os.makedirs(ANALYSIS_OUTPUT_FOLDER, exist_ok=True)
|
| 260 |
-
|
| 261 |
-
uvicorn.run(
|
| 262 |
-
app,
|
| 263 |
-
host="0.0.0.0",
|
| 264 |
-
port=8000,
|
| 265 |
-
log_level="info",
|
| 266 |
-
reload=False # Set to False for production
|
| 267 |
-
)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
+
import threading
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# Import from vision_analyzer (previously cursor_tracker)
|
| 9 |
+
from vision_analyzer import (
|
| 10 |
+
main_processing_loop,
|
| 11 |
+
processing_status,
|
| 12 |
+
ANALYSIS_OUTPUT_FOLDER,
|
| 13 |
+
log_message
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Global variable to track if processing is running
|
| 17 |
+
processing_thread = None
|
| 18 |
+
|
| 19 |
+
def start_processing_gradio(start_index: int = 0):
|
| 20 |
+
global processing_thread
|
| 21 |
+
|
| 22 |
+
if processing_thread and processing_thread.is_alive():
|
| 23 |
+
return "Processing is already running"
|
| 24 |
+
|
| 25 |
+
if processing_status["is_running"]:
|
| 26 |
+
return "Processing is already running"
|
| 27 |
+
|
| 28 |
+
# Start processing in a background thread
|
| 29 |
+
processing_thread = threading.Thread(target=main_processing_loop, args=(start_index,))
|
| 30 |
+
processing_thread.daemon = True
|
| 31 |
+
processing_thread.start()
|
| 32 |
+
|
| 33 |
+
return f"Processing started in background from index {start_index}"
|
| 34 |
+
|
| 35 |
+
def stop_processing_gradio():
|
| 36 |
+
global processing_thread
|
| 37 |
+
|
| 38 |
+
if not processing_status["is_running"] and (not processing_thread or not processing_thread.is_alive()):
|
| 39 |
+
return "No processing is currently running"
|
| 40 |
+
|
| 41 |
+
# Note: This is a graceful stop request
|
| 42 |
+
processing_status["is_running"] = False
|
| 43 |
+
|
| 44 |
+
return "Stop signal sent to processing pipeline"
|
| 45 |
+
|
| 46 |
+
def get_status_gradio():
|
| 47 |
+
return {
|
| 48 |
+
"processing_status": processing_status,
|
| 49 |
+
"analysis_folder": ANALYSIS_OUTPUT_FOLDER,
|
| 50 |
+
"folder_exists": os.path.exists(ANALYSIS_OUTPUT_FOLDER)
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
def list_analysis_data_gradio():
|
| 54 |
+
if not os.path.exists(ANALYSIS_OUTPUT_FOLDER):
|
| 55 |
+
return "Analysis output folder does not exist yet"
|
| 56 |
+
|
| 57 |
+
json_files = []
|
| 58 |
+
for file in os.listdir(ANALYSIS_OUTPUT_FOLDER):
|
| 59 |
+
if file.endswith(".json"):
|
| 60 |
+
file_path = os.path.join(ANALYSIS_OUTPUT_FOLDER, file)
|
| 61 |
+
file_stats = os.stat(file_path)
|
| 62 |
+
json_files.append({
|
| 63 |
+
"filename": file,
|
| 64 |
+
"size_bytes": file_stats.st_size,
|
| 65 |
+
"modified_time": time.ctime(file_stats.st_mtime),
|
| 66 |
+
})
|
| 67 |
+
|
| 68 |
+
return json.dumps({
|
| 69 |
+
"files": json_files,
|
| 70 |
+
"total_files": len(json_files),
|
| 71 |
+
"folder_path": ANALYSIS_OUTPUT_FOLDER
|
| 72 |
+
}, indent=2)
|
| 73 |
+
|
| 74 |
+
def get_analysis_data_gradio(filename: str):
|
| 75 |
+
if not filename.endswith(".json"):
|
| 76 |
+
return "File must be a JSON file"
|
| 77 |
+
|
| 78 |
+
file_path = os.path.join(ANALYSIS_OUTPUT_FOLDER, filename)
|
| 79 |
+
|
| 80 |
+
if not os.path.exists(file_path):
|
| 81 |
+
return f"File {filename} not found"
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
with open(file_path, "r") as f:
|
| 85 |
+
data = json.load(f)
|
| 86 |
+
|
| 87 |
+
# Add metadata
|
| 88 |
+
file_stats = os.stat(file_path)
|
| 89 |
+
|
| 90 |
+
# Extract summary information
|
| 91 |
+
frame_analyses = data.get("frame_analyses", [])
|
| 92 |
+
summary = data.get("summary", {})
|
| 93 |
+
|
| 94 |
+
response_data = {
|
| 95 |
+
"filename": filename,
|
| 96 |
+
"file_size_bytes": file_stats.st_size,
|
| 97 |
+
"modified_time": time.ctime(file_stats.st_mtime),
|
| 98 |
+
"total_frames": len(frame_analyses),
|
| 99 |
+
"summary": summary,
|
| 100 |
+
"frame_samples": frame_analyses[:5] # Return first 5 frames as samples
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
return json.dumps(response_data, indent=2)
|
| 104 |
+
|
| 105 |
+
except json.JSONDecodeError:
|
| 106 |
+
return f"Invalid JSON in file {filename}"
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return f"Error reading file {filename}: {str(e)}"
|
| 109 |
+
|
| 110 |
+
def get_full_analysis_data_gradio(filename: str):
|
| 111 |
+
if not filename.endswith(".json"):
|
| 112 |
+
return "File must be a JSON file"
|
| 113 |
+
|
| 114 |
+
file_path = os.path.join(ANALYSIS_OUTPUT_FOLDER, filename)
|
| 115 |
+
|
| 116 |
+
if not os.path.exists(file_path):
|
| 117 |
+
return f"File {filename} not found"
|
| 118 |
+
|
| 119 |
+
try:
|
| 120 |
+
with open(file_path, "r") as f:
|
| 121 |
+
data = json.load(f)
|
| 122 |
+
|
| 123 |
+
# Add metadata
|
| 124 |
+
file_stats = os.stat(file_path)
|
| 125 |
+
data["metadata"] = {
|
| 126 |
+
"filename": filename,
|
| 127 |
+
"file_size_bytes": file_stats.st_size,
|
| 128 |
+
"modified_time": time.ctime(file_stats.st_mtime)
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
return json.dumps(data, indent=2)
|
| 132 |
+
|
| 133 |
+
except json.JSONDecodeError:
|
| 134 |
+
return f"Invalid JSON in file {filename}"
|
| 135 |
+
except Exception as e:
|
| 136 |
+
return f"Error reading file {filename}: {str(e)}"
|
| 137 |
+
|
| 138 |
+
def get_analysis_summary_gradio(filename: str):
|
| 139 |
+
if not filename.endswith(".json"):
|
| 140 |
+
return "File must be a JSON file"
|
| 141 |
+
|
| 142 |
+
file_path = os.path.join(ANALYSIS_OUTPUT_FOLDER, filename)
|
| 143 |
+
|
| 144 |
+
if not os.path.exists(file_path):
|
| 145 |
+
return f"File {filename} not found"
|
| 146 |
+
|
| 147 |
+
try:
|
| 148 |
+
with open(file_path, "r") as f:
|
| 149 |
+
data = json.load(f)
|
| 150 |
+
|
| 151 |
+
# Get basic statistics
|
| 152 |
+
frame_analyses = data.get("frame_analyses", [])
|
| 153 |
+
summary = data.get("summary", {})
|
| 154 |
+
|
| 155 |
+
# Count frames with descriptions
|
| 156 |
+
frames_with_descriptions = len([f for f in frame_analyses if f.get("description")])
|
| 157 |
+
|
| 158 |
+
file_stats = os.stat(file_path)
|
| 159 |
+
|
| 160 |
+
return json.dumps({
|
| 161 |
+
"filename": filename,
|
| 162 |
+
"file_size_bytes": file_stats.st_size,
|
| 163 |
+
"modified_time": time.ctime(file_stats.st_mtime),
|
| 164 |
+
"total_frames": len(frame_analyses),
|
| 165 |
+
"frames_with_descriptions": frames_with_descriptions,
|
| 166 |
+
"summary": summary,
|
| 167 |
+
"steps": summary.get("steps", []),
|
| 168 |
+
"high_level_goal": summary.get("high_level_goal", ""),
|
| 169 |
+
"final_goal": summary.get("final_goal", "")
|
| 170 |
+
}, indent=2)
|
| 171 |
+
|
| 172 |
+
except json.JSONDecodeError:
|
| 173 |
+
return f"Invalid JSON in file {filename}"
|
| 174 |
+
except Exception as e:
|
| 175 |
+
return f"Error reading file {filename}: {str(e)}"
|
| 176 |
+
|
| 177 |
+
# Ensure the analysis output folder exists
|
| 178 |
+
os.makedirs(ANALYSIS_OUTPUT_FOLDER, exist_ok=True)
|
| 179 |
+
|
| 180 |
+
# Gradio Interface
|
| 181 |
+
with gr.Blocks() as demo:
|
| 182 |
+
gr.Markdown("# Video Analysis Interface")
|
| 183 |
+
|
| 184 |
+
with gr.Tab("Processing Control"):
|
| 185 |
+
gr.Markdown("## Start/Stop Video Processing")
|
| 186 |
+
start_index_input = gr.Number(label="Start Index", value=0, precision=0)
|
| 187 |
+
start_btn = gr.Button("Start Processing")
|
| 188 |
+
stop_btn = gr.Button("Stop Processing")
|
| 189 |
+
processing_output = gr.Textbox(label="Processing Status")
|
| 190 |
+
|
| 191 |
+
start_btn.click(start_processing_gradio, inputs=start_index_input, outputs=processing_output)
|
| 192 |
+
stop_btn.click(stop_processing_gradio, outputs=processing_output)
|
| 193 |
+
|
| 194 |
+
with gr.Tab("Analysis Data"):
|
| 195 |
+
gr.Markdown("## View Analysis Data")
|
| 196 |
+
list_data_btn = gr.Button("List All Analysis Files")
|
| 197 |
+
list_data_output = gr.JSON(label="Available Analysis Files")
|
| 198 |
+
|
| 199 |
+
list_data_btn.click(list_analysis_data_gradio, outputs=list_data_output)
|
| 200 |
+
|
| 201 |
+
gr.Markdown("### Get Specific Analysis Data")
|
| 202 |
+
filename_input = gr.Textbox(label="Filename (e.g., video_analysis.json)")
|
| 203 |
+
get_data_btn = gr.Button("Get Analysis Data (Summary)")
|
| 204 |
+
get_full_data_btn = gr.Button("Get Full Analysis Data")
|
| 205 |
+
get_summary_btn = gr.Button("Get Analysis Summary")
|
| 206 |
+
specific_data_output = gr.JSON(label="Analysis Data")
|
| 207 |
+
|
| 208 |
+
get_data_btn.click(get_analysis_data_gradio, inputs=filename_input, outputs=specific_data_output)
|
| 209 |
+
get_full_data_btn.click(get_full_analysis_data_gradio, inputs=filename_input, outputs=specific_data_output)
|
| 210 |
+
get_summary_btn.click(get_analysis_summary_gradio, inputs=filename_input, outputs=specific_data_output)
|
| 211 |
+
|
| 212 |
+
with gr.Tab("Current Status"):
|
| 213 |
+
gr.Markdown("## Current Processing Status")
|
| 214 |
+
status_btn = gr.Button("Refresh Status")
|
| 215 |
+
status_output = gr.JSON(label="Current Status")
|
| 216 |
+
|
| 217 |
+
status_btn.click(get_status_gradio, outputs=status_output)
|
| 218 |
+
|
| 219 |
+
if __name__ == "__main__":
|
| 220 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|