Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,100 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
-
from utils import parse_excel_file, save_project
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
return "❗ لطفاً فایل اکسل و توضیح پروژه را وارد کنید."
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
except Exception as e:
|
| 12 |
-
return str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
try:
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
except Exception as e:
|
| 17 |
-
return str(e)
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
with gr.
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
description_input = gr.Textbox(label="توضیح پروژه", placeholder="مثلاً: پروژه تیرکوبی شهریور")
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import json
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
+
# 🔧 مسیر فایل ذخیره مواد
|
| 7 |
+
JSON_DB_PATH = "material_db.json"
|
|
|
|
| 8 |
|
| 9 |
+
# 📦 ایجاد فایل JSON اگر موجود نبود
|
| 10 |
+
if not os.path.exists(JSON_DB_PATH):
|
| 11 |
+
with open(JSON_DB_PATH, "w", encoding="utf-8") as f:
|
| 12 |
+
json.dump([], f, indent=2, ensure_ascii=False)
|
| 13 |
+
|
| 14 |
+
# 📚 استخراج مواد از اکسل
|
| 15 |
+
def parse_excel_file(file):
|
| 16 |
try:
|
| 17 |
+
df = pd.read_excel(file.name, header=2)
|
| 18 |
except Exception as e:
|
| 19 |
+
return "", f"❌ خطا در خواندن فایل اکسل: {str(e)}"
|
| 20 |
+
|
| 21 |
+
column_mapping = {
|
| 22 |
+
"کد": ["کد فهرست بها", "کد"],
|
| 23 |
+
"عنوان": ["عنوان", "شرح کالا", "نام کالا"],
|
| 24 |
+
"تعداد": ["تعداد", "تعداد مورد نیاز"],
|
| 25 |
+
"واحد": ["واحد", "واحد شمارش", "نوع واحد"]
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
resolved_columns = {}
|
| 29 |
+
for key, options in column_mapping.items():
|
| 30 |
+
found = next((col for col in options if col in df.columns), None)
|
| 31 |
+
if not found:
|
| 32 |
+
return "", f"❌ ستون «{key}» در فایل پیدا نشد."
|
| 33 |
+
resolved_columns[key] = found
|
| 34 |
+
|
| 35 |
+
materials = []
|
| 36 |
+
conditions = []
|
| 37 |
+
|
| 38 |
+
for _, row in df.iterrows():
|
| 39 |
+
try:
|
| 40 |
+
quantity = float(row[resolved_columns["تعداد"]])
|
| 41 |
+
if pd.isna(quantity) or quantity <= 0:
|
| 42 |
+
continue
|
| 43 |
+
except:
|
| 44 |
+
continue
|
| 45 |
+
|
| 46 |
+
code = str(row[resolved_columns["کد"]]).strip()
|
| 47 |
+
desc = str(row[resolved_columns["عنوان"]]).strip()
|
| 48 |
+
unit = str(row[resolved_columns["واحد"]]).strip()
|
| 49 |
+
|
| 50 |
+
material = {
|
| 51 |
+
"code": code,
|
| 52 |
+
"description": desc,
|
| 53 |
+
"quantity": quantity,
|
| 54 |
+
"unit": unit
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
materials.append(material)
|
| 58 |
+
conditions.append(f"{desc}، به مقدار {quantity} {unit}")
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
with open(JSON_DB_PATH, "r", encoding="utf-8") as f:
|
| 62 |
+
db = json.load(f)
|
| 63 |
+
existing_descriptions = [m["description"] for m in db]
|
| 64 |
+
duplicate = any(m["description"] in existing_descriptions for m in materials)
|
| 65 |
+
if duplicate:
|
| 66 |
+
return "", "⚠️ پروژهای با این توصیف قبلاً ثبت شده است."
|
| 67 |
+
except:
|
| 68 |
+
return "", "❌ خطا در بررسی پایگاهداده"
|
| 69 |
|
| 70 |
try:
|
| 71 |
+
db.extend(materials)
|
| 72 |
+
with open(JSON_DB_PATH, "w", encoding="utf-8") as f:
|
| 73 |
+
json.dump(db, f, indent=2, ensure_ascii=False)
|
| 74 |
except Exception as e:
|
| 75 |
+
return "", f"❌ خطا در ذخیره فایل JSON: {str(e)}"
|
| 76 |
+
|
| 77 |
+
return "\n".join(conditions), "✅ مواد با موفقیت ثبت شدند."
|
| 78 |
+
|
| 79 |
|
| 80 |
+
# 🎨 رابط گرافیکی Gradio
|
| 81 |
+
with gr.Blocks(title="مدیریت مواد پروژه") as demo:
|
| 82 |
+
gr.Markdown("## 📁 بارگذاری فایل برآورد")
|
| 83 |
|
| 84 |
+
with gr.Tabs():
|
| 85 |
+
with gr.Tab("📤 بارگذاری"):
|
| 86 |
+
file_input = gr.File(label="فایل اکسل با عنوان در سطر سوم")
|
| 87 |
+
upload_btn = gr.Button("استخراج و ثبت")
|
| 88 |
+
result_text = gr.Textbox(label="وضعیت", lines=3)
|
| 89 |
|
| 90 |
+
with gr.Tab("🔍 قیود استخراجشده"):
|
| 91 |
+
condition_output = gr.Textbox(label="قیود", lines=10)
|
|
|
|
| 92 |
|
| 93 |
+
def handle_upload(file):
|
| 94 |
+
conditions, msg = parse_excel_file(file)
|
| 95 |
+
return gr.update(value=msg), gr.update(value=conditions)
|
| 96 |
|
| 97 |
+
upload_btn.click(fn=handle_upload, inputs=[file_input], outputs=[result_text, condition_output])
|
| 98 |
|
| 99 |
+
demo.launch()
|
| 100 |
+
3
|