-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell_processors.py
More file actions
287 lines (229 loc) · 10.7 KB
/
cell_processors.py
File metadata and controls
287 lines (229 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""
Cell processing modules for different notebook cell types
"""
import re
from state import AgentState
from llm_client import LLMClient, get_image_data
from config import get_translation_label, get_description_label
from typing import Dict, Any
import copy
# Initialize LLM client
llm_client = LLMClient()
def process_markdown_cell(state: AgentState) -> AgentState:
"""
Process a markdown cell: translate text and describe images
Args:
state: AgentState with current cell to process
Returns:
Updated AgentState with processed cell added to processed_cells
"""
try:
current_index = state["current_cell_index"]
if current_index >= len(state["notebook_content"]["cells"]):
state["error_message"] = "Cell index out of range"
return state
cell = state["notebook_content"]["cells"][current_index]
# Create a copy of the cell to modify
processed_cell = copy.deepcopy(cell)
if cell["cell_type"] != "markdown":
state["error_message"] = f"Expected markdown cell, got {cell['cell_type']}"
return state
original_source = cell["source"]
target_language = state["target_language"]
# Process the source content
new_source_lines = []
# Regex to find images: 
image_pattern = re.compile(r'!\[([^\]]*)\]\(([^)]+)\)')
# Process the content by sections (separated by blank lines or headers)
if isinstance(original_source, list):
source_text = ''.join(original_source)
else:
source_text = original_source
source_lines = source_text.split('\n')
current_section = []
sections = []
# Group lines into sections
for line in source_lines:
if line.strip() == "" or line.startswith('#'):
if current_section:
sections.append(current_section)
current_section = []
if line.strip(): # Add the header line to start new section
current_section.append(line)
else:
current_section.append(line)
# Add the last section if exists
if current_section:
sections.append(current_section)
# Process each section
for section in sections:
if not section:
continue
section_text = '\n'.join(section)
# Add original section
new_source_lines.extend(section)
# Check for images in the section
image_matches = image_pattern.findall(section_text)
for alt_text, src in image_matches:
try:
# Get image data and generate description
# Pass input_path to resolve relative image paths
image_data = get_image_data(src, state.get("input_path"))
description = llm_client.describe_image(image_data, target_language)
description_label = get_description_label(target_language)
new_source_lines.append("")
new_source_lines.append(f"**{description_label}:**")
new_source_lines.append(description)
print(f"✅ Generated image description for: {src}")
except Exception as e:
print(f"⚠️ Could not process image {src}: {e}")
# Translate the section if it contains meaningful text
if any(line.strip() and not line.startswith('!') for line in section):
try:
# Translate the entire section
translation = llm_client.translate_text(section_text, target_language)
translation_label = get_translation_label(target_language)
# Add translation with better formatting
new_source_lines.append("")
new_source_lines.append(f"**{translation_label}:**")
new_source_lines.append(translation)
print(f"✅ Translated section: {section_text[:50]}...")
except Exception as e:
print(f"⚠️ Could not translate section: {e}")
# Add spacing between sections
new_source_lines.append("")
# Update the cell source
processed_cell["source"] = new_source_lines
# Add to processed cells
state["processed_cells"].append(processed_cell)
state["current_cell_index"] += 1
print(f"📝 Processed markdown cell {current_index + 1}/{state['total_cells']}")
return state
except Exception as e:
state["error_message"] = f"Error processing markdown cell: {str(e)}"
print(f"Error: {state['error_message']}")
return state
def process_code_cell(state: AgentState) -> AgentState:
"""
Process a code cell: add explanatory comments and translate existing comments
Args:
state: AgentState with current cell to process
Returns:
Updated AgentState with processed cell added to processed_cells
"""
try:
current_index = state["current_cell_index"]
if current_index >= len(state["notebook_content"]["cells"]):
state["error_message"] = "Cell index out of range"
return state
cell = state["notebook_content"]["cells"][current_index]
# Create a copy of the cell to modify
processed_cell = copy.deepcopy(cell)
if cell["cell_type"] != "code":
state["error_message"] = f"Expected code cell, got {cell['cell_type']}"
return state
original_source = cell["source"]
target_language = state["target_language"]
# Convert source to string if it's a list
if isinstance(original_source, list):
code_content = ''.join(original_source)
else:
code_content = original_source
try:
# Add comments and translate existing ones
enhanced_code = llm_client.add_code_comments(code_content, target_language)
# Clean up any markdown code block wrapping that might have been added
enhanced_code = enhanced_code.strip()
if enhanced_code.startswith('```python'):
enhanced_code = enhanced_code[9:] # Remove ```python
elif enhanced_code.startswith('```'):
enhanced_code = enhanced_code[3:] # Remove generic ```
if enhanced_code.endswith('```'):
enhanced_code = enhanced_code[:-3] # Remove ending ```
enhanced_code = enhanced_code.strip()
processed_cell["source"] = enhanced_code
print(f"✅ Enhanced code cell {current_index + 1}/{state['total_cells']}")
except Exception as e:
print(f"⚠️ Could not enhance code cell: {e}")
# Keep original code if enhancement fails
processed_cell["source"] = original_source
# Add to processed cells
state["processed_cells"].append(processed_cell)
state["current_cell_index"] += 1
print(f"💻 Processed code cell {current_index + 1}/{state['total_cells']}")
return state
except Exception as e:
state["error_message"] = f"Error processing code cell: {str(e)}"
print(f"Error: {state['error_message']}")
return state
def route_cell_processing(state: AgentState) -> str:
"""
Router function to determine which processing node to use next
Args:
state: Current AgentState
Returns:
Next node name or END
"""
try:
# Check for errors
if state.get("error_message"):
return "END"
# Check if we've processed all cells
current_index = state["current_cell_index"]
total_cells = state["total_cells"]
if current_index >= total_cells:
print("🎉 All cells processed successfully!")
return "rebuild_notebook"
# Get the current cell type
cell = state["notebook_content"]["cells"][current_index]
cell_type = cell["cell_type"]
if cell_type == "markdown":
return "process_markdown_cell"
elif cell_type == "code":
return "process_code_cell"
else:
# For other cell types (raw, etc.), just copy as-is
processed_cell = copy.deepcopy(cell)
state["processed_cells"].append(processed_cell)
state["current_cell_index"] += 1
print(f"⏭️ Skipped {cell_type} cell {current_index + 1}/{total_cells}")
# Check if there are more cells to process after skipping this one
if state["current_cell_index"] >= total_cells:
return "rebuild_notebook"
else:
# Get the next cell type to route to appropriate processor
next_cell = state["notebook_content"]["cells"][state["current_cell_index"]]
next_cell_type = next_cell["cell_type"]
if next_cell_type == "markdown":
return "process_markdown_cell"
elif next_cell_type == "code":
return "process_code_cell"
else:
# If next cell is also unsupported, create a special routing
return "skip_unsupported_cell"
except Exception as e:
state["error_message"] = f"Error in routing: {str(e)}"
print(f"Error: {state['error_message']}")
return "END"
def skip_unsupported_cell(state: AgentState) -> AgentState:
"""
Skip unsupported cell types by copying them as-is
Args:
state: AgentState with current cell to process
Returns:
Updated AgentState with cell copied to processed_cells
"""
try:
current_index = state["current_cell_index"]
if current_index >= len(state["notebook_content"]["cells"]):
return state
cell = state["notebook_content"]["cells"][current_index]
# Copy cell as-is
processed_cell = copy.deepcopy(cell)
state["processed_cells"].append(processed_cell)
state["current_cell_index"] += 1
print(f"⏭️ Skipped {cell['cell_type']} cell {current_index + 1}/{state['total_cells']}")
return state
except Exception as e:
state["error_message"] = f"Error skipping cell: {str(e)}"
return state