Spaces:
Configuration error
Configuration error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647fake-logo-backend/app.pyimport gradio as grimport numpy as npimport tensorflow as tf from PILimport Imageimport osimport timeimport csvPathsMODEL_PATH = "model/best_float32.tflite" LABEL_PATH = "model/labels.txt" LOGS_PATH = "logs/predictions.csv"Load labelswith open(LABEL_PATH, "r") as f: labels = [line.strip() for line in f.readlines()]Load TFLite modeltflite_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()Make sure logs directory existsos.makedirs("logs", exist_ok=True)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_arraydef 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]# Loglog_prediction(label, confidence)return f"Prediction: {label} (Confidence: {confidence*100:.2f}%)", confidencedef 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}%"])def download_logs(): return LOGS_PATHwith 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])gr.Markdown("## Logs Dashboard")download_button = gr.File(label="Download Report")download_button.render()download_button.update(value=download_logs())if name == 'main': demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172import gradio as grimport numpy as npimport tensorflow as tffrom PIL import Imageimport osimport timeimport csv# PathsMODEL_PATH = "model/best_float32.tflite"LABEL_PATH = "model/labels.txt"LOGS_PATH = "logs/predictions.csv"# Load labelswith open(LABEL_PATH, "r") as f:labels = [line.strip() for line in f.readlines()]# Load TFLite modeltflite_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()# Make sure logs directory existsos.makedirs("logs", exist_ok=True)def preprocess_image(image: Image.Image) -> np.ndarray:image = image.resize((224, 224))img_array = np.array(image).astype(np.float32) / 255.0img_array = np.expand_dims(img_array, axis=0) # (1, 224, 224, 3)return img_arraydef 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}%"])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]# Log predictionlog_prediction(label, confidence)return f"Prediction: {label} (Confidence: {confidence*100:.2f}%)", confidencedef download_logs():return LOGS_PATHwith 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])gr.Markdown("## Logs Dashboard")download_button = gr.File(label="Download Report")# Set the file path for downloaddownload_button.update(value=download_logs())if _name_ == '_main_':demo.launch()
app.py+61-36