Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from tabulate import tabulate
|
| 3 |
+
import random
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.cluster import KMeans
|
| 6 |
+
|
| 7 |
+
class ProductSystem:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.product_catalog = {
|
| 10 |
+
"laptop": ["Laptop Model A", "Laptop Model B", "Laptop Model C", "Laptop Model D"],
|
| 11 |
+
"mouse": ["Wireless Mouse", "Gaming Mouse", "Ergonomic Mouse"],
|
| 12 |
+
"charger": ["Laptop Charger A", "Laptop Charger B"],
|
| 13 |
+
"keyboard": ["Mechanical Keyboard", "Wireless Keyboard"],
|
| 14 |
+
"monitor": ["24-inch Monitor", "27-inch Monitor"],
|
| 15 |
+
"printer": ["Laser Printer", "Inkjet Printer"],
|
| 16 |
+
"headphones": ["Noise-Canceling Headphones", "Bluetooth Headphones"],
|
| 17 |
+
"speakers": ["Wireless Speakers", "USB Speakers"],
|
| 18 |
+
"external_storage": ["1TB External HDD", "500GB SSD"]
|
| 19 |
+
}
|
| 20 |
+
self.stock = {
|
| 21 |
+
"Laptop Model A": 5, "Laptop Model B": 10, "Laptop Model C": 2, "Laptop Model D": 0,
|
| 22 |
+
"Wireless Mouse": 15, "Gaming Mouse": 5, "Ergonomic Mouse": 8,
|
| 23 |
+
"Laptop Charger A": 3, "Laptop Charger B": 7,
|
| 24 |
+
"Mechanical Keyboard": 6, "Wireless Keyboard": 4,
|
| 25 |
+
"24-inch Monitor": 5, "27-inch Monitor": 3,
|
| 26 |
+
"Laser Printer": 4, "Inkjet Printer": 6,
|
| 27 |
+
"Noise-Canceling Headphones": 10, "Bluetooth Headphones": 7,
|
| 28 |
+
"Wireless Speakers": 8, "USB Speakers": 9,
|
| 29 |
+
"1TB External HDD": 6, "500GB SSD": 5
|
| 30 |
+
}
|
| 31 |
+
self.resource_stock = {
|
| 32 |
+
"Processor": 20, "RAM": 30, "SSD": 15, "Battery": 10, "Screen": 8,
|
| 33 |
+
"Mouse Sensor": 25, "Keyboard Switches": 40, "Charging Cable": 12,
|
| 34 |
+
"Printer Ink": 20, "Monitor Panel": 10, "Speaker Driver": 15, "Headphone Cushions": 18, "External Storage Chips": 12,
|
| 35 |
+
"GPU": 5, "Motherboard": 7, "Cooling Fan": 10, "Power Supply": 6, "Touchpad": 8
|
| 36 |
+
}
|
| 37 |
+
self.resource_requirements = {
|
| 38 |
+
"laptop": {"Processor": 1, "RAM": 2, "SSD": 1, "Battery": 1, "Screen": 1, "GPU": 1, "Motherboard": 1, "Cooling Fan": 1, "Power Supply": 1, "Touchpad": 1},
|
| 39 |
+
"mouse": {"Mouse Sensor": 1},
|
| 40 |
+
"keyboard": {"Keyboard Switches": 10},
|
| 41 |
+
"charger": {"Charging Cable": 1},
|
| 42 |
+
"monitor": {"Monitor Panel": 1},
|
| 43 |
+
"printer": {"Printer Ink": 2},
|
| 44 |
+
"headphones": {"Headphone Cushions": 2},
|
| 45 |
+
"speakers": {"Speaker Driver": 2},
|
| 46 |
+
"external_storage": {"External Storage Chips": 1}
|
| 47 |
+
}
|
| 48 |
+
self.historical_data = np.array([
|
| 49 |
+
[1, 1, 0, 0, 1],
|
| 50 |
+
[1, 0, 1, 1, 0],
|
| 51 |
+
[1, 1, 1, 0, 1],
|
| 52 |
+
[1, 0, 0, 1, 1],
|
| 53 |
+
[1, 1, 1, 1, 1]
|
| 54 |
+
])
|
| 55 |
+
|
| 56 |
+
def suggest_products(self, user_input):
|
| 57 |
+
primary_products = self.product_catalog.get(user_input, [])[:4]
|
| 58 |
+
|
| 59 |
+
kmeans = KMeans(n_clusters=2, random_state=42, n_init=10)
|
| 60 |
+
kmeans.fit(self.historical_data)
|
| 61 |
+
cluster = kmeans.predict(self.historical_data[-1].reshape(1, -1))[0]
|
| 62 |
+
|
| 63 |
+
recommended_secondary = []
|
| 64 |
+
for i, value in enumerate(self.historical_data[cluster]):
|
| 65 |
+
if value == 1:
|
| 66 |
+
recommended_secondary.append(list(self.product_catalog.keys())[i])
|
| 67 |
+
|
| 68 |
+
secondary_products = []
|
| 69 |
+
for product in recommended_secondary:
|
| 70 |
+
secondary_products.extend(self.product_catalog.get(product, []))
|
| 71 |
+
|
| 72 |
+
secondary_products = [prod for prod in secondary_products if prod not in primary_products]
|
| 73 |
+
secondary_products = random.sample(secondary_products, min(5, len(secondary_products)))
|
| 74 |
+
|
| 75 |
+
return primary_products, secondary_products
|
| 76 |
+
|
| 77 |
+
def check_resources(self, product_type, quantity_needed):
|
| 78 |
+
required_resources = self.resource_requirements.get(product_type, {})
|
| 79 |
+
resource_data = []
|
| 80 |
+
|
| 81 |
+
for resource, qty_per_unit in required_resources.items():
|
| 82 |
+
total_needed = qty_per_unit * quantity_needed
|
| 83 |
+
available = self.resource_stock.get(resource, 0)
|
| 84 |
+
restock_needed = max(0, total_needed - available)
|
| 85 |
+
resource_data.append([resource, available, total_needed, restock_needed])
|
| 86 |
+
|
| 87 |
+
return resource_data
|
| 88 |
+
|
| 89 |
+
system = ProductSystem()
|
| 90 |
+
|
| 91 |
+
def interface_main(product_type, quantity):
|
| 92 |
+
primary, secondary = system.suggest_products(product_type)
|
| 93 |
+
table_data = system.check_resources(product_type, quantity)
|
| 94 |
+
resource_table = tabulate(table_data, headers=["Resource", "Available", "Required", "Restock"], tablefmt="grid")
|
| 95 |
+
return primary, secondary, resource_table
|
| 96 |
+
|
| 97 |
+
demo = gr.Interface(
|
| 98 |
+
fn=interface_main,
|
| 99 |
+
inputs=[
|
| 100 |
+
gr.Dropdown(choices=list(system.product_catalog.keys()), label="Select Primary Product Type"),
|
| 101 |
+
gr.Number(label="Enter Quantity", value=1)
|
| 102 |
+
],
|
| 103 |
+
outputs=[
|
| 104 |
+
gr.Textbox(label="Primary Products"),
|
| 105 |
+
gr.Textbox(label="Recommended Secondary Products"),
|
| 106 |
+
gr.Textbox(label="Resource Requirements Table")
|
| 107 |
+
],
|
| 108 |
+
title="Product Manufacturing & Recommendation System",
|
| 109 |
+
description="Suggests products and checks resource availability using AI clustering (KMeans)."
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
demo.launch()
|