Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -141,6 +141,142 @@ with gr.Blocks() as demo:
|
|
141 |
ihtiyac_output = gr.DataFrame()
|
142 |
|
143 |
filter_ihtiyac_button.click(fn=filter_ihtiyac_data, inputs=[ilce_multiselect_ihtiyac_filter, brans_multiselect_ihtiyac_filter], outputs=[ihtiyac_output, ihtiyac_total_count])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
# Run the app
|
146 |
demo.launch()
|
|
|
141 |
ihtiyac_output = gr.DataFrame()
|
142 |
|
143 |
filter_ihtiyac_button.click(fn=filter_ihtiyac_data, inputs=[ilce_multiselect_ihtiyac_filter, brans_multiselect_ihtiyac_filter], outputs=[ihtiyac_output, ihtiyac_total_count])
|
144 |
+
|
145 |
+
# 4. Tab: Branşları Karşılaştırma
|
146 |
+
# 4. Tab: Branşları Karşılaştırma
|
147 |
+
with gr.Tab("Branşları Karşılaştırma"):
|
148 |
+
# İhtiyaç ve norm fazlası veri çerçevelerinde bulunan tüm benzersiz branşları alıyoruz
|
149 |
+
all_branches = sorted(set(df_ihtiyac['branş'].dropna().unique()).union(df_norm['Branşı'].dropna().unique()))
|
150 |
+
brans_multiselect_compare = gr.Dropdown(choices=all_branches, label="Karşılaştırılacak Branşları Seçin", multiselect=True)
|
151 |
+
|
152 |
+
compare_button = gr.Button("Karşılaştır")
|
153 |
+
compare_output = gr.DataFrame()
|
154 |
+
compare_chart = gr.Image()
|
155 |
+
|
156 |
+
def compare_branches(brans_values):
|
157 |
+
# Seçim yapılmadıysa tüm branşları al
|
158 |
+
selected_branches = brans_values if brans_values else all_branches
|
159 |
+
|
160 |
+
# Sonuçları toplamak için liste
|
161 |
+
results = []
|
162 |
+
for brans in selected_branches:
|
163 |
+
# İlgili branş için toplam ihtiyaç ve norm fazlası değerlerini al
|
164 |
+
total_needs = df_ihtiyac[df_ihtiyac['branş'] == brans]['ihtiyac'].sum() if brans in df_ihtiyac['branş'].values else 0
|
165 |
+
mazaretli_count = df_norm[(df_norm['Branşı'] == brans) & (df_norm['Açıklamalar'].notna())].shape[0] if brans in df_norm['Branşı'].values else 0
|
166 |
+
mazaretsiz_count = df_norm[(df_norm['Branşı'] == brans) & (df_norm['Açıklamalar'].isna())].shape[0] if brans in df_norm['Branşı'].values else 0
|
167 |
+
|
168 |
+
# Oran hesaplaması, sıfıra bölme hatasından kaçınmak için kontrol
|
169 |
+
if total_needs > 0:
|
170 |
+
ratio = (mazaretli_count + mazaretsiz_count) / total_needs
|
171 |
+
else:
|
172 |
+
ratio = float('nan') # Toplam ihtiyaç 0 ise oranı NaN olarak ayarla
|
173 |
+
|
174 |
+
results.append({
|
175 |
+
'Branş': brans,
|
176 |
+
'Toplam İhtiyaç': total_needs,
|
177 |
+
'Norm Fazlası (Mazaretli)': mazaretli_count,
|
178 |
+
'Norm Fazlası (Mazaretsiz)': mazaretsiz_count,
|
179 |
+
'Oran': ratio
|
180 |
+
})
|
181 |
+
|
182 |
+
# Sonuçları DataFrame olarak döndür
|
183 |
+
compare_df = pd.DataFrame(results)
|
184 |
+
|
185 |
+
# Branş sayısı 10'dan fazlaysa grafik çizdirme
|
186 |
+
if len(selected_branches) <= 10:
|
187 |
+
# Çubuk grafik oluşturma
|
188 |
+
fig, ax1 = plt.subplots(figsize=(10, 6))
|
189 |
+
|
190 |
+
# İhtiyaç ve norm fazlası verilerini çubuk grafik olarak çiz
|
191 |
+
compare_df.set_index('Branş')[['Toplam İhtiyaç', 'Norm Fazlası (Mazaretli)', 'Norm Fazlası (Mazaretsiz)']].plot(kind='bar', ax=ax1)
|
192 |
+
ax1.set_ylabel("Sayı")
|
193 |
+
ax1.set_xlabel("Branş")
|
194 |
+
ax1.set_title("Branş Bazında İhtiyaç, Norm Fazlası ve Oran Karşılaştırması")
|
195 |
+
plt.xticks(rotation=45, ha="right")
|
196 |
+
|
197 |
+
# Oran verilerini ikincil bir eksen olarak çiz
|
198 |
+
ax2 = ax1.twinx()
|
199 |
+
ax2.plot(compare_df['Branş'], compare_df['Oran'], color='red', marker='o', linestyle='-', linewidth=2)
|
200 |
+
ax2.set_ylabel("Oran", color='red')
|
201 |
+
ax2.tick_params(axis='y', labelcolor='red')
|
202 |
+
|
203 |
+
plt.tight_layout()
|
204 |
+
chart_path = 'branch_comparison_chart.png'
|
205 |
+
plt.savefig(chart_path)
|
206 |
+
plt.close()
|
207 |
+
else:
|
208 |
+
chart_path = None # Grafik gösterilmeyecek
|
209 |
+
|
210 |
+
return compare_df, chart_path
|
211 |
+
|
212 |
+
compare_button.click(fn=compare_branches, inputs=[brans_multiselect_compare], outputs=[compare_output, compare_chart])
|
213 |
+
# 5. Tab: İlçeleri Karşılaştırma
|
214 |
+
with gr.Tab("İlçeleri Karşılaştırma"):
|
215 |
+
# İhtiyaç ve norm fazlası veri çerçevelerinde bulunan tüm benzersiz ilçeleri alıyoruz
|
216 |
+
all_districts = sorted(set(df_ihtiyac['ilçe'].dropna().unique()).union(df_norm['İlçe Adı'].dropna().unique()))
|
217 |
+
ilce_multiselect_compare = gr.Dropdown(choices=all_districts, label="Karşılaştırılacak İlçeleri Seçin", multiselect=True)
|
218 |
+
|
219 |
+
compare_button_ilce = gr.Button("Karşılaştır")
|
220 |
+
compare_output_ilce = gr.DataFrame()
|
221 |
+
compare_chart_ilce = gr.Image()
|
222 |
+
|
223 |
+
def compare_districts(ilce_values):
|
224 |
+
# Seçim yapılmadıysa tüm ilçeleri al
|
225 |
+
selected_districts = ilce_values if ilce_values else all_districts
|
226 |
+
|
227 |
+
# Sonuçları toplamak için liste
|
228 |
+
results = []
|
229 |
+
for ilce in selected_districts:
|
230 |
+
# İlgili ilçe için toplam ihtiyaç ve norm fazlası değerlerini al
|
231 |
+
total_needs = df_ihtiyac[df_ihtiyac['ilçe'] == ilce]['ihtiyac'].sum() if ilce in df_ihtiyac['ilçe'].values else 0
|
232 |
+
mazaretli_count = df_norm[(df_norm['İlçe Adı'] == ilce) & (df_norm['Açıklamalar'].notna())].shape[0] if ilce in df_norm['İlçe Adı'].values else 0
|
233 |
+
mazaretsiz_count = df_norm[(df_norm['İlçe Adı'] == ilce) & (df_norm['Açıklamalar'].isna())].shape[0] if ilce in df_norm['İlçe Adı'].values else 0
|
234 |
+
|
235 |
+
# Oran hesaplaması, sıfıra bölme hatasından kaçınmak için kontrol
|
236 |
+
if total_needs > 0:
|
237 |
+
ratio = (mazaretli_count + mazaretsiz_count) / total_needs
|
238 |
+
else:
|
239 |
+
ratio = float('nan') # Toplam ihtiyaç 0 ise oranı NaN olarak ayarla
|
240 |
+
|
241 |
+
results.append({
|
242 |
+
'İlçe': ilce,
|
243 |
+
'Toplam İhtiyaç': total_needs,
|
244 |
+
'Norm Fazlası (Mazaretli)': mazaretli_count,
|
245 |
+
'Norm Fazlası (Mazaretsiz)': mazaretsiz_count,
|
246 |
+
'Oran': ratio
|
247 |
+
})
|
248 |
+
|
249 |
+
# Sonuçları DataFrame olarak döndür
|
250 |
+
compare_df_ilce = pd.DataFrame(results)
|
251 |
+
|
252 |
+
# İlçe sayısı 10'dan fazlaysa grafik çizdirme
|
253 |
+
if len(selected_districts) <= 10:
|
254 |
+
# Çubuk grafik oluşturma
|
255 |
+
fig, ax1 = plt.subplots(figsize=(10, 6))
|
256 |
+
|
257 |
+
# İhtiyaç ve norm fazlası verilerini çubuk grafik olarak çiz
|
258 |
+
compare_df_ilce.set_index('İlçe')[['Toplam İhtiyaç', 'Norm Fazlası (Mazaretli)', 'Norm Fazlası (Mazaretsiz)']].plot(kind='bar', ax=ax1)
|
259 |
+
ax1.set_ylabel("Sayı")
|
260 |
+
ax1.set_xlabel("İlçe")
|
261 |
+
ax1.set_title("İlçe Bazında İhtiyaç, Norm Fazlası ve Oran Karşılaştırması")
|
262 |
+
plt.xticks(rotation=45, ha="right")
|
263 |
+
|
264 |
+
# Oran verilerini ikincil bir eksen olarak çiz
|
265 |
+
ax2 = ax1.twinx()
|
266 |
+
ax2.plot(compare_df_ilce['İlçe'], compare_df_ilce['Oran'], color='red', marker='o', linestyle='-', linewidth=2)
|
267 |
+
ax2.set_ylabel("Oran", color='red')
|
268 |
+
ax2.tick_params(axis='y', labelcolor='red')
|
269 |
+
|
270 |
+
plt.tight_layout()
|
271 |
+
chart_path_ilce = 'district_comparison_chart.png'
|
272 |
+
plt.savefig(chart_path_ilce)
|
273 |
+
plt.close()
|
274 |
+
else:
|
275 |
+
chart_path_ilce = None # Grafik gösterilmeyecek
|
276 |
+
|
277 |
+
return compare_df_ilce, chart_path_ilce
|
278 |
+
|
279 |
+
compare_button_ilce.click(fn=compare_districts, inputs=[ilce_multiselect_compare], outputs=[compare_output_ilce, compare_chart_ilce])
|
280 |
|
281 |
# Run the app
|
282 |
demo.launch()
|