Spaces:
Running
Running
Update app.py
Browse files
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,
|
27 |
"""Process text and return corrections with position information."""
|
28 |
result = {
|
29 |
"spell_suggestions": [],
|
30 |
-
"other_suggestions": [],
|
31 |
"entities": [],
|
32 |
"tags": []
|
33 |
}
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
"
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
67 |
"""Process text and forward to translation service."""
|
68 |
-
translation, preprocessing_result = preprocess_text(text,
|
69 |
return translation, preprocessing_result
|
70 |
|
71 |
# Gradio interface
|
72 |
with gr.Blocks() as demo:
|
73 |
input_text = gr.Textbox(label="Input Text")
|
74 |
-
|
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,
|
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()
|