Update app.py
Browse files
app.py
CHANGED
|
@@ -182,27 +182,23 @@ class AnonymizationEvaluator:
|
|
| 182 |
|
| 183 |
return report
|
| 184 |
|
| 185 |
-
def create_downloadable_csv(self) ->
|
| 186 |
-
"""ایجاد
|
| 187 |
if self.results_df is None or self.results_df.empty:
|
| 188 |
return None
|
| 189 |
|
| 190 |
try:
|
| 191 |
-
# تولید
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
#
|
| 195 |
-
|
| 196 |
-
prefix=f'evaluation_results_{timestamp}_',
|
| 197 |
-
encoding='utf-8-sig', newline='') as temp_file:
|
| 198 |
-
# نوشتن دادهها در فایل
|
| 199 |
-
self.results_df.to_csv(temp_file, index=False)
|
| 200 |
-
temp_filename = temp_file.name
|
| 201 |
-
|
| 202 |
-
return temp_filename
|
| 203 |
|
| 204 |
except Exception as e:
|
| 205 |
-
print(f"خطا در ایجاد
|
| 206 |
return None
|
| 207 |
|
| 208 |
def create_evaluation_interface():
|
|
@@ -354,15 +350,26 @@ def create_evaluation_interface():
|
|
| 354 |
gr.File(visible=False)
|
| 355 |
)
|
| 356 |
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
return (
|
| 360 |
f"✅ فایل نتایج آماده شد: {filename} ({len(evaluator.results_df)} سطر)",
|
| 361 |
-
gr.File(value=
|
| 362 |
)
|
| 363 |
else:
|
| 364 |
return (
|
| 365 |
-
"❌ خطا در ایجاد
|
| 366 |
gr.File(visible=False)
|
| 367 |
)
|
| 368 |
except Exception as e:
|
|
@@ -436,19 +443,34 @@ def create_evaluation_interface():
|
|
| 436 |
|
| 437 |
def create_test_file():
|
| 438 |
"""ایجاد فایل تست برای بررسی دانلود"""
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
|
| 453 |
test_download_btn.click(
|
| 454 |
fn=create_test_file,
|
|
|
|
| 182 |
|
| 183 |
return report
|
| 184 |
|
| 185 |
+
def create_downloadable_csv(self) -> bytes:
|
| 186 |
+
"""ایجاد محتوای CSV برای دانلود مستقیم"""
|
| 187 |
if self.results_df is None or self.results_df.empty:
|
| 188 |
return None
|
| 189 |
|
| 190 |
try:
|
| 191 |
+
# تولید محتوای CSV در حافظه
|
| 192 |
+
csv_buffer = io.StringIO()
|
| 193 |
+
self.results_df.to_csv(csv_buffer, index=False, encoding='utf-8')
|
| 194 |
+
csv_content = csv_buffer.getvalue()
|
| 195 |
+
csv_buffer.close()
|
| 196 |
|
| 197 |
+
# تبدیل به bytes برای دانلود
|
| 198 |
+
return csv_content.encode('utf-8-sig')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
+
print(f"خطا در ایجاد محتوای CSV: {str(e)}")
|
| 202 |
return None
|
| 203 |
|
| 204 |
def create_evaluation_interface():
|
|
|
|
| 350 |
gr.File(visible=False)
|
| 351 |
)
|
| 352 |
|
| 353 |
+
# ایجاد محتوای CSV
|
| 354 |
+
csv_content = evaluator.create_downloadable_csv()
|
| 355 |
+
if csv_content:
|
| 356 |
+
# تولید نام فایل
|
| 357 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 358 |
+
filename = f"evaluation_results_{timestamp}.csv"
|
| 359 |
+
|
| 360 |
+
# ذخیره در فایل موقت برای دانلود
|
| 361 |
+
with tempfile.NamedTemporaryFile(mode='wb', delete=False,
|
| 362 |
+
suffix='.csv', prefix='eval_') as temp_file:
|
| 363 |
+
temp_file.write(csv_content)
|
| 364 |
+
temp_filename = temp_file.name
|
| 365 |
+
|
| 366 |
return (
|
| 367 |
f"✅ فایل نتایج آماده شد: {filename} ({len(evaluator.results_df)} سطر)",
|
| 368 |
+
gr.File(value=temp_filename, visible=True)
|
| 369 |
)
|
| 370 |
else:
|
| 371 |
return (
|
| 372 |
+
"❌ خطا در ایجاد محتوای CSV",
|
| 373 |
gr.File(visible=False)
|
| 374 |
)
|
| 375 |
except Exception as e:
|
|
|
|
| 443 |
|
| 444 |
def create_test_file():
|
| 445 |
"""ایجاد فایل تست برای بررسی دانلود"""
|
| 446 |
+
try:
|
| 447 |
+
test_data = {
|
| 448 |
+
'original_text': ['متن تست 1', 'متن تست 2'],
|
| 449 |
+
'Reference_text': ['company-01 amount-01', 'person-01 amount-02'],
|
| 450 |
+
'anonymized_text': ['company-01 amount-01', 'person-01 amount-02'],
|
| 451 |
+
'Precision': [1.0, 1.0],
|
| 452 |
+
'Recall': [1.0, 1.0],
|
| 453 |
+
'F1_Score': [1.0, 1.0]
|
| 454 |
+
}
|
| 455 |
+
test_df = pd.DataFrame(test_data)
|
| 456 |
+
|
| 457 |
+
# ایجاد محتوای CSV در حافظه
|
| 458 |
+
csv_buffer = io.StringIO()
|
| 459 |
+
test_df.to_csv(csv_buffer, index=False)
|
| 460 |
+
csv_content = csv_buffer.getvalue()
|
| 461 |
+
csv_buffer.close()
|
| 462 |
+
|
| 463 |
+
# تبدیل به bytes و ذخیره در فایل موقت
|
| 464 |
+
csv_bytes = csv_content.encode('utf-8-sig')
|
| 465 |
+
with tempfile.NamedTemporaryFile(mode='wb', delete=False,
|
| 466 |
+
suffix='.csv', prefix='test_') as temp_file:
|
| 467 |
+
temp_file.write(csv_bytes)
|
| 468 |
+
temp_filename = temp_file.name
|
| 469 |
+
|
| 470 |
+
return gr.File(value=temp_filename, visible=True)
|
| 471 |
+
except Exception as e:
|
| 472 |
+
print(f"خطا در ایجاد فایل تست: {str(e)}")
|
| 473 |
+
return gr.File(visible=False)
|
| 474 |
|
| 475 |
test_download_btn.click(
|
| 476 |
fn=create_test_file,
|