Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,15 +9,24 @@ DB_PATH = "material_db.json"
|
|
| 9 |
|
| 10 |
class ElectricPoleEstimator:
|
| 11 |
def __init__(self):
|
|
|
|
|
|
|
| 12 |
self.db = self._load_db()
|
| 13 |
self.rule_engine = RuleEngine()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def _load_db(self) -> List[Dict]:
|
| 16 |
-
"""بارگذاری دیتابیس از فایل
|
| 17 |
-
|
| 18 |
with open(DB_PATH, 'r', encoding='utf-8') as f:
|
| 19 |
return json.load(f)
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
def _save_db(self):
|
| 23 |
"""ذخیره دیتابیس در فایل"""
|
|
@@ -114,10 +123,6 @@ class RuleEngine:
|
|
| 114 |
self._passing_config_rule
|
| 115 |
]
|
| 116 |
|
| 117 |
-
def add_rule(self, rule_func):
|
| 118 |
-
"""اضافه کردن قانون جدید"""
|
| 119 |
-
self.rules.append(rule_func)
|
| 120 |
-
|
| 121 |
def apply_rules(self, context: Dict) -> Dict:
|
| 122 |
"""اعمال تمام قوانین"""
|
| 123 |
overrides = {}
|
|
@@ -146,7 +151,7 @@ with gr.Blocks(title="برآورد هوشمند مصالح تاسیسات برق
|
|
| 146 |
|
| 147 |
with gr.Tab("بارگذاری دادهها"):
|
| 148 |
gr.Markdown("### بارگذاری فایل اکسل جدید")
|
| 149 |
-
file_input = gr.File(label="فایل اکسل برآورد")
|
| 150 |
upload_btn = gr.Button("بارگذاری و ذخیره در دیتابیس")
|
| 151 |
upload_output = gr.Textbox(label="نتایج بارگذاری")
|
| 152 |
upload_btn.click(estimator.add_materials, inputs=file_input, outputs=upload_output)
|
|
@@ -169,7 +174,8 @@ with gr.Blocks(title="برآورد هوشمند مصالح تاسیسات برق
|
|
| 169 |
|
| 170 |
results_table = gr.Dataframe(
|
| 171 |
headers=["کد", "عنوان", "مقدار", "واحد", "شرایط"],
|
| 172 |
-
datatype=["str", "str", "number", "str", "str"]
|
|
|
|
| 173 |
)
|
| 174 |
|
| 175 |
estimate_btn.click(
|
|
@@ -178,14 +184,18 @@ with gr.Blocks(title="برآورد هوشمند مصالح تاسیسات برق
|
|
| 178 |
outputs=results_table
|
| 179 |
)
|
| 180 |
|
| 181 |
-
with gr.Tab("مدی
|
| 182 |
-
gr.Markdown("###
|
| 183 |
-
gr.
|
| 184 |
-
"
|
| 185 |
-
"
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
if __name__ == "__main__":
|
| 191 |
demo.launch()
|
|
|
|
| 9 |
|
| 10 |
class ElectricPoleEstimator:
|
| 11 |
def __init__(self):
|
| 12 |
+
# ایجاد فایل دیتابیس اگر وجود ندارد
|
| 13 |
+
self._initialize_db()
|
| 14 |
self.db = self._load_db()
|
| 15 |
self.rule_engine = RuleEngine()
|
| 16 |
|
| 17 |
+
def _initialize_db(self):
|
| 18 |
+
"""ایجاد فایل دیتابیس اولیه"""
|
| 19 |
+
if not os.path.exists(DB_PATH):
|
| 20 |
+
with open(DB_PATH, 'w', encoding='utf-8') as f:
|
| 21 |
+
json.dump([], f, ensure_ascii=False, indent=2)
|
| 22 |
+
|
| 23 |
def _load_db(self) -> List[Dict]:
|
| 24 |
+
"""بارگذاری دیتابیس از فایل"""
|
| 25 |
+
try:
|
| 26 |
with open(DB_PATH, 'r', encoding='utf-8') as f:
|
| 27 |
return json.load(f)
|
| 28 |
+
except (json.JSONDecodeError, FileNotFoundError):
|
| 29 |
+
return []
|
| 30 |
|
| 31 |
def _save_db(self):
|
| 32 |
"""ذخیره دیتابیس در فایل"""
|
|
|
|
| 123 |
self._passing_config_rule
|
| 124 |
]
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
def apply_rules(self, context: Dict) -> Dict:
|
| 127 |
"""اعمال تمام قوانین"""
|
| 128 |
overrides = {}
|
|
|
|
| 151 |
|
| 152 |
with gr.Tab("بارگذاری دادهها"):
|
| 153 |
gr.Markdown("### بارگذاری فایل اکسل جدید")
|
| 154 |
+
file_input = gr.File(label="فایل اکسل برآورد", type="filepath")
|
| 155 |
upload_btn = gr.Button("بارگذاری و ذخیره در دیتابیس")
|
| 156 |
upload_output = gr.Textbox(label="نتایج بارگذاری")
|
| 157 |
upload_btn.click(estimator.add_materials, inputs=file_input, outputs=upload_output)
|
|
|
|
| 174 |
|
| 175 |
results_table = gr.Dataframe(
|
| 176 |
headers=["کد", "عنوان", "مقدار", "واحد", "شرایط"],
|
| 177 |
+
datatype=["str", "str", "number", "str", "str"],
|
| 178 |
+
wrap=True
|
| 179 |
)
|
| 180 |
|
| 181 |
estimate_btn.click(
|
|
|
|
| 184 |
outputs=results_table
|
| 185 |
)
|
| 186 |
|
| 187 |
+
with gr.Tab("مشاهده دیتابیس"):
|
| 188 |
+
gr.Markdown("### محتوای فعلی دیتابیس")
|
| 189 |
+
db_viewer = gr.Dataframe(
|
| 190 |
+
headers=["کد", "عنوان", "واحد", "ارتفاع", "توان", "هادی"],
|
| 191 |
+
datatype=["str", "str", "str", "number", "number", "str"],
|
| 192 |
+
interactive=False
|
| 193 |
+
)
|
| 194 |
+
refresh_btn = gr.Button("بروزرسانی نمایش")
|
| 195 |
+
refresh_btn.click(
|
| 196 |
+
lambda: pd.DataFrame(estimator.db)[['code', 'title', 'unit', 'pole_height', 'pole_capacity', 'conductor_type']],
|
| 197 |
+
outputs=db_viewer
|
| 198 |
+
)
|
| 199 |
|
| 200 |
if __name__ == "__main__":
|
| 201 |
demo.launch()
|