NsManick commited on
Commit
ccd137f
·
verified ·
1 Parent(s): bc3e06a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -36
app.py CHANGED
@@ -1,47 +1,72 @@
1
- fake-logo-backend/app.py
2
-
3
  import gradio as gr
4
  import numpy as np
5
- import tensorflow as tf from PIL
6
- import Image
7
  import os
8
  import time
9
  import csv
10
 
11
- Paths
12
-
13
- MODEL_PATH = "model/best_float32.tflite" LABEL_PATH = "model/labels.txt" LOGS_PATH = "logs/predictions.csv"
14
-
15
- Load labels
16
-
17
- with open(LABEL_PATH, "r") as f: labels = [line.strip() for line in f.readlines()]
18
 
19
- Load TFLite model
 
 
20
 
21
- tflite_interpreter = tf.lite.Interpreter(model_path=MODEL_PATH) tflite_interpreter.allocate_tensors() input_details = tflite_interpreter.get_input_details() output_details = tflite_interpreter.get_output_details()
22
-
23
- Make sure logs directory exists
 
 
24
 
 
25
  os.makedirs("logs", exist_ok=True)
26
 
27
- def preprocess_image(image: Image.Image) -> np.ndarray: image = image.resize((224, 224)) img_array = np.array(image).astype(np.float32) / 255.0 img_array = np.expand_dims(img_array, axis=0) # (1, 224, 224, 3) return img_array
28
-
29
- def predict(image: Image.Image): input_tensor = preprocess_image(image) tflite_interpreter.set_tensor(input_details[0]['index'], input_tensor) tflite_interpreter.invoke() output_data = tflite_interpreter.get_tensor(output_details[0]['index']) predicted_index = np.argmax(output_data) confidence = float(np.max(output_data)) label = labels[predicted_index]
30
-
31
- # Log
32
- log_prediction(label, confidence)
33
-
34
- return f"Prediction: {label} (Confidence: {confidence*100:.2f}%)", confidence
35
-
36
- def log_prediction(label, confidence): with open(LOGS_PATH, "a", newline="") as f: writer = csv.writer(f) writer.writerow([time.strftime("%Y-%m-%d %H:%M:%S"), label, f"{confidence*100:.2f}%"])
37
-
38
- def download_logs(): return LOGS_PATH
39
-
40
- with gr.Blocks() as demo: gr.Markdown("# Fake Logo Detector – Gradio App") with gr.Row(): image_input = gr.Image(type="pil", label="Upload or Snap Logo") result_output = gr.Textbox(label="Result") confidence_bar = gr.Slider(0, 1, label="Confidence", interactive=False) predict_btn = gr.Button("Scan Logo") predict_btn.click(fn=predict, inputs=image_input, outputs=[result_output, confidence_bar])
41
-
42
- gr.Markdown("## Logs Dashboard")
43
- download_button = gr.File(label="Download Report")
44
- download_button.render()
45
- download_button.update(value=download_logs())
46
-
47
- if name == 'main': demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
+ import tensorflow as tf
4
+ from PIL import Image
5
  import os
6
  import time
7
  import csv
8
 
9
+ # Paths
10
+ MODEL_PATH = "model/best_float32.tflite"
11
+ LABEL_PATH = "model/labels.txt"
12
+ LOGS_PATH = "logs/predictions.csv"
 
 
 
13
 
14
+ # Load labels
15
+ with open(LABEL_PATH, "r") as f:
16
+ labels = [line.strip() for line in f.readlines()]
17
 
18
+ # Load TFLite model
19
+ tflite_interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
20
+ tflite_interpreter.allocate_tensors()
21
+ input_details = tflite_interpreter.get_input_details()
22
+ output_details = tflite_interpreter.get_output_details()
23
 
24
+ # Make sure logs directory exists
25
  os.makedirs("logs", exist_ok=True)
26
 
27
+ def preprocess_image(image: Image.Image) -> np.ndarray:
28
+ image = image.resize((224, 224))
29
+ img_array = np.array(image).astype(np.float32) / 255.0
30
+ img_array = np.expand_dims(img_array, axis=0) # (1, 224, 224, 3)
31
+ return img_array
32
+
33
+ def log_prediction(label, confidence):
34
+ with open(LOGS_PATH, "a", newline="") as f:
35
+ writer = csv.writer(f)
36
+ writer.writerow([time.strftime("%Y-%m-%d %H:%M:%S"), label, f"{confidence*100:.2f}%"])
37
+
38
+ def predict(image: Image.Image):
39
+ input_tensor = preprocess_image(image)
40
+ tflite_interpreter.set_tensor(input_details[0]['index'], input_tensor)
41
+ tflite_interpreter.invoke()
42
+ output_data = tflite_interpreter.get_tensor(output_details[0]['index'])
43
+ predicted_index = np.argmax(output_data)
44
+ confidence = float(np.max(output_data))
45
+ label = labels[predicted_index]
46
+
47
+ # Log prediction
48
+ log_prediction(label, confidence)
49
+
50
+ return f"Prediction: {label} (Confidence: {confidence*100:.2f}%)", confidence
51
+
52
+ def download_logs():
53
+ return LOGS_PATH
54
+
55
+ with gr.Blocks() as demo:
56
+ gr.Markdown("# Fake Logo Detector – Gradio App")
57
+
58
+ with gr.Row():
59
+ image_input = gr.Image(type="pil", label="Upload or Snap Logo")
60
+ result_output = gr.Textbox(label="Result")
61
+ confidence_bar = gr.Slider(0, 1, label="Confidence", interactive=False)
62
+
63
+ predict_btn = gr.Button("Scan Logo")
64
+ predict_btn.click(fn=predict, inputs=image_input, outputs=[result_output, confidence_bar])
65
+
66
+ gr.Markdown("## Logs Dashboard")
67
+ download_button = gr.File(label="Download Report")
68
+ # Set the file path for download
69
+ download_button.update(value=download_logs())
70
+
71
+ if _name_ == '_main_':
72
+ demo.launch()