Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1 @@
|
|
| 1 |
-
|
| 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, description):
|
| 9 |
-
if file is None or not description.strip():
|
| 10 |
-
return "", "", "⚠️ لطفاً فایل اکسل و توضیح پروژه را وارد کنید."
|
| 11 |
-
|
| 12 |
-
try:
|
| 13 |
-
df = pd.read_excel(file.name, header=2)
|
| 14 |
-
except Exception as e:
|
| 15 |
-
return "", "", f"❌ خطا در خواندن فایل اکسل: {str(e)}"
|
| 16 |
-
|
| 17 |
-
column_mapping = {
|
| 18 |
-
"code": ["کد", "کد فهرست بها"],
|
| 19 |
-
"description": ["عنوان", "شرح کالا", "نام کالا"],
|
| 20 |
-
"quantity": ["تعداد", "تعداد مورد نیاز"],
|
| 21 |
-
"unit": ["واحد", "واحد شمارش", "نوع واحد"]
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
resolved = {}
|
| 25 |
-
for key, options in column_mapping.items():
|
| 26 |
-
found = next((col for col in options if col in df.columns), None)
|
| 27 |
-
if not found:
|
| 28 |
-
return "", "", f"❌ ستون «{key}» در فایل پیدا نشد."
|
| 29 |
-
resolved[key] = found
|
| 30 |
-
|
| 31 |
-
materials = []
|
| 32 |
-
constraints = []
|
| 33 |
-
for _, row in df.iterrows():
|
| 34 |
-
try:
|
| 35 |
-
qty = float(row[resolved["quantity"]])
|
| 36 |
-
if pd.isna(qty) or qty <= 0:
|
| 37 |
-
continue
|
| 38 |
-
except:
|
| 39 |
-
continue
|
| 40 |
-
|
| 41 |
-
material = {
|
| 42 |
-
"code": str(row[resolved["code"]]).strip(),
|
| 43 |
-
"description": str(row[resolved["description"]]).strip(),
|
| 44 |
-
"quantity": qty,
|
| 45 |
-
"unit": str(row[resolved["unit"]]).strip()
|
| 46 |
-
}
|
| 47 |
-
materials.append(material)
|
| 48 |
-
|
| 49 |
-
constraints.append(f"{material['description']} ({material['quantity']} {material['unit']})")
|
| 50 |
-
|
| 51 |
-
# ذخیره در فایل جیسان با توضیح پروژه
|
| 52 |
-
try:
|
| 53 |
-
if os.path.exists(JSON_PATH):
|
| 54 |
-
with open(JSON_PATH, "r", encoding="utf-8") as f:
|
| 55 |
-
existing = json.load(f)
|
| 56 |
-
else:
|
| 57 |
-
existing = []
|
| 58 |
-
|
| 59 |
-
new_record = {
|
| 60 |
-
"project_description": description.strip(),
|
| 61 |
-
"items": materials
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
existing.append(new_record)
|
| 65 |
-
|
| 66 |
-
with open(JSON_PATH, "w", encoding="utf-8") as f:
|
| 67 |
-
json.dump(existing, f, indent=2, ensure_ascii=False)
|
| 68 |
-
|
| 69 |
-
except Exception as e:
|
| 70 |
-
return "", "", f"❌ خطا در ذخیره فایل JSON: {str(e)}"
|
| 71 |
-
|
| 72 |
-
return "\n".join(constraints), f"✅ {len(materials)} قلم ثبت شد.", JSON_PATH
|
| 73 |
-
|
| 74 |
-
def download_json():
|
| 75 |
-
return JSON_PATH if os.path.exists(JSON_PATH) else None
|
| 76 |
-
|
| 77 |
-
with gr.Blocks(title="مدیریت مواد پروژه") as demo:
|
| 78 |
-
gr.Markdown("## 📋 بارگذاری فایل برآورد، توصیف پروژه و استخراج اقلام")
|
| 79 |
-
|
| 80 |
-
with gr.Row():
|
| 81 |
-
file_input = gr.File(label="آپلود فایل اکسل")
|
| 82 |
-
project_desc = gr.Textbox(label="📝 توضیح پروژه", placeholder="مثلاً: پروژه خط ۲۰ کیلوولت فاز دوم")
|
| 83 |
-
|
| 84 |
-
upload_btn = gr.Button("📤 بارگذاری و ثبت")
|
| 85 |
-
download_btn = gr.Button("📥 دانلود فایل JSON")
|
| 86 |
-
|
| 87 |
-
status_output = gr.Textbox(label="وضعیت", lines=2)
|
| 88 |
-
constraints_output = gr.Textbox(label="🔍 قیود استخراجشده", lines=10)
|
| 89 |
-
json_file_output = gr.File(label="فایل JSON خروجی", visible=True)
|
| 90 |
-
|
| 91 |
-
upload_btn.click(parse_excel, inputs=[file_input, project_desc], outputs=[constraints_output, status_output, json_file_output])
|
| 92 |
-
download_btn.click(download_json, outputs=json_file_output)
|
| 93 |
-
|
| 94 |
-
if __name__ == "__main__":
|
| 95 |
-
demo.launch()
|
|
|
|
| 1 |
+
d
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|