Mgolo commited on
Commit
d1ec28b
·
verified ·
1 Parent(s): dd02666

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -60
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
- if hf_token := os.getenv("hffff"):
 
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
- 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}"
@@ -608,15 +608,14 @@ class TranslationApp:
608
  # --- FEEDBACK SECTION ---
609
  gr.Markdown("### 📝 Provide Feedback on Translation")
610
 
611
- with gr.Row():
612
- notation = gr.Slider(
613
- minimum=1,
614
- maximum=5,
615
- step=1,
616
- label="Rate translation quality (1-5 stars)",
617
- value=None,
618
- interactive=True
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!", # 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
 
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