Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1 +1,124 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
JSON_PATH = "/tmp/material_db.json"
|
| 7 |
+
|
| 8 |
+
def parse_excel(file):
|
| 9 |
+
try:
|
| 10 |
+
df = pd.read_excel(file.name, header=2)
|
| 11 |
+
except Exception as e:
|
| 12 |
+
return None, None, f"❌ خطا در خواندن فایل اکسل: {str(e)}"
|
| 13 |
+
|
| 14 |
+
column_mapping = {
|
| 15 |
+
"code": ["کد", "کد فهرست بها"],
|
| 16 |
+
"description": ["عنوان", "شرح کالا", "نام کالا"],
|
| 17 |
+
"quantity": ["تعداد", "تعداد مورد نیاز"],
|
| 18 |
+
"unit": ["واحد", "واحد شمارش", "نوع واحد"]
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
resolved = {}
|
| 22 |
+
for key, options in column_mapping.items():
|
| 23 |
+
found = next((col for col in options if col in df.columns), None)
|
| 24 |
+
if not found:
|
| 25 |
+
return None, None, f"❌ ستون «{key}» در فایل پیدا نشد."
|
| 26 |
+
resolved[key] = found
|
| 27 |
+
|
| 28 |
+
materials = []
|
| 29 |
+
constraints = []
|
| 30 |
+
for _, row in df.iterrows():
|
| 31 |
+
try:
|
| 32 |
+
qty = float(row[resolved["quantity"]])
|
| 33 |
+
if pd.isna(qty) or qty <= 0:
|
| 34 |
+
continue
|
| 35 |
+
except:
|
| 36 |
+
continue
|
| 37 |
+
|
| 38 |
+
material = {
|
| 39 |
+
"code": str(row[resolved["code"]]).strip(),
|
| 40 |
+
"description": str(row[resolved["description"]]).strip(),
|
| 41 |
+
"quantity": qty,
|
| 42 |
+
"unit": str(row[resolved["unit"]]).strip()
|
| 43 |
+
}
|
| 44 |
+
materials.append(material)
|
| 45 |
+
constraints.append(f"{material['description']} ({material['quantity']} {material['unit']})")
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
if os.path.exists(JSON_PATH):
|
| 49 |
+
with open(JSON_PATH, "r", encoding="utf-8") as f:
|
| 50 |
+
existing = json.load(f)
|
| 51 |
+
else:
|
| 52 |
+
existing = []
|
| 53 |
+
|
| 54 |
+
new_materials = [m for m in materials if m not in existing]
|
| 55 |
+
existing.extend(new_materials)
|
| 56 |
+
|
| 57 |
+
with open(JSON_PATH, "w", encoding="utf-8") as f:
|
| 58 |
+
json.dump(existing, f, indent=2, ensure_ascii=False)
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return None, None, f"❌ خطا در ذخیره فایل JSON: {str(e)}"
|
| 62 |
+
|
| 63 |
+
return constraints, len(new_materials), f"✅ {len(new_materials)} مورد جدید با موفقیت ثبت شدند."
|
| 64 |
+
|
| 65 |
+
def load_json_file(file):
|
| 66 |
+
try:
|
| 67 |
+
with open(file.name, "r", encoding="utf-8") as f:
|
| 68 |
+
data = json.load(f)
|
| 69 |
+
with open(JSON_PATH, "w", encoding="utf-8") as f_out:
|
| 70 |
+
json.dump(data, f_out, indent=2, ensure_ascii=False)
|
| 71 |
+
return f"✅ فایل JSON با {len(data)} مورد با موفقیت بارگذاری شد."
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"❌ خطا در بارگذاری فایل JSON: {str(e)}"
|
| 74 |
+
|
| 75 |
+
def read_json_descriptions():
|
| 76 |
+
if not os.path.exists(JSON_PATH):
|
| 77 |
+
return "ℹ️ تاکنون هیچ موردی ثبت نشده است."
|
| 78 |
+
try:
|
| 79 |
+
with open(JSON_PATH, "r", encoding="utf-8") as f:
|
| 80 |
+
data = json.load(f)
|
| 81 |
+
descriptions = [f"{item['description']} ({item['quantity']} {item['unit']})" for item in data]
|
| 82 |
+
return "\n".join(descriptions)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"❌ خطا در خواندن فایل JSON: {str(e)}"
|
| 85 |
+
|
| 86 |
+
with gr.Blocks(title="مدیریت مواد پروژه - نسخه 0.1.1") as demo:
|
| 87 |
+
with gr.Tabs():
|
| 88 |
+
with gr.TabItem("📤 بارگذاری فایل اکسل"):
|
| 89 |
+
gr.Markdown("## استخراج اقلام از فایل اکسل")
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
file_input = gr.File(label="آپلود فایل اکسل")
|
| 93 |
+
upload_btn = gr.Button("📤 بارگذاری و ثبت")
|
| 94 |
+
|
| 95 |
+
status_output = gr.Textbox(label="وضعیت", lines=2)
|
| 96 |
+
constraints_output = gr.Textbox(label="🔍 قیود استخراجشده", lines=10)
|
| 97 |
+
|
| 98 |
+
def handle_upload(file):
|
| 99 |
+
if file is None:
|
| 100 |
+
return "", "", "⚠️ لطفاً یک فایل اکسل انتخاب کنید."
|
| 101 |
+
constraints, count, msg = parse_excel(file)
|
| 102 |
+
joined = "\n".join(constraints) if constraints else ""
|
| 103 |
+
return msg, joined
|
| 104 |
+
|
| 105 |
+
upload_btn.click(handle_upload, inputs=[file_input], outputs=[status_output, constraints_output])
|
| 106 |
+
|
| 107 |
+
with gr.TabItem("📂 بارگذاری فایل JSON"):
|
| 108 |
+
gr.Markdown("## بارگذاری اولیه اطلاعات ذخیرهشده")
|
| 109 |
+
|
| 110 |
+
json_input = gr.File(label="فایل JSON")
|
| 111 |
+
load_btn = gr.Button("📥 بارگذاری")
|
| 112 |
+
|
| 113 |
+
load_status = gr.Textbox(label="نتیجه", lines=2)
|
| 114 |
+
load_btn.click(load_json_file, inputs=[json_input], outputs=[load_status])
|
| 115 |
+
|
| 116 |
+
with gr.TabItem("📑 نمایش اقلام ذخیرهشده"):
|
| 117 |
+
gr.Markdown("## فهرست اقلام ثبتشده در فایل JSON")
|
| 118 |
+
refresh_btn = gr.Button("🔄 بازخوانی")
|
| 119 |
+
json_display = gr.Textbox(label="موارد موجود", lines=20)
|
| 120 |
+
|
| 121 |
+
refresh_btn.click(read_json_descriptions, outputs=[json_display])
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
demo.launch()
|