Spaces:
Configuration error
Configuration error
Update app.py
Browse files
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
|
| 6 |
-
import Image
|
| 7 |
import os
|
| 8 |
import time
|
| 9 |
import csv
|
| 10 |
|
| 11 |
-
Paths
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
Load labels
|
| 16 |
-
|
| 17 |
-
with open(LABEL_PATH, "r") as f: labels = [line.strip() for line in f.readlines()]
|
| 18 |
|
| 19 |
-
Load
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
os.makedirs("logs", exist_ok=True)
|
| 26 |
|
| 27 |
-
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|