Frenchizer commited on
Commit
ed2703c
·
verified ·
1 Parent(s): 094d492

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -23
app.py CHANGED
@@ -23,39 +23,41 @@ def preprocess_capitalization(text: str) -> str:
23
 
24
  return " ".join(processed_words)
25
 
26
- def preprocess_text(text: str, is_spell_corrected: bool = False):
27
  """Process text and return corrections with position information."""
28
  result = {
29
  "spell_suggestions": [],
30
- "other_suggestions": [], # For spell_checker suggestions
31
  "entities": [],
32
  "tags": []
33
  }
34
 
35
- # Apply capitalization preprocessing (spell suggestions)
36
- capitalized_text = preprocess_capitalization(text)
37
- if capitalized_text != text and not is_spell_corrected:
38
- result["spell_suggestions"].append({
39
- "original": text,
40
- "corrected": capitalized_text
41
- })
42
- text = capitalized_text # Update text after preprocessing
 
 
43
 
44
- # Translate right after preprocessing
 
 
 
 
 
 
 
 
45
  client = Client("Frenchizer/space_17")
46
  try:
47
  translation = client.predict(text)
48
  except Exception as e:
49
  translation = f"Error: {str(e)}"
50
 
51
- # Transformer spell check (other suggestions), after translation
52
- spell_checked = spell_checker(text, max_length=512)[0]['generated_text']
53
- if spell_checked != text:
54
- result["other_suggestions"].append({
55
- "original": text,
56
- "corrected": spell_checked
57
- })
58
-
59
  # Add entities and tags
60
  doc = nlp(text)
61
  result["entities"] = [{"text": ent.text, "label": ent.label_} for ent in doc.ents]
@@ -63,18 +65,18 @@ def preprocess_text(text: str, is_spell_corrected: bool = False):
63
 
64
  return translation, result
65
 
66
- def preprocess_and_forward(text: str, is_spell_corrected: bool = False):
67
  """Process text and forward to translation service."""
68
- translation, preprocessing_result = preprocess_text(text, is_spell_corrected)
69
  return translation, preprocessing_result
70
 
71
  # Gradio interface
72
  with gr.Blocks() as demo:
73
  input_text = gr.Textbox(label="Input Text")
74
- is_spell_corrected = gr.Checkbox(label="Spell Corrected", value=False, visible=False) # Hidden flag
75
  output_text = gr.Textbox(label="Output Text")
76
  preprocess_button = gr.Button("Process")
77
- preprocess_button.click(fn=preprocess_and_forward, inputs=[input_text, is_spell_corrected], outputs=[output_text])
78
 
79
  if __name__ == "__main__":
80
  demo.launch()
 
23
 
24
  return " ".join(processed_words)
25
 
26
+ def preprocess_text(text: str, is_suggestion_applied: bool = False):
27
  """Process text and return corrections with position information."""
28
  result = {
29
  "spell_suggestions": [],
30
+ "other_suggestions": [],
31
  "entities": [],
32
  "tags": []
33
  }
34
 
35
+ # Only generate suggestions if no suggestion has been applied
36
+ if not is_suggestion_applied:
37
+ # Apply capitalization preprocessing (spell suggestions)
38
+ capitalized_text = preprocess_capitalization(text)
39
+ if capitalized_text != text:
40
+ result["spell_suggestions"].append({
41
+ "original": text,
42
+ "corrected": capitalized_text
43
+ })
44
+ text = capitalized_text # Update text for further processing
45
 
46
+ # Transformer spell check (other suggestions)
47
+ spell_checked = spell_checker(text, max_length=512)[0]['generated_text']
48
+ if spell_checked != text:
49
+ result["other_suggestions"].append({
50
+ "original": text,
51
+ "corrected": spell_checked
52
+ })
53
+
54
+ # Translate the text (after preprocessing if first pass, or as-is if suggestion applied)
55
  client = Client("Frenchizer/space_17")
56
  try:
57
  translation = client.predict(text)
58
  except Exception as e:
59
  translation = f"Error: {str(e)}"
60
 
 
 
 
 
 
 
 
 
61
  # Add entities and tags
62
  doc = nlp(text)
63
  result["entities"] = [{"text": ent.text, "label": ent.label_} for ent in doc.ents]
 
65
 
66
  return translation, result
67
 
68
+ def preprocess_and_forward(text: str, is_suggestion_applied: bool = False):
69
  """Process text and forward to translation service."""
70
+ translation, preprocessing_result = preprocess_text(text, is_suggestion_applied)
71
  return translation, preprocessing_result
72
 
73
  # Gradio interface
74
  with gr.Blocks() as demo:
75
  input_text = gr.Textbox(label="Input Text")
76
+ is_suggestion_applied = gr.Checkbox(label="Suggestion Applied", value=False, visible=False) # Hidden flag
77
  output_text = gr.Textbox(label="Output Text")
78
  preprocess_button = gr.Button("Process")
79
+ preprocess_button.click(fn=preprocess_and_forward, inputs=[input_text, is_suggestion_applied], outputs=[output_text])
80
 
81
  if __name__ == "__main__":
82
  demo.launch()