reyhane1222 commited on
Commit
63782d6
·
verified ·
1 Parent(s): ca1bfff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -20
app.py CHANGED
@@ -8,9 +8,14 @@ import torch, math, pandas as pd
8
  # ==============================
9
  # 📦 بارگذاری مدل (یکبار کش میشه)
10
  # ==============================
11
- model_id = "prithivMLmods/Minc-Materials-23"
12
- processor = AutoImageProcessor.from_pretrained(model_id)
13
- model = AutoModelForImageClassification.from_pretrained(model_id)
 
 
 
 
 
14
 
15
  # ==============================
16
  # 📊 پارامترهای مصالح
@@ -34,18 +39,6 @@ material_params = {
34
  "sky":{"alpha":1.0,"eps":1.0,"I":0},
35
  }
36
 
37
- # دسته‌ها و جایگزین‌ها (خلاصه)
38
- replacement_text = {
39
- "facade": {"brick":"آجر روشن یا نمای سرامیکی روشن","stone":"سنگ روشن یا نمای گچی بازتابی"},
40
- "glazing": {"glass":"شیشه دوجداره Low-E","mirror":"شیشه مات یا بازتاب متعادل"},
41
- "metallic": {"metal":"آلومینیوم رنگ روشن"},
42
- "coverings": {"plastic":"چوب روشن یا سنگ سبک"},
43
- "wood_elements": {"wood":"چوب روشن با پوشش بازتابی"},
44
- "vegetation": {"foliage":None},
45
- "water_bodies": {"water":None},
46
- "background": {"sky":None}
47
- }
48
-
49
  material_categories = {
50
  "facade":["brick","stone","polishedstone","concrete","tile","ceramic","painted"],
51
  "glazing":["glass","mirror"],
@@ -57,6 +50,23 @@ material_categories = {
57
  "background":["sky"]
58
  }
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  # ==============================
61
  # محاسبات
62
  # ==============================
@@ -94,6 +104,7 @@ def get_patches(image,size=224,stride=200):
94
  def analyze(img,T_air,RH=40,u=2,S=700):
95
  img=img.convert("RGB")
96
  patches=get_patches(img)
 
97
 
98
  all_predictions=[]
99
  for patch in patches:
@@ -108,13 +119,16 @@ def analyze(img,T_air,RH=40,u=2,S=700):
108
  total_patches=len(patches)
109
  materials_found={m for m,c in counter.items() if c>=3 and m in material_params}
110
 
 
 
 
111
  results=[]
112
  for m in materials_found:
113
  share=counter[m]/total_patches
114
  dT=calc_deltaT(m,T_air,RH,u,S)
115
- results.append(f"{m} | سهم={share*100:.1f}% | ΔT={dT:+.3f}°C")
116
-
117
- if not results: return "⛔ هیچ مصالح معتبری شناسایی نشد."
118
 
119
  scene_deltaT=sum((counter[m]/total_patches)*calc_deltaT(m,T_air,RH,u,S) for m in materials_found)
120
  summary=f"📌 ΔT میانگین وزنی: {scene_deltaT:+.2f} °C\n📌 دمای مؤثر سطح: {T_air+scene_deltaT:.2f} °C"
@@ -132,8 +146,11 @@ demo = gr.Interface(
132
  gr.Number(label="💨 سرعت باد (m/s)",value=2),
133
  gr.Number(label="☀️ تابش خورشیدی (W/m²)",value=700)
134
  ],
135
- outputs=gr.Textbox(label="نتایج تحلیل")
 
 
 
136
  )
137
 
138
  if __name__=="__main__":
139
- demo.launch()
 
8
  # ==============================
9
  # 📦 بارگذاری مدل (یکبار کش میشه)
10
  # ==============================
