Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,7 +27,7 @@ from bs4 import BeautifulSoup
|
|
| 27 |
from markdown import markdown
|
| 28 |
import chardet
|
| 29 |
from transformers import pipeline, MarianTokenizer, AutoModelForSeq2SeqLM
|
| 30 |
-
from huggingface_hub import login
|
| 31 |
|
| 32 |
# ================================
|
| 33 |
# Configuration & Constants
|
|
@@ -133,7 +133,8 @@ class ModelManager:
|
|
| 133 |
logger.info(f"Loading translation model: {config.model_name}")
|
| 134 |
|
| 135 |
# Authenticate with Hugging Face if token provided
|
| 136 |
-
|
|
|
|
| 137 |
login(token=hf_token)
|
| 138 |
|
| 139 |
model = AutoModelForSeq2SeqLM.from_pretrained(
|
|
@@ -513,17 +514,16 @@ class TranslationApp:
|
|
| 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 |
-
|
| 519 |
-
|
| 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=
|
| 527 |
token=hf_token,
|
| 528 |
repo_type="space",
|
| 529 |
commit_message=f"Add/update feedback: {filename}"
|
|
@@ -608,15 +608,14 @@ class TranslationApp:
|
|
| 608 |
# --- FEEDBACK SECTION ---
|
| 609 |
gr.Markdown("### 📝 Provide Feedback on Translation")
|
| 610 |
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
)
|
| 620 |
|
| 621 |
correct_translation = gr.Textbox(
|
| 622 |
label="Correct Translation (if incorrect)",
|
|
@@ -718,57 +717,57 @@ class TranslationApp:
|
|
| 718 |
)
|
| 719 |
# Return values to reset UI components
|
| 720 |
return (
|
| 721 |
-
"✅ Thank you for your feedback!",
|
| 722 |
-
None,
|
| 723 |
-
""
|
| 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 |
-
|
| 730 |
-
correct_trans
|
| 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 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
source_lang_state,
|
| 764 |
-
target_lang_state,
|
| 765 |
-
notation,
|
| 766 |
-
correct_translation
|
| 767 |
-
],
|
| 768 |
-
outputs=feedback_status
|
| 769 |
-
)
|
| 770 |
|
| 771 |
-
|
| 772 |
|
| 773 |
# ================================
|
| 774 |
# Application Entry Point
|
|
|
|
| 27 |
from markdown import markdown
|
| 28 |
import chardet
|
| 29 |
from transformers import pipeline, MarianTokenizer, AutoModelForSeq2SeqLM
|
| 30 |
+
from huggingface_hub import login, upload_file
|
| 31 |
|
| 32 |
# ================================
|
| 33 |
# Configuration & Constants
|
|
|
|
| 133 |
logger.info(f"Loading translation model: {config.model_name}")
|
| 134 |
|
| 135 |
# Authenticate with Hugging Face if token provided
|
| 136 |
+
hf_token = os.getenv("hffff")
|
| 137 |
+
if hf_token:
|
| 138 |
login(token=hf_token)
|
| 139 |
|
| 140 |
model = AutoModelForSeq2SeqLM.from_pretrained(
|
|
|
|
| 514 |
writer.writeheader()
|
| 515 |
writer.writerow(row)
|
| 516 |
logger.info(f"Feedback saved locally to {filename}")
|
| 517 |
+
|
| 518 |
# Try to push to HF repo if HF_TOKEN exists
|
| 519 |
+
hf_token = os.getenv("hffff")
|
| 520 |
+
space_id = os.getenv("SPACE_ID") # e.g., "yourusername/your-space-name"
|
| 521 |
+
if hf_token and space_id:
|
| 522 |
try:
|
|
|
|
| 523 |
upload_file(
|
| 524 |
path_or_fileobj=filename,
|
| 525 |
path_in_repo=filename,
|
| 526 |
+
repo_id=space_id,
|
| 527 |
token=hf_token,
|
| 528 |
repo_type="space",
|
| 529 |
commit_message=f"Add/update feedback: {filename}"
|
|
|
|
| 608 |
# --- FEEDBACK SECTION ---
|
| 609 |
gr.Markdown("### 📝 Provide Feedback on Translation")
|
| 610 |
|
| 611 |
+
notation = gr.Slider(
|
| 612 |
+
minimum=1,
|
| 613 |
+
maximum=5,
|
| 614 |
+
step=1,
|
| 615 |
+
label="Rate translation quality (1-5 stars)",
|
| 616 |
+
value=None,
|
| 617 |
+
interactive=True
|
| 618 |
+
)
|
|
|
|
| 619 |
|
| 620 |
correct_translation = gr.Textbox(
|
| 621 |
label="Correct Translation (if incorrect)",
|
|
|
|
| 717 |
)
|
| 718 |
# Return values to reset UI components
|
| 719 |
return (
|
| 720 |
+
"✅ Thank you for your feedback!",
|
| 721 |
+
None, # reset slider
|
| 722 |
+
"" # clear correction box
|
| 723 |
)
|
| 724 |
except Exception as e:
|
| 725 |
logger.error(f"Feedback submission failed: {e}")
|
| 726 |
return (
|
| 727 |
f"❌ Failed to save feedback: {str(e)}",
|
| 728 |
+
notation_val, # keep value
|
| 729 |
+
correct_trans # keep text
|
| 730 |
)
|
| 731 |
+
|
| 732 |
+
# Connect events
|
| 733 |
+
input_mode.change(
|
| 734 |
+
fn=update_visibility,
|
| 735 |
+
inputs=input_mode,
|
| 736 |
+
outputs=[input_text, audio_input, file_input, extracted_text, output_text]
|
| 737 |
+
)
|
| 738 |
|
| 739 |
+
# Chain: Process → Translate → Store context
|
| 740 |
+
process_event = translate_btn.click(
|
| 741 |
+
fn=handle_process,
|
| 742 |
+
inputs=[input_mode, input_lang, input_text, audio_input, file_input],
|
| 743 |
+
outputs=[extracted_text, output_text]
|
| 744 |
+
)
|
| 745 |
+
|
| 746 |
+
process_event.success(
|
| 747 |
+
fn=handle_translate_with_input,
|
| 748 |
+
inputs=[extracted_text, input_lang, output_lang],
|
| 749 |
+
outputs=[output_text, gr.State(), gr.State()]
|
| 750 |
+
).success(
|
| 751 |
+
fn=set_states_for_feedback,
|
| 752 |
+
inputs=[output_text, extracted_text, input_lang, output_lang],
|
| 753 |
+
outputs=[user_input_state, model_output_state, source_lang_state, target_lang_state]
|
| 754 |
+
)
|
| 755 |
+
|
| 756 |
+
# Feedback submission
|
| 757 |
+
submit_feedback.click(
|
| 758 |
+
fn=save_feedback,
|
| 759 |
+
inputs=[
|
| 760 |
+
user_input_state,
|
| 761 |
+
model_output_state,
|
| 762 |
+
source_lang_state,
|
| 763 |
+
target_lang_state,
|
| 764 |
+
notation,
|
| 765 |
+
correct_translation
|
| 766 |
+
],
|
| 767 |
+
outputs=[feedback_status, notation, correct_translation]
|
| 768 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 769 |
|
| 770 |
+
return interface
|
| 771 |
|
| 772 |
# ================================
|
| 773 |
# Application Entry Point
|