Spaces:
Sleeping
Sleeping
Update models/braille_translator.py
Browse files- models/braille_translator.py +78 -0
models/braille_translator.py
CHANGED
|
@@ -53,10 +53,88 @@ def text_to_grade1_braille(text):
|
|
| 53 |
|
| 54 |
return braille_text
|
| 55 |
|
|
|
|
| 56 |
def text_to_braille(text, use_context=True):
|
| 57 |
"""
|
| 58 |
Convert text to Braille, with optional context enhancement.
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
Args:
|
| 61 |
text: Text to convert to Braille
|
| 62 |
use_context: Whether to use AI to enhance context understanding
|
|
|
|
| 53 |
|
| 54 |
return braille_text
|
| 55 |
|
| 56 |
+
|
| 57 |
def text_to_braille(text, use_context=True):
|
| 58 |
"""
|
| 59 |
Convert text to Braille, with optional context enhancement.
|
| 60 |
|
| 61 |
+
Args:
|
| 62 |
+
text: Text to convert to Braille
|
| 63 |
+
use_context: Whether to use AI to enhance context understanding
|
| 64 |
+
|
| 65 |
+
Returns:
|
| 66 |
+
Dictionary with Braille text and metadata
|
| 67 |
+
"""
|
| 68 |
+
try:
|
| 69 |
+
# Basic Braille translation
|
| 70 |
+
braille_text = text_to_grade1_braille(text)
|
| 71 |
+
|
| 72 |
+
# Create an ASCII representation for PDF
|
| 73 |
+
ascii_braille = unicode_braille_to_ascii(braille_text)
|
| 74 |
+
|
| 75 |
+
# If context enhancement is enabled
|
| 76 |
+
context_summary = None
|
| 77 |
+
if use_context and len(text) > 200: # Only for longer texts
|
| 78 |
+
summarizer = get_summarizer()
|
| 79 |
+
if summarizer:
|
| 80 |
+
try:
|
| 81 |
+
# Generate a summary to understand context
|
| 82 |
+
summary_result = summarizer(text)
|
| 83 |
+
if summary_result and len(summary_result) > 0:
|
| 84 |
+
context_summary = summary_result[0]['summary_text']
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Summarization error: {str(e)}")
|
| 87 |
+
|
| 88 |
+
# Format the Braille text for better readability
|
| 89 |
+
formatted_braille = format_braille_text(braille_text)
|
| 90 |
+
formatted_ascii = format_braille_text(ascii_braille)
|
| 91 |
+
|
| 92 |
+
return {
|
| 93 |
+
'braille_text': braille_text,
|
| 94 |
+
'formatted_braille': formatted_braille,
|
| 95 |
+
'ascii_braille': ascii_braille,
|
| 96 |
+
'formatted_ascii': formatted_ascii,
|
| 97 |
+
'context_summary': context_summary,
|
| 98 |
+
'success': True
|
| 99 |
+
}
|
| 100 |
+
except Exception as e:
|
| 101 |
+
return {
|
| 102 |
+
'braille_text': '',
|
| 103 |
+
'error': str(e),
|
| 104 |
+
'success': False
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
def unicode_braille_to_ascii(braille_text):
|
| 108 |
+
"""
|
| 109 |
+
Convert Unicode Braille to ASCII representation.
|
| 110 |
+
|
| 111 |
+
Args:
|
| 112 |
+
braille_text: Text with Unicode Braille characters
|
| 113 |
+
|
| 114 |
+
Returns:
|
| 115 |
+
ASCII representation of Braille
|
| 116 |
+
"""
|
| 117 |
+
# Map each Braille character to a descriptive ASCII representation
|
| 118 |
+
result = ""
|
| 119 |
+
for char in braille_text:
|
| 120 |
+
if char in BRAILLE_MAP.values():
|
| 121 |
+
# For Braille characters, use a letter representation
|
| 122 |
+
for letter, braille in BRAILLE_MAP.items():
|
| 123 |
+
if braille == char and len(letter) == 1:
|
| 124 |
+
result += f"[{letter.upper()}]"
|
| 125 |
+
break
|
| 126 |
+
else:
|
| 127 |
+
result += "[?]"
|
| 128 |
+
else:
|
| 129 |
+
# For non-Braille characters, keep them as is
|
| 130 |
+
result += char
|
| 131 |
+
|
| 132 |
+
return result
|
| 133 |
+
|
| 134 |
+
def text_to_braille1(text, use_context=True):
|
| 135 |
+
"""
|
| 136 |
+
Convert text to Braille, with optional context enhancement.
|
| 137 |
+
|
| 138 |
Args:
|
| 139 |
text: Text to convert to Braille
|
| 140 |
use_context: Whether to use AI to enhance context understanding
|