11
+ @torch.no_grad()
12
+ def load_model():
13
+ model_id = "prithivMLmods/Minc-Materials-23"
14
+ processor = AutoImageProcessor.from_pretrained(model_id)
15
+ model = AutoModelForImageClassification.from_pretrained(model_id)
16
+ return processor, model
17
+
18
+ processor, model = load_model()
19
 
20
  # ==============================
21
  # 📊 پارامترهای مصالح
 
39
  "sky":{"alpha":1.0,"eps":1.0,"I":0},
40
  }
41
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  material_categories = {
43
  "facade":["brick","stone","polishedstone","concrete","tile","ceramic","painted"],
44
  "glazing":["glass","mirror"],
 
50
  "background":["sky"]
51
  }
52
 
53
+ replacement_text = {
54
+ "facade": {
55
+ "brick":"آجر روشن یا نمای سرامیکی روشن",
56
+ "stone":"سنگ روشن یا نمای گچی بازتابی",
57
+ "tile":"کاشی/سرامیک روشن یا متخلخل",
58
+ "ceramic":"سرامیک روشن با پوشش بازتابی",
59
+ "painted":"رنگ بازتابی (cool paint)"
60
+ },
61
+ "glazing": {"glass":"شیشه دوجداره Low-E","mirror":"شیشه مات یا بازتاب متعادل"},
62
+ "metallic":{"metal":"آلومینیوم رنگ روشن"},
63
+ "coverings":{"plastic":"چوب روشن یا سنگ سبک","paper":"جایگزینی طراحی","fabric":"پارچه بازتابی"},
64
+ "wood_elements":{"wood":"چوب روشن با پوشش بازتابی"},
65
+ "vegetation":{"foliage":None},
66
+ "water_bodies":{"water":None},
67
+ "background":{"sky":None}
68
+ }
69
+
70
  # ==============================
71
  # محاسبات
72
  # ==============================
 
104
  def analyze(img,T_air,RH=40,u=2,S=700):
105
  img=img.convert("RGB")
106
  patches=get_patches(img)
107
+ if len(patches)==0: return "⛔ تصویر نامعتبر است یا کوچک است."
108
 
109
  all_predictions=[]
110
  for patch in patches:
 
119
  total_patches=len(patches)
120
  materials_found={m for m,c in counter.items() if c>=3 and m in material_params}
121
 
122
+ if not materials_found:
123
+ return "⛔ هیچ مصالح معتبری شناسایی نشد."
124
+
125
  results=[]
126
  for m in materials_found:
127
  share=counter[m]/total_patches
128
  dT=calc_deltaT(m,T_air,RH,u,S)
129
+ category = next((cat for cat, members in material_categories.items() if m in members), None)
130
+ suggestion = replacement_text.get(category, {}).get(m, "نیازی به جایگزینی ندارد")
131
+ results.append(f"{m} | سهم={share*100:.1f}% | ΔT={dT:+.3f}°C | پیشنهاد: {suggestion}")
132
 
133
  scene_deltaT=sum((counter[m]/total_patches)*calc_deltaT(m,T_air,RH,u,S) for m in materials_found)
134
  summary=f"📌 ΔT میانگین وزنی: {scene_deltaT:+.2f} °C\n📌 دمای مؤثر سطح: {T_air+scene_deltaT:.2f} °C"
 
146
  gr.Number(label="💨 سرعت باد (m/s)",value=2),
147
  gr.Number(label="☀️ تابش خورشیدی (W/m²)",value=700)
148
  ],
149
+ outputs=gr.Textbox(label="نتایج تحلیل"),
150
+ title="🏗️ تحلیل مصالح و توصیه اقلیمی",
151
+ description="این اپلیکیشن مصالح را از تصویر شناسایی کرده و ΔT سطح و پیشنهادهای جایگزینی را نمایش می‌دهد.",
152
+ allow_flagging="never"
153
  )
154
 
155
  if __name__=="__main__":
156
+ demo.launch(share=True) # share=True لینک عمومی موقت می‌دهد