random2222 commited on
Commit
1cb0feb
·
verified ·
1 Parent(s): 4ac7149

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -1
app.py CHANGED
@@ -5,6 +5,9 @@ import gradio as gr
5
  from PIL import Image
6
  import tempfile
7
  from typing import Union, Tuple
 
 
 
8
 
9
  # Custom CSS for styling the interface
10
  custom_css = """
@@ -90,6 +93,67 @@ button.primary:hover {
90
  # Enable OpenCL for better performance
91
  cv2.ocl.setUseOpenCL(True)
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  # ------------------- Theme Setup ------------------- #
94
  def create_custom_theme():
95
  """Create a custom dark theme for the interface"""
@@ -280,6 +344,9 @@ def process_video_sketch(video_path, intensity=255, blur_ksize=21, sigma=0):
280
  # ------------------- Gradio Interface Functions ------------------- #
281
  def black_white_image(image, threshold_method, threshold_value):
282
  """Process image with black and white filter for Gradio"""
 
 
 
283
  if threshold_method != "manual":
284
  threshold_value = 0 # Not used for adaptive or Otsu
285
 
@@ -288,6 +355,9 @@ def black_white_image(image, threshold_method, threshold_value):
288
 
289
  def black_white_video(video, threshold_method, threshold_value):
290
  """Process video with black and white filter for Gradio"""
 
 
 
291
  if threshold_method != "manual":
292
  threshold_value = 0 # Not used for adaptive or Otsu
293
 
@@ -299,11 +369,17 @@ def black_white_video(video, threshold_method, threshold_value):
299
 
300
  def sketch_image(image, intensity, blur_ksize, sigma):
301
  """Process image with pencil sketch filter for Gradio"""
 
 
 
302
  result = process_image_sketch(image, intensity, blur_ksize, sigma)
303
  return Image.fromarray(result)
304
 
305
  def sketch_video(video, intensity, blur_ksize, sigma):
306
  """Process video with pencil sketch filter for Gradio"""
 
 
 
307
  message, output_path = process_video_sketch(video, intensity, blur_ksize, sigma)
308
  if output_path:
309
  return output_path
@@ -322,6 +398,9 @@ def create_interface():
322
 
323
  # Black and White Image Interface
324
  with gr.Blocks(title="Image Processor", css=custom_css, theme=gr.themes.Base()) as app:
 
 
 
325
  with gr.Row(elem_classes="container"):
326
  gr.Markdown("""
327
  # Image and Video Processor
@@ -533,4 +612,4 @@ def create_interface():
533
  # ------------------- Launch App ------------------- #
534
  if __name__ == "__main__":
535
  app = create_interface()
536
- app.launch()
 
5
  from PIL import Image
6
  import tempfile
7
  from typing import Union, Tuple
8
+ import json
9
+ import datetime
10
+ import pathlib
11
 
12
  # Custom CSS for styling the interface
13
  custom_css = """
 
93
  # Enable OpenCL for better performance
94
  cv2.ocl.setUseOpenCL(True)
95
 
96
+ # ------------------- Logger Class ------------------- #
97
+ class UsageLogger:
98
+ """Simple logger to record app usage timestamps"""
99
+ def __init__(self, log_file="usage_logs.json"):
100
+ self.log_file = log_file
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
+ if not os.path.exists(self.log_file):
106
+ with open(self.log_file, 'w') as f:
107
+ json.dump({"visits": []}, f)
108
+
109
+ def log_visit(self):
110
+ """Log a timestamp when the app is visited/used"""
111
+ current_time = datetime.datetime.now().isoformat()
112
+
113
+ try:
114
+ # Read existing logs
115
+ with open(self.log_file, 'r') as f:
116
+ logs = json.load(f)
117
+
118
+ # Append new timestamp
119
+ logs["visits"].append({"timestamp": current_time})
120
+
121
+ # Write updated logs
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)}")
128
+ return False
129
+
130
+ def log_usage(self, feature_type):
131
+ """Log when a specific feature is used"""
132
+ current_time = datetime.datetime.now().isoformat()
133
+
134
+ try:
135
+ # Read existing logs
136
+ with open(self.log_file, 'r') as f:
137
+ logs = json.load(f)
138
+
139
+ # Append new usage record
140
+ logs["visits"].append({
141
+ "timestamp": current_time,
142
+ "feature": feature_type
143
+ })
144
+
145
+ # Write updated logs
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)}")
152
+ return False
153
+
154
+ # Create a global logger instance
155
+ logger = UsageLogger()
156
+
157
  # ------------------- Theme Setup ------------------- #
158
  def create_custom_theme():
159
  """Create a custom dark theme for the interface"""
 
344
  # ------------------- Gradio Interface Functions ------------------- #
345
  def black_white_image(image, threshold_method, threshold_value):
346
  """Process image with black and white filter for Gradio"""
347
+ # Log the usage of this feature
348
+ logger.log_usage("black_white_image")
349
+
350
  if threshold_method != "manual":
351
  threshold_value = 0 # Not used for adaptive or Otsu
352
 
 
355
 
356
  def black_white_video(video, threshold_method, threshold_value):
357
  """Process video with black and white filter for Gradio"""
358
+ # Log the usage of this feature
359
+ logger.log_usage("black_white_video")
360
+
361
  if threshold_method != "manual":
362
  threshold_value = 0 # Not used for adaptive or Otsu
363
 
 
369
 
370
  def sketch_image(image, intensity, blur_ksize, sigma):
371
  """Process image with pencil sketch filter for Gradio"""
372
+ # Log the usage of this feature
373
+ logger.log_usage("sketch_image")
374
+
375
  result = process_image_sketch(image, intensity, blur_ksize, sigma)
376
  return Image.fromarray(result)
377
 
378
  def sketch_video(video, intensity, blur_ksize, sigma):
379
  """Process video with pencil sketch filter for Gradio"""
380
+ # Log the usage of this feature
381
+ logger.log_usage("sketch_video")
382
+
383
  message, output_path = process_video_sketch(video, intensity, blur_ksize, sigma)
384
  if output_path:
385
  return output_path
 
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 startup
402
+ app.load(fn=logger.log_visit, inputs=None, outputs=None)
403
+
404
  with gr.Row(elem_classes="container"):
405
  gr.Markdown("""
406
  # Image and Video Processor
 
612
  # ------------------- Launch App ------------------- #
613
  if __name__ == "__main__":
614
  app = create_interface()
615
+ app.launch()