Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ os.makedirs(DATA_FOLDER, exist_ok=True)
|
|
| 13 |
|
| 14 |
def extract_materials_from_excel(file):
|
| 15 |
try:
|
| 16 |
-
df = pd.read_excel(file.name)
|
| 17 |
materials = []
|
| 18 |
for _, row in df.iterrows():
|
| 19 |
material = row.to_dict()
|
|
@@ -21,7 +21,7 @@ def extract_materials_from_excel(file):
|
|
| 21 |
materials.append(material)
|
| 22 |
return materials
|
| 23 |
except Exception as e:
|
| 24 |
-
print("
|
| 25 |
return []
|
| 26 |
|
| 27 |
|
|
@@ -62,7 +62,7 @@ def process_excel(file, description):
|
|
| 62 |
|
| 63 |
materials = extract_materials_from_excel(file)
|
| 64 |
if not materials:
|
| 65 |
-
return "خطا در استخراج مواد از فایل. لطفاً فایل اکسل معتبر وارد کنید.", ""
|
| 66 |
|
| 67 |
conditions = generate_conditions(materials)
|
| 68 |
record = {"توصیف پروژه": description, "مواد": materials}
|
|
@@ -71,20 +71,39 @@ def process_excel(file, description):
|
|
| 71 |
return message, "\n".join(conditions)
|
| 72 |
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
file_input = gr.File(label="فایل اکسل مواد", file_types=[".xlsx"])
|
| 79 |
-
description_input = gr.Textbox(label="توصیف پروژه", placeholder="مثلاً برآورد تعمیرات شبکه روستایی ...")
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
inputs=[file_input, description_input],
|
| 88 |
-
outputs=[output_message, output_conditions])
|
| 89 |
|
| 90 |
demo.launch()
|
|
|
|
| 13 |
|
| 14 |
def extract_materials_from_excel(file):
|
| 15 |
try:
|
| 16 |
+
df = pd.read_excel(file.name, skiprows=2) # ← خواندن از سطر سوم
|
| 17 |
materials = []
|
| 18 |
for _, row in df.iterrows():
|
| 19 |
material = row.to_dict()
|
|
|
|
| 21 |
materials.append(material)
|
| 22 |
return materials
|
| 23 |
except Exception as e:
|
| 24 |
+
print("⚠️ خطا در خواندن فایل اکسل:", e)
|
| 25 |
return []
|
| 26 |
|
| 27 |
|
|
|
|
| 62 |
|
| 63 |
materials = extract_materials_from_excel(file)
|
| 64 |
if not materials:
|
| 65 |
+
return "⚠️ خطا در استخراج مواد از فایل. لطفاً فایل اکسل معتبر وارد کنید.", ""
|
| 66 |
|
| 67 |
conditions = generate_conditions(materials)
|
| 68 |
record = {"توصیف پروژه": description, "مواد": materials}
|
|
|
|
| 71 |
return message, "\n".join(conditions)
|
| 72 |
|
| 73 |
|
| 74 |
+
def load_all_projects():
|
| 75 |
+
if not os.path.exists(DATA_FILE):
|
| 76 |
+
return "هیچ پروژهای ثبت نشده است."
|
| 77 |
+
try:
|
| 78 |
+
with open(DATA_FILE, "r", encoding="utf-8") as f:
|
| 79 |
+
data = json.load(f)
|
| 80 |
+
return json.dumps(data, ensure_ascii=False, indent=2)
|
| 81 |
+
except:
|
| 82 |
+
return "⚠️ خطا در بارگذاری فایل پروژهها."
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
with gr.Blocks(title="نسخه 0.0.2 - مدیریت پروژهها") as demo:
|
| 86 |
+
with gr.Tab("📥 ثبت پروژه جدید"):
|
| 87 |
+
gr.Markdown("### ثبت پروژه بر اساس فایل اکسل")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
file_input = gr.File(label="فایل اکسل مواد", file_types=[".xlsx"])
|
| 91 |
+
description_input = gr.Textbox(label="توصیف پروژه", placeholder="مثلاً برآورد تعمیرات شبکه روستایی ...")
|
| 92 |
+
|
| 93 |
+
output_message = gr.Textbox(label="وضعیت ذخیرهسازی", interactive=False)
|
| 94 |
+
output_conditions = gr.Textbox(label="شرطهای استخراج شده", lines=6)
|
| 95 |
|
| 96 |
+
process_btn = gr.Button("✅ پردازش و ذخیره")
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
process_btn.click(fn=process_excel,
|
| 99 |
+
inputs=[file_input, description_input],
|
| 100 |
+
outputs=[output_message, output_conditions])
|
| 101 |
|
| 102 |
+
with gr.Tab("🗂️ پروژههای ثبتشده"):
|
| 103 |
+
gr.Markdown("### مشاهده پروژههای ثبتشده در سیستم")
|
| 104 |
+
view_btn = gr.Button("📄 نمایش پروژهها")
|
| 105 |
+
output_projects = gr.Textbox(label="همه پروژهها", lines=20, interactive=False)
|
| 106 |
|
| 107 |
+
view_btn.click(fn=load_all_projects, outputs=output_projects)
|
|
|
|
|
|
|
| 108 |
|
| 109 |
demo.launch()
|