Spaces:
Sleeping
Sleeping
Rename app002.py to app010.py
Browse files
app002.py
DELETED
|
@@ -1,107 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import json
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
# مسیر فایل JSON
|
| 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, project_description):
|
| 16 |
-
if not project_description or not file:
|
| 17 |
-
return "❌ لطفاً توضیح پروژه و فایل اکسل را وارد کنید.", ""
|
| 18 |
-
|
| 19 |
-
try:
|
| 20 |
-
df = pd.read_excel(file.name, header=2)
|
| 21 |
-
except Exception as e:
|
| 22 |
-
return f"❌ خطا در خواندن فایل اکسل: {str(e)}", ""
|
| 23 |
-
|
| 24 |
-
column_mapping = {
|
| 25 |
-
"کد": ["کد فهرست بها", "کد"],
|
| 26 |
-
"عنوان": ["عنوان", "شرح کالا", "نام کالا"],
|
| 27 |
-
"تعداد": ["تعداد", "تعداد مورد نیاز"],
|
| 28 |
-
"واحد": ["واحد", "واحد شمارش", "نوع واحد"]
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
resolved_columns = {}
|
| 32 |
-
for key, options in column_mapping.items():
|
| 33 |
-
found = next((col for col in options if col in df.columns), None)
|
| 34 |
-
if not found:
|
| 35 |
-
return f"❌ ستون «{key}» در فایل پیدا نشد.", ""
|
| 36 |
-
|
| 37 |
-
resolved_columns[key] = found
|
| 38 |
-
|
| 39 |
-
# بارگذاری دیتابیس
|
| 40 |
-
try:
|
| 41 |
-
with open(JSON_DB_PATH, "r", encoding="utf-8") as f:
|
| 42 |
-
db = json.load(f)
|
| 43 |
-
except Exception as e:
|
| 44 |
-
return f"❌ خطا در بارگذاری پایگاهداده: {str(e)}", ""
|
| 45 |
-
|
| 46 |
-
# بررسی پروژه تکراری
|
| 47 |
-
existing_projects = [m["project"] for m in db]
|
| 48 |
-
if project_description.strip() in existing_projects:
|
| 49 |
-
return "⚠️ پروژهای با این عنوان قبلاً ثبت شده است.", ""
|
| 50 |
-
|
| 51 |
-
materials = []
|
| 52 |
-
conditions = []
|
| 53 |
-
|
| 54 |
-
for _, row in df.iterrows():
|
| 55 |
-
try:
|
| 56 |
-
quantity = float(row[resolved_columns["تعداد"]])
|
| 57 |
-
if pd.isna(quantity) or quantity <= 0:
|
| 58 |
-
continue
|
| 59 |
-
except:
|
| 60 |
-
continue
|
| 61 |
-
|
| 62 |
-
code = str(row[resolved_columns["کد"]]).strip()
|
| 63 |
-
desc = str(row[resolved_columns["عنوان"]]).strip()
|
| 64 |
-
unit = str(row[resolved_columns["واحد"]]).strip()
|
| 65 |
-
|
| 66 |
-
material = {
|
| 67 |
-
"code": code,
|
| 68 |
-
"description": desc,
|
| 69 |
-
"quantity": quantity,
|
| 70 |
-
"unit": unit,
|
| 71 |
-
"project": project_description.strip()
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
materials.append(material)
|
| 75 |
-
conditions.append(f"{desc} به مقدار {quantity} {unit}")
|
| 76 |
-
|
| 77 |
-
if not materials:
|
| 78 |
-
return "⚠️ هیچ مادهای برای ثبت پیدا نشد.", ""
|
| 79 |
-
|
| 80 |
-
try:
|
| 81 |
-
db.extend(materials)
|
| 82 |
-
with open(JSON_DB_PATH, "w", encoding="utf-8") as f:
|
| 83 |
-
json.dump(db, f, indent=2, ensure_ascii=False)
|
| 84 |
-
except Exception as e:
|
| 85 |
-
return f"❌ خطا در ذخیره پایگاهداده: {str(e)}", ""
|
| 86 |
-
|
| 87 |
-
return "✅ مواد با موفقیت ثبت شدند.", "\n".join(conditions)
|
| 88 |
-
|
| 89 |
-
# رابط کاربری Gradio
|
| 90 |
-
with gr.Blocks(title="ثبت پروژه") as demo:
|
| 91 |
-
gr.Markdown("## 📝 ثبت پروژه برآورد و استخراج قیود")
|
| 92 |
-
|
| 93 |
-
with gr.Row():
|
| 94 |
-
with gr.Column(scale=2):
|
| 95 |
-
project_description = gr.Textbox(label="توصیف پروژه", placeholder="مثلاً توسعه شبکه فشار متوسط")
|
| 96 |
-
|
| 97 |
-
file_input = gr.File(label="آپلود فایل اکسل", file_types=[".xls", ".xlsx"])
|
| 98 |
-
submit_btn = gr.Button("ثبت پروژه و استخراج قیود")
|
| 99 |
-
|
| 100 |
-
result_box = gr.Textbox(label="وضعیت ثبت", lines=2)
|
| 101 |
-
|
| 102 |
-
with gr.Column(scale=1):
|
| 103 |
-
condition_box = gr.Textbox(label="قیود استخراجشده", lines=15, interactive=False)
|
| 104 |
-
|
| 105 |
-
submit_btn.click(fn=parse_excel_file, inputs=[file_input, project_description], outputs=[result_box, condition_box])
|
| 106 |
-
|
| 107 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app010.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, 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()
|