Chamin09 commited on
Commit
46a7853
·
verified ·
1 Parent(s): 8d42c7c

Create braille_translator.py

Browse files
Files changed (1) hide show
  1. models/braille_translator.py +166 -0
models/braille_translator.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import re
3
+
4
+ # English to Braille mapping (Grade 1 Braille)
5
+ BRAILLE_MAP = {
6
+ 'a': '⠁', 'b': '⠃', 'c': '⠉', 'd': '⠙', 'e': '⠑', 'f': '⠋', 'g': '⠛', 'h': '⠓', 'i': '⠊', 'j': '⠚',
7
+ 'k': '⠅', 'l': '⠇', 'm': '⠍', 'n': '⠝', 'o': '⠕', 'p': '⠏', 'q': '⠟', 'r': '⠗', 's': '⠎', 't': '⠞',
8
+ 'u': '⠥', 'v': '⠧', 'w': '⠺', 'x': '⠭', 'y': '⠽', 'z': '⠵',
9
+ '0': '⠚', '1': '⠁', '2': '⠃', '3': '⠉', '4': '⠙', '5': '⠑', '6': '⠋', '7': '⠛', '8': '⠓', '9': '⠊',
10
+ '.': '⠲', ',': '⠂', ';': '⠆', ':': '⠒', '!': '⠖', '?': '⠦', '"': '⠦', "'": '⠄', '(': '⠐⠣', ')': '⠐⠜',
11
+ '-': '⠤', '/': '⠌', '+': '⠬', '=': '⠐⠶', '*': '⠐⠔', '&': '⠯', '%': '⠐⠏', '#': '⠼', '@': '⠐⠁',
12
+ '$': '⠐⠎', '€': '⠐⠑', '£': '⠐⠇', '¥': '⠐⠽', '₹': '⠐⠗',
13
+ ' ': '⠀'
14
+ }
15
+
16
+ # Initialize the summarization pipeline for context understanding
17
+ summarizer = None
18
+
19
+ def get_summarizer():
20
+ """Get or initialize the summarization model."""
21
+ global summarizer
22
+ if summarizer is None:
23
+ try:
24
+ # Use a small, efficient model for summarization
25
+ summarizer = pipeline(
26
+ "summarization",
27
+ model="facebook/bart-large-cnn",
28
+ max_length=100,
29
+ min_length=30,
30
+ truncation=True
31
+ )
32
+ except Exception as e:
33
+ print(f"Error loading summarizer: {str(e)}")
34
+ return summarizer
35
+
36
+ def text_to_grade1_braille(text):
37
+ """
38
+ Convert text to Grade 1 Braille.
39
+
40
+ Args:
41
+ text: Text to convert
42
+
43
+ Returns:
44
+ Braille text
45
+ """
46
+ braille_text = ""
47
+ for char in text.lower():
48
+ if char in BRAILLE_MAP:
49
+ braille_text += BRAILLE_MAP[char]
50
+ else:
51
+ # For characters not in our map, just keep the original
52
+ braille_text += char
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
63
+
64
+ Returns:
65
+ Dictionary with Braille text and metadata
66
+ """
67
+ try:
68
+ # Basic Braille translation
69
+ braille_text = text_to_grade1_braille(text)
70
+
71
+ # If context enhancement is enabled
72
+ context_summary = None
73
+ if use_context and len(text) > 200: # Only for longer texts
74
+ summarizer = get_summarizer()
75
+ if summarizer:
76
+ try:
77
+ # Generate a summary to understand context
78
+ summary_result = summarizer(text)
79
+ if summary_result and len(summary_result) > 0:
80
+ context_summary = summary_result[0]['summary_text']
81
+ except Exception as e:
82
+ print(f"Summarization error: {str(e)}")
83
+
84
+ # Format the Braille text for better readability
85
+ formatted_braille = format_braille_text(braille_text)
86
+
87
+ return {
88
+ 'braille_text': braille_text,
89
+ 'formatted_braille': formatted_braille,
90
+ 'context_summary': context_summary,
91
+ 'success': True
92
+ }
93
+ except Exception as e:
94
+ return {
95
+ 'braille_text': '',
96
+ 'error': str(e),
97
+ 'success': False
98
+ }
99
+
100
+ def format_braille_text(braille_text, line_length=32):
101
+ """
102
+ Format Braille text for better readability.
103
+
104
+ Args:
105
+ braille_text: Raw Braille text
106
+ line_length: Maximum characters per line
107
+
108
+ Returns:
109
+ Formatted Braille text
110
+ """
111
+ # Split text by existing newlines first
112
+ paragraphs = braille_text.split('\n')
113
+ formatted_paragraphs = []
114
+
115
+ for paragraph in paragraphs:
116
+ # Skip empty paragraphs
117
+ if not paragraph.strip():
118
+ formatted_paragraphs.append('')
119
+ continue
120
+
121
+ # Word wrap to line_length
122
+ words = paragraph.split(' ')
123
+ lines = []
124
+ current_line = []
125
+ current_length = 0
126
+
127
+ for word in words:
128
+ # If adding this word exceeds line length, start a new line
129
+ if current_length + len(word) + (1 if current_length > 0 else 0) > line_length:
130
+ lines.append(' '.join(current_line))
131
+ current_line = [word]
132
+ current_length = len(word)
133
+ else:
134
+ if current_length > 0:
135
+ current_length += 1 # Space
136
+ current_line.append(word)
137
+ current_length += len(word)
138
+
139
+ # Add the last line if not empty
140
+ if current_line:
141
+ lines.append(' '.join(current_line))
142
+
143
+ formatted_paragraphs.append('\n'.join(lines))
144
+
145
+ # Join paragraphs with double newlines
146
+ return '\n\n'.join(formatted_paragraphs)
147
+
148
+ def get_braille_metadata(text):
149
+ """
150
+ Get metadata about the Braille translation.
151
+
152
+ Args:
153
+ text: Original text
154
+
155
+ Returns:
156
+ Dictionary with metadata
157
+ """
158
+ word_count = len(re.findall(r'\b\w+\b', text))
159
+ character_count = len(text)
160
+ line_count = len(text.split('\n'))
161
+
162
+ return {
163
+ 'word_count': word_count,
164
+ 'character_count': character_count,
165
+ 'line_count': line_count
166
+ }