Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -96,13 +96,38 @@ cv2.ocl.setUseOpenCL(True)
|
|
96 |
# ------------------- Logger Class ------------------- #
|
97 |
class UsageLogger:
|
98 |
"""Simple logger to record app usage timestamps"""
|
99 |
-
def __init__(self,
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
self.ensure_log_file_exists()
|
102 |
|
103 |
def ensure_log_file_exists(self):
|
104 |
"""Create log file with empty array if it doesn't exist"""
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
with open(self.log_file, 'w') as f:
|
107 |
json.dump({"visits": []}, f)
|
108 |
|
@@ -111,9 +136,19 @@ class UsageLogger:
|
|
111 |
current_time = datetime.datetime.now().isoformat()
|
112 |
|
113 |
try:
|
114 |
-
#
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
# Append new timestamp
|
119 |
logs["visits"].append({"timestamp": current_time})
|
@@ -122,6 +157,7 @@ class UsageLogger:
|
|
122 |
with open(self.log_file, 'w') as f:
|
123 |
json.dump(logs, f, indent=2)
|
124 |
|
|
|
125 |
return True
|
126 |
except Exception as e:
|
127 |
print(f"Error logging visit: {str(e)}")
|
@@ -132,9 +168,19 @@ class UsageLogger:
|
|
132 |
current_time = datetime.datetime.now().isoformat()
|
133 |
|
134 |
try:
|
135 |
-
#
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
# Append new usage record
|
140 |
logs["visits"].append({
|
@@ -146,6 +192,7 @@ class UsageLogger:
|
|
146 |
with open(self.log_file, 'w') as f:
|
147 |
json.dump(logs, f, indent=2)
|
148 |
|
|
|
149 |
return True
|
150 |
except Exception as e:
|
151 |
print(f"Error logging usage: {str(e)}")
|
@@ -398,8 +445,17 @@ def create_interface():
|
|
398 |
|
399 |
# Black and White Image Interface
|
400 |
with gr.Blocks(title="Image Processor", css=custom_css, theme=gr.themes.Base()) as app:
|
401 |
-
# Log app visit at
|
402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
403 |
|
404 |
with gr.Row(elem_classes="container"):
|
405 |
gr.Markdown("""
|
|
|
96 |
# ------------------- Logger Class ------------------- #
|
97 |
class UsageLogger:
|
98 |
"""Simple logger to record app usage timestamps"""
|
99 |
+
def __init__(self, log_filename="usage_logs.json"):
|
100 |
+
# Get the current working directory for the application
|
101 |
+
try:
|
102 |
+
# Create logs directory if it doesn't exist
|
103 |
+
self.logs_dir = os.path.join(os.getcwd(), "logs")
|
104 |
+
os.makedirs(self.logs_dir, exist_ok=True)
|
105 |
+
self.log_file = os.path.join(self.logs_dir, log_filename)
|
106 |
+
except Exception as e:
|
107 |
+
# Fallback to a directory we should have write access to
|
108 |
+
print(f"Error creating logs directory: {str(e)}")
|
109 |
+
temp_dir = tempfile.gettempdir()
|
110 |
+
self.logs_dir = os.path.join(temp_dir, "image_processor_logs")
|
111 |
+
os.makedirs(self.logs_dir, exist_ok=True)
|
112 |
+
self.log_file = os.path.join(self.logs_dir, log_filename)
|
113 |
+
|
114 |
+
print(f"Log file location: {self.log_file}")
|
115 |
self.ensure_log_file_exists()
|
116 |
|
117 |
def ensure_log_file_exists(self):
|
118 |
"""Create log file with empty array if it doesn't exist"""
|
119 |
+
try:
|
120 |
+
if not os.path.exists(self.log_file):
|
121 |
+
with open(self.log_file, 'w') as f:
|
122 |
+
json.dump({"visits": []}, f)
|
123 |
+
# Test if file is readable/writable
|
124 |
+
with open(self.log_file, 'r+') as f:
|
125 |
+
pass
|
126 |
+
except Exception as e:
|
127 |
+
print(f"Error accessing log file: {str(e)}")
|
128 |
+
# Create a new log file in a different location as fallback
|
129 |
+
self.log_file = os.path.join(tempfile.gettempdir(), "image_processor_logs.json")
|
130 |
+
print(f"Using fallback log file: {self.log_file}")
|
131 |
with open(self.log_file, 'w') as f:
|
132 |
json.dump({"visits": []}, f)
|
133 |
|
|
|
136 |
current_time = datetime.datetime.now().isoformat()
|
137 |
|
138 |
try:
|
139 |
+
# Make sure log directory exists
|
140 |
+
os.makedirs(os.path.dirname(self.log_file), exist_ok=True)
|
141 |
+
|
142 |
+
# Read existing logs or create new if file doesn't exist or is corrupt
|
143 |
+
try:
|
144 |
+
if os.path.exists(self.log_file) and os.path.getsize(self.log_file) > 0:
|
145 |
+
with open(self.log_file, 'r') as f:
|
146 |
+
logs = json.load(f)
|
147 |
+
else:
|
148 |
+
logs = {"visits": []}
|
149 |
+
except (json.JSONDecodeError, FileNotFoundError):
|
150 |
+
# If file is corrupt or doesn't exist, start fresh
|
151 |
+
logs = {"visits": []}
|
152 |
|
153 |
# Append new timestamp
|
154 |
logs["visits"].append({"timestamp": current_time})
|
|
|
157 |
with open(self.log_file, 'w') as f:
|
158 |
json.dump(logs, f, indent=2)
|
159 |
|
160 |
+
print(f"Visit logged at {current_time}")
|
161 |
return True
|
162 |
except Exception as e:
|
163 |
print(f"Error logging visit: {str(e)}")
|
|
|
168 |
current_time = datetime.datetime.now().isoformat()
|
169 |
|
170 |
try:
|
171 |
+
# Make sure log directory exists
|
172 |
+
os.makedirs(os.path.dirname(self.log_file), exist_ok=True)
|
173 |
+
|
174 |
+
# Read existing logs or create new if file doesn't exist or is corrupt
|
175 |
+
try:
|
176 |
+
if os.path.exists(self.log_file) and os.path.getsize(self.log_file) > 0:
|
177 |
+
with open(self.log_file, 'r') as f:
|
178 |
+
logs = json.load(f)
|
179 |
+
else:
|
180 |
+
logs = {"visits": []}
|
181 |
+
except (json.JSONDecodeError, FileNotFoundError):
|
182 |
+
# If file is corrupt or doesn't exist, start fresh
|
183 |
+
logs = {"visits": []}
|
184 |
|
185 |
# Append new usage record
|
186 |
logs["visits"].append({
|
|
|
192 |
with open(self.log_file, 'w') as f:
|
193 |
json.dump(logs, f, indent=2)
|
194 |
|
195 |
+
print(f"Feature usage logged: {feature_type} at {current_time}")
|
196 |
return True
|
197 |
except Exception as e:
|
198 |
print(f"Error logging usage: {str(e)}")
|
|
|
445 |
|
446 |
# Black and White Image Interface
|
447 |
with gr.Blocks(title="Image Processor", css=custom_css, theme=gr.themes.Base()) as app:
|
448 |
+
# Log app visit at launch
|
449 |
+
def log_application_visit():
|
450 |
+
print("Application loaded - logging visit")
|
451 |
+
success = logger.log_visit()
|
452 |
+
if success:
|
453 |
+
print("Visit logged successfully")
|
454 |
+
else:
|
455 |
+
print("Failed to log visit")
|
456 |
+
return None
|
457 |
+
|
458 |
+
app.load(fn=log_application_visit, inputs=None, outputs=None)
|
459 |
|
460 |
with gr.Row(elem_classes="container"):
|
461 |
gr.Markdown("""
|