Julien Matuszak commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,29 +28,37 @@ def preprocess_capitalization(text: str) -> str:
|
|
| 28 |
def preprocess_text(text: str):
|
| 29 |
"""Process text and return corrections with position information."""
|
| 30 |
result = {
|
| 31 |
-
"
|
| 32 |
-
"entities": [],
|
| 33 |
-
"tags": []
|
| 34 |
}
|
| 35 |
|
| 36 |
-
# Apply capitalization preprocessing
|
| 37 |
capitalized_text = preprocess_capitalization(text)
|
| 38 |
if capitalized_text != text:
|
| 39 |
-
result["
|
| 40 |
"original": text,
|
| 41 |
-
"corrected": capitalized_text
|
|
|
|
| 42 |
})
|
| 43 |
-
text = capitalized_text
|
| 44 |
|
| 45 |
-
# Transformer spell check
|
| 46 |
spell_checked = spell_checker(text, max_length=512)[0]['generated_text']
|
| 47 |
if spell_checked != text:
|
| 48 |
-
result["
|
| 49 |
"original": text,
|
| 50 |
-
"corrected": spell_checked
|
|
|
|
| 51 |
})
|
| 52 |
|
| 53 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
doc = nlp(text)
|
| 55 |
result["entities"] = [{"text": ent.text, "label": ent.label_} for ent in doc.ents]
|
| 56 |
result["tags"] = [token.text for token in doc if token.text.startswith(('#', '@'))]
|
|
@@ -60,16 +68,14 @@ def preprocess_text(text: str):
|
|
| 60 |
def preprocess_and_forward(text: str):
|
| 61 |
"""Process text and forward to translation service."""
|
| 62 |
original_text, preprocessing_result = preprocess_text(text)
|
| 63 |
-
|
| 64 |
-
# Forward original text to translation service
|
| 65 |
-
client = Client("Frenchizer/space_21")
|
| 66 |
try:
|
| 67 |
translation = client.predict(original_text)
|
| 68 |
return translation, preprocessing_result
|
| 69 |
except Exception as e:
|
| 70 |
return f"Error: {str(e)}", preprocessing_result
|
| 71 |
|
| 72 |
-
# Gradio
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
input_text = gr.Textbox(label="Input Text")
|
| 75 |
output_text = gr.Textbox(label="Output Text")
|
|
|
|
| 28 |
def preprocess_text(text: str):
|
| 29 |
"""Process text and return corrections with position information."""
|
| 30 |
result = {
|
| 31 |
+
"suggestions": [] # Rename to generic "suggestions" for flexibility
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
+
# Apply capitalization preprocessing (Spell type)
|
| 35 |
capitalized_text = preprocess_capitalization(text)
|
| 36 |
if capitalized_text != text:
|
| 37 |
+
result["suggestions"].append({
|
| 38 |
"original": text,
|
| 39 |
+
"corrected": capitalized_text,
|
| 40 |
+
"type": "spell" # Add type
|
| 41 |
})
|
| 42 |
+
text = capitalized_text
|
| 43 |
|
| 44 |
+
# Transformer spell check (Spell type)
|
| 45 |
spell_checked = spell_checker(text, max_length=512)[0]['generated_text']
|
| 46 |
if spell_checked != text:
|
| 47 |
+
result["suggestions"].append({
|
| 48 |
"original": text,
|
| 49 |
+
"corrected": spell_checked,
|
| 50 |
+
"type": "spell" # Add type
|
| 51 |
})
|
| 52 |
|
| 53 |
+
# Example "other" suggestion (e.g., adding punctuation)
|
| 54 |
+
if not text.endswith("!") and text.strip():
|
| 55 |
+
result["suggestions"].append({
|
| 56 |
+
"original": text,
|
| 57 |
+
"corrected": text + "!",
|
| 58 |
+
"type": "other" # Add type for non-spell suggestions
|
| 59 |
+
})
|
| 60 |
+
|
| 61 |
+
# Add entities and tags (unchanged)
|
| 62 |
doc = nlp(text)
|
| 63 |
result["entities"] = [{"text": ent.text, "label": ent.label_} for ent in doc.ents]
|
| 64 |
result["tags"] = [token.text for token in doc if token.text.startswith(('#', '@'))]
|
|
|
|
| 68 |
def preprocess_and_forward(text: str):
|
| 69 |
"""Process text and forward to translation service."""
|
| 70 |
original_text, preprocessing_result = preprocess_text(text)
|
| 71 |
+
client = Client("Frenchizer/space_17")
|
|
|
|
|
|
|
| 72 |
try:
|
| 73 |
translation = client.predict(original_text)
|
| 74 |
return translation, preprocessing_result
|
| 75 |
except Exception as e:
|
| 76 |
return f"Error: {str(e)}", preprocessing_result
|
| 77 |
|
| 78 |
+
# Update Gradio outputs to match
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
input_text = gr.Textbox(label="Input Text")
|
| 81 |
output_text = gr.Textbox(label="Output Text")
|