Spaces:
Runtime error
Runtime error
import braille | |
from transformers import pipeline | |
import re | |
# Initialize the summarization pipeline for context understanding | |
summarizer = None | |
def get_summarizer(): | |
"""Get or initialize the summarization model.""" | |
global summarizer | |
if summarizer is None: | |
try: | |
# Use a small, efficient model for summarization | |
summarizer = pipeline( | |
"summarization", | |
model="facebook/bart-large-cnn", | |
max_length=100, | |
min_length=30, | |
truncation=True | |
) | |
except Exception as e: | |
print(f"Error loading summarizer: {str(e)}") | |
return summarizer | |
def text_to_braille(text, use_context=True): | |
""" | |
Convert text to Braille, with optional context enhancement. | |
Args: | |
text: Text to convert to Braille | |
use_context: Whether to use AI to enhance context understanding | |
Returns: | |
Dictionary with Braille text and metadata | |
""" | |
try: | |
# Basic Braille translation | |
braille_text = braille.grade2(text) | |
# If context enhancement is enabled | |
context_summary = None | |
if use_context and len(text) > 200: # Only for longer texts | |
summarizer = get_summarizer() | |
if summarizer: | |
try: | |
# Generate a summary to understand context | |
summary_result = summarizer(text) | |
if summary_result and len(summary_result) > 0: | |
context_summary = summary_result[0]['summary_text'] | |
except Exception as e: | |
print(f"Summarization error: {str(e)}") | |
# Format the Braille text for better readability | |
formatted_braille = format_braille_text(braille_text) | |
return { | |
'braille_text': braille_text, | |
'formatted_braille': formatted_braille, | |
'context_summary': context_summary, | |
'success': True | |
} | |
except Exception as e: | |
return { | |
'braille_text': '', | |
'error': str(e), | |
'success': False | |
} | |
def format_braille_text(braille_text, line_length=32): | |
""" | |
Format Braille text for better readability. | |
Args: | |
braille_text: Raw Braille text | |
line_length: Maximum characters per line | |
Returns: | |
Formatted Braille text | |
""" | |
# Split text by existing newlines first | |
paragraphs = braille_text.split('\n') | |
formatted_paragraphs = [] | |
for paragraph in paragraphs: | |
# Skip empty paragraphs | |
if not paragraph.strip(): | |
formatted_paragraphs.append('') | |
continue | |
# Word wrap to line_length | |
words = paragraph.split(' ') | |
lines = [] | |
current_line = [] | |
current_length = 0 | |
for word in words: | |
# If adding this word exceeds line length, start a new line | |
if current_length + len(word) + (1 if current_length > 0 else 0) > line_length: | |
lines.append(' '.join(current_line)) | |
current_line = [word] | |
current_length = len(word) | |
else: | |
if current_length > 0: | |
current_length += 1 # Space | |
current_line.append(word) | |
current_length += len(word) | |
# Add the last line if not empty | |
if current_line: | |
lines.append(' '.join(current_line)) | |
formatted_paragraphs.append('\n'.join(lines)) | |
# Join paragraphs with double newlines | |
return '\n\n'.join(formatted_paragraphs) | |
def get_braille_metadata(text): | |
""" | |
Get metadata about the Braille translation. | |
Args: | |
text: Original text | |
Returns: | |
Dictionary with metadata | |
""" | |
word_count = len(re.findall(r'\b\w+\b', text)) | |
character_count = len(text) | |
line_count = len(text.split('\n')) | |
return { | |
'word_count': word_count, | |
'character_count': character_count, | |
'line_count': line_count | |
} | |