Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| import joblib | |
| import sqlite3 | |
| from datetime import datetime, timedelta | |
| # Load model and encoder | |
| model = joblib.load("anomaly_model.pkl") | |
| label_encoder = joblib.load("label_encoder.pkl") | |
| df = pd.read_csv("cleaned_dataset.csv") | |
| df = df[['Product Name', 'Price']].dropna() | |
| # Connect to SQLite | |
| def connect_to_db(): | |
| return sqlite3.connect("sql.db") | |
| def detect_and_act(product_name, bid_price): | |
| vendor_id = 1 # hardcoded | |
| try: | |
| conn = connect_to_db() | |
| cursor = conn.cursor() | |
| bid_price = float(bid_price) | |
| encoded_product = label_encoder.transform([product_name])[0] | |
| input_data = np.array([[encoded_product, bid_price]]) | |
| prediction = model.predict(input_data) | |
| product_data = df[df['Product Name'] == product_name] | |
| mean_price = product_data['Price'].mean() | |
| std_price = product_data['Price'].std() | |
| min_range = mean_price - 2 * std_price | |
| max_range = mean_price + 2 * std_price | |
| bid_valid = min_range <= bid_price <= max_range | |
| # Check if vendor exists | |
| cursor.execute("SELECT * FROM vendors WHERE vendor_id = ?", (vendor_id,)) | |
| vendor = cursor.fetchone() | |
| if not vendor: | |
| return "⚠️ Vendor not found!" | |
| # Get latest bid by this vendor | |
| cursor.execute("SELECT bid_id FROM bids WHERE vendor_id = ? ORDER BY bid_id DESC LIMIT 1", (vendor_id,)) | |
| bid = cursor.fetchone() | |
| if not bid: | |
| return "⚠️ No bid found in the system to associate with a vendor." | |
| bid_id = bid[0] | |
| if prediction[0] == -1 and not bid_valid: | |
| cursor.execute("UPDATE vendors SET anomaly_count = anomaly_count + 1 WHERE vendor_id = ?", (vendor_id,)) | |
| conn.commit() | |
| cursor.execute("SELECT anomaly_count FROM vendors WHERE vendor_id = ?", (vendor_id,)) | |
| anomaly_count = cursor.fetchone()[0] | |
| if anomaly_count >= 3: | |
| cursor.execute("UPDATE vendors SET blocked_status = 1 WHERE vendor_id = ?", (vendor_id,)) | |
| conn.commit() | |
| return "❌ Vendor permanently blocked after 3 fake bids." | |
| elif anomaly_count == 2: | |
| block_until = datetime.now() + timedelta(hours=24) | |
| cursor.execute("UPDATE vendors SET suspended_until = ? WHERE vendor_id = ?", | |
| (block_until.strftime("%Y-%m-%d %H:%M:%S"), vendor_id)) | |
| conn.commit() | |
| return "⚠️ Vendor temporarily blocked for 24 hours (2nd fake bid)." | |
| else: | |
| return "⚠️ Fake bid detected. Warning issued." | |
| elif prediction[0] == -1 or not bid_valid: | |
| block_until = datetime.now() + timedelta(hours=24) | |
| cursor.execute("UPDATE vendors SET suspended_until = ? WHERE vendor_id = ?", | |
| (block_until.strftime("%Y-%m-%d %H:%M:%S"), vendor_id)) | |
| conn.commit() | |
| return "⚠️ Bid suspicious. Vendor temporarily blocked for 24 hours." | |
| else: | |
| return "✅ Bid is normal." | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" | |
| finally: | |
| conn.close() | |
| # Gradio Interface | |
| iface = gr.Interface( | |
| fn=detect_and_act, | |
| inputs=["text", "text"], | |
| outputs="text", | |
| title="🛡️ Fake Bid Detection System", | |
| description="Enter Product Name and Bid Price" | |
| ) | |
| iface.launch() |