Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -477,22 +477,13 @@ class TranslationApp:
|
|
| 477 |
correct_translation: Optional[str] = None
|
| 478 |
):
|
| 479 |
"""
|
| 480 |
-
Log user feedback to a CSV file
|
| 481 |
-
|
| 482 |
-
Args:
|
| 483 |
-
source_lang: Source language enum
|
| 484 |
-
target_lang: Target language enum
|
| 485 |
-
user_input: Original input text
|
| 486 |
-
model_output: Model-generated translation
|
| 487 |
-
notation: User rating (optional)
|
| 488 |
-
correct_translation: Corrected version (optional)
|
| 489 |
"""
|
| 490 |
# Define filename
|
| 491 |
src = source_lang.value.lower()
|
| 492 |
tgt = target_lang.value.lower()
|
| 493 |
filename = f"{src}_{tgt}.csv"
|
| 494 |
-
|
| 495 |
-
# Define headers and row
|
| 496 |
headers = [
|
| 497 |
"timestamp",
|
| 498 |
"source_language",
|
|
@@ -511,17 +502,35 @@ class TranslationApp:
|
|
| 511 |
"notation": notation,
|
| 512 |
"correct_translation": correct_translation.strip() if correct_translation else ""
|
| 513 |
}
|
| 514 |
-
|
| 515 |
-
# Check if file exists to determine whether to write headers
|
| 516 |
file_exists = Path(filename).exists()
|
| 517 |
-
|
| 518 |
try:
|
|
|
|
| 519 |
with open(filename, mode="a", encoding="utf-8", newline="") as f:
|
| 520 |
writer = csv.DictWriter(f, fieldnames=headers)
|
| 521 |
if not file_exists:
|
| 522 |
-
writer.writeheader()
|
| 523 |
writer.writerow(row)
|
| 524 |
-
logger.info(f"Feedback saved to {filename}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
except Exception as e:
|
| 526 |
logger.error(f"Failed to save feedback to {filename}: {e}")
|
| 527 |
|
|
@@ -694,7 +703,7 @@ class TranslationApp:
|
|
| 694 |
notation_val: float,
|
| 695 |
correct_trans: str
|
| 696 |
):
|
| 697 |
-
"""Save feedback to CSV."""
|
| 698 |
try:
|
| 699 |
source_lang = Language(source_lang_str)
|
| 700 |
target_lang = Language(target_lang_str)
|
|
@@ -707,50 +716,59 @@ class TranslationApp:
|
|
| 707 |
notation=notation_val,
|
| 708 |
correct_translation=correct_trans or None
|
| 709 |
)
|
| 710 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 711 |
except Exception as e:
|
| 712 |
logger.error(f"Feedback submission failed: {e}")
|
| 713 |
-
return
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
inputs=input_mode,
|
| 719 |
-
outputs=[input_text, audio_input, file_input, extracted_text, output_text]
|
| 720 |
-
)
|
| 721 |
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
|
| 755 |
# ================================
|
| 756 |
# Application Entry Point
|
|
|
|
| 477 |
correct_translation: Optional[str] = None
|
| 478 |
):
|
| 479 |
"""
|
| 480 |
+
Log user feedback to a CSV file and push to Hugging Face repo if running in Space.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
"""
|
| 482 |
# Define filename
|
| 483 |
src = source_lang.value.lower()
|
| 484 |
tgt = target_lang.value.lower()
|
| 485 |
filename = f"{src}_{tgt}.csv"
|
| 486 |
+
|
|
|
|
| 487 |
headers = [
|
| 488 |
"timestamp",
|
| 489 |
"source_language",
|
|
|
|
| 502 |
"notation": notation,
|
| 503 |
"correct_translation": correct_translation.strip() if correct_translation else ""
|
| 504 |
}
|
| 505 |
+
|
|
|
|
| 506 |
file_exists = Path(filename).exists()
|
| 507 |
+
|
| 508 |
try:
|
| 509 |
+
# Write to local CSV
|
| 510 |
with open(filename, mode="a", encoding="utf-8", newline="") as f:
|
| 511 |
writer = csv.DictWriter(f, fieldnames=headers)
|
| 512 |
if not file_exists:
|
| 513 |
+
writer.writeheader()
|
| 514 |
writer.writerow(row)
|
| 515 |
+
logger.info(f"Feedback saved locally to {filename}")
|
| 516 |
+
|
| 517 |
+
# Try to push to HF repo if HF_TOKEN exists
|
| 518 |
+
if hf_token := os.getenv("hffff"):
|
| 519 |
+
from huggingface_hub import Repository, upload_file, get_repo_info
|
| 520 |
+
|
| 521 |
+
try:
|
| 522 |
+
# Upload directly using upload_file (recommended for single files)
|
| 523 |
+
upload_file(
|
| 524 |
+
path_or_fileobj=filename,
|
| 525 |
+
path_in_repo=filename,
|
| 526 |
+
repo_id=os.getenv("LocaleNLP/LocaleNLP_Translator"), # e.g., "yourusername/your-space-name"
|
| 527 |
+
token=hf_token,
|
| 528 |
+
repo_type="space",
|
| 529 |
+
commit_message=f"Add/update feedback: {filename}"
|
| 530 |
+
)
|
| 531 |
+
logger.info(f"Successfully pushed {filename} to Hugging Face Space repo.")
|
| 532 |
+
except Exception as e:
|
| 533 |
+
logger.warning(f"Failed to push feedback to HF Hub: {e}")
|
| 534 |
except Exception as e:
|
| 535 |
logger.error(f"Failed to save feedback to {filename}: {e}")
|
| 536 |
|
|
|
|
| 703 |
notation_val: float,
|
| 704 |
correct_trans: str
|
| 705 |
):
|
| 706 |
+
"""Save feedback to CSV and clear form."""
|
| 707 |
try:
|
| 708 |
source_lang = Language(source_lang_str)
|
| 709 |
target_lang = Language(target_lang_str)
|
|
|
|
| 716 |
notation=notation_val,
|
| 717 |
correct_translation=correct_trans or None
|
| 718 |
)
|
| 719 |
+
# Return values to reset UI components
|
| 720 |
+
return (
|
| 721 |
+
"✅ Thank you for your feedback!", # feedback_status
|
| 722 |
+
None, # notation (reset slider)
|
| 723 |
+
"" # correct_translation (clear textbox)
|
| 724 |
+
)
|
| 725 |
except Exception as e:
|
| 726 |
logger.error(f"Feedback submission failed: {e}")
|
| 727 |
+
return (
|
| 728 |
+
f"❌ Failed to save feedback: {str(e)}",
|
| 729 |
+
gr保留(notation_val), # keep value on error
|
| 730 |
+
correct_trans # keep text on error
|
| 731 |
+
)
|
|
|
|
|
|
|
|
|
|
| 732 |
|
| 733 |
+
# Connect events
|
| 734 |
+
input_mode.change(
|
| 735 |
+
fn=update_visibility,
|
| 736 |
+
inputs=input_mode,
|
| 737 |
+
outputs=[input_text, audio_input, file_input, extracted_text, output_text]
|
| 738 |
+
)
|
| 739 |
+
|
| 740 |
+
# Chain: Process → Translate → Store context → Wait for feedback
|
| 741 |
+
process_event = translate_btn.click(
|
| 742 |
+
fn=handle_process,
|
| 743 |
+
inputs=[input_mode, input_lang, input_text, audio_input, file_input],
|
| 744 |
+
outputs=[extracted_text, output_text]
|
| 745 |
+
)
|
| 746 |
+
|
| 747 |
+
process_event.success(
|
| 748 |
+
fn=handle_translate_with_input,
|
| 749 |
+
inputs=[extracted_text, input_lang, output_lang],
|
| 750 |
+
outputs=[output_text, gr.State(), gr.State()]
|
| 751 |
+
).success(
|
| 752 |
+
fn=set_states_for_feedback,
|
| 753 |
+
inputs=[output_text, extracted_text, input_lang, output_lang],
|
| 754 |
+
outputs=[user_input_state, model_output_state, source_lang_state, target_lang_state]
|
| 755 |
+
)
|
| 756 |
+
|
| 757 |
+
# Feedback submission
|
| 758 |
+
submit_feedback.click(
|
| 759 |
+
fn=save_feedback,
|
| 760 |
+
inputs=[
|
| 761 |
+
user_input_state,
|
| 762 |
+
model_output_state,
|
| 763 |
+
source_lang_state,
|
| 764 |
+
target_lang_state,
|
| 765 |
+
notation,
|
| 766 |
+
correct_translation
|
| 767 |
+
],
|
| 768 |
+
outputs=feedback_status
|
| 769 |
+
)
|
| 770 |
+
|
| 771 |
+
return interface
|
| 772 |
|
| 773 |
# ================================
|
| 774 |
# Application Entry Point
|