seawolf2357 commited on
Commit
2a8cb4b
·
verified ·
1 Parent(s): 19fd7dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -6
app.py CHANGED
@@ -245,12 +245,36 @@ class Demo:
245
  return []
246
 
247
  def remove_code_block(text):
248
- pattern = r'```html\n(.+?)\n```'
249
- match = re.search(pattern, text, re.DOTALL)
250
- if match:
251
- return match.group(1).strip()
252
- else:
253
- return text.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
 
255
  def history_render(history: History):
256
  return gr.update(open=True), history
 
245
  return []
246
 
247
  def remove_code_block(text):
248
+ # Remove markdown code block syntax
249
+ text = re.sub(r'```[python|html]?\n', '', text)
250
+ text = re.sub(r'\n```', '', text)
251
+
252
+ # Remove duplicate imports and launch configurations
253
+ lines = text.split('\n')
254
+ filtered_lines = []
255
+ seen_imports = set()
256
+
257
+ for line in lines:
258
+ # Skip empty lines
259
+ if not line.strip():
260
+ continue
261
+
262
+ # Skip duplicate imports
263
+ if line.startswith('import ') or line.startswith('from '):
264
+ import_key = line.split('#')[0].strip()
265
+ if import_key in seen_imports:
266
+ continue
267
+ seen_imports.add(import_key)
268
+
269
+ # Skip duplicate launch configurations
270
+ if 'if __name__ == "__main__":' in line:
271
+ continue
272
+ if 'demo.launch()' in line:
273
+ continue
274
+
275
+ filtered_lines.append(line)
276
+
277
+ return '\n'.join(filtered_lines)
278
 
279
  def history_render(history: History):
280
  return gr.update(open=True), history