Rishi-Jain-27 commited on
Commit
43c8912
Β·
1 Parent(s): 9f61252

Updated cleaning system

Browse files
Files changed (2) hide show
  1. README.md +2 -2
  2. app.py +21 -5
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: CodeFlow
3
  emoji: πŸ“Š
4
- colorFrom: pink
5
- colorTo: purple
6
  sdk: gradio
7
  sdk_version: 6.16.0
8
  python_version: '3.13'
 
1
  ---
2
  title: CodeFlow
3
  emoji: πŸ“Š
4
+ colorFrom: indigo
5
+ colorTo: cyan
6
  sdk: gradio
7
  sdk_version: 6.16.0
8
  python_version: '3.13'
app.py CHANGED
@@ -35,6 +35,24 @@ llm = Llama(
35
  app = gr.Server(title="Code-to-Flowchart Generator")
36
 
37
  # ----- Functions ----- #
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  @app.api(name="generate_flowchart")
39
  def generate_flowchart(src_code: str) -> str:
40
  # check if src_code is empty
@@ -113,11 +131,9 @@ def generate_flowchart(src_code: str) -> str:
113
  # remove the thinking tags from the response
114
  cleaned = re.sub(r'<thinking>.*?</thinking>', '', content, flags=re.DOTALL)
115
 
116
- # Remove raw quotes on both parentheses and non-parentheses
117
- cleaned = cleaned.replace('"', "'")
118
- cleaned = re.sub(r'\[([^\[\]]+)\]', r'["\g<1>"]', cleaned)
119
- cleaned = re.sub(r'\{([^{}]+)\}', r'{"\g<1>"}', cleaned)
120
-
121
  return cleaned.strip() # and remove excess whitespace
122
 
123
  # ----- Custom Frontend ----- #
 
35
  app = gr.Server(title="Code-to-Flowchart Generator")
36
 
37
  # ----- Functions ----- #
38
+
39
+ # This is a cleaning function to resolve common syntax errors.
40
+ def quote_labels(text: str) -> str:
41
+ # Mermaid node labels can't hold raw code char, so quote-wrap each label body.
42
+ END = r'(?=\s*(?:--|==|-\.|<|\||;|$))'
43
+
44
+ def esc(body: str) -> str:
45
+ return (body.replace('"', "'")
46
+ .replace('[', '&#91;').replace(']', '&#93;')
47
+ .replace('{', '&#123;').replace('}', '&#125;'))
48
+
49
+ out = []
50
+ for line in text.split('\n'):
51
+ line = re.sub(r'(?<=\w)\[(.*?)\]' + END, lambda m: '["' + esc(m.group(1)) + '"]', line)
52
+ line = re.sub(r'(?<=\w)\{(.*?)\}' + END, lambda m: '{"' + esc(m.group(1)) + '"}', line)
53
+ out.append(line)
54
+ return '\n'.join(out)
55
+
56
  @app.api(name="generate_flowchart")
57
  def generate_flowchart(src_code: str) -> str:
58
  # check if src_code is empty
 
131
  # remove the thinking tags from the response
132
  cleaned = re.sub(r'<thinking>.*?</thinking>', '', content, flags=re.DOTALL)
133
 
134
+ # Quote-wrap each node label and escape any leaked code characters
135
+ cleaned = quote_labels(cleaned)
136
+
 
 
137
  return cleaned.strip() # and remove excess whitespace
138
 
139
  # ----- Custom Frontend ----- #