Spaces:
Sleeping
Sleeping
Update app_logic.py
Browse files- app_logic.py +40 -49
app_logic.py
CHANGED
@@ -62,8 +62,6 @@ def load_token_from_image_and_set_env(image_pil_object: Image.Image, password: s
|
|
62 |
|
63 |
|
64 |
|
65 |
-
import re
|
66 |
-
|
67 |
def process_commented_markdown(commented_input):
|
68 |
"""Process a commented markdown string by stripping '# ' from lines where present if '# # Space:' is detected."""
|
69 |
# Check for '# # Space:' or '# Space:' to identify a space definition
|
@@ -78,7 +76,6 @@ def process_commented_markdown(commented_input):
|
|
78 |
# Detect code block boundaries (handle '# ```' or '```')
|
79 |
if line.strip() in ("# ```", "```"):
|
80 |
in_code_block = not in_code_block
|
81 |
-
# Preserve backticks as-is (strip '# ' if present)
|
82 |
cleaned_line = line.lstrip("# ").rstrip()
|
83 |
cleaned_lines.append(cleaned_line)
|
84 |
continue
|
@@ -94,36 +91,34 @@ def process_commented_markdown(commented_input):
|
|
94 |
|
95 |
return "\n".join(cleaned_lines)
|
96 |
|
|
|
|
|
97 |
def parse_markdown(markdown_input):
|
98 |
"""Parse markdown input to extract space details and file structure."""
|
99 |
-
# Preprocess to
|
100 |
cleaned_markdown = process_commented_markdown(markdown_input)
|
101 |
-
space_info = {"repo_name": "", "owner": "", "files": []}
|
102 |
current_file = None
|
103 |
file_content = []
|
104 |
in_file_content = False
|
105 |
in_code_block = False
|
106 |
-
language = None
|
107 |
|
108 |
lines = cleaned_markdown.strip().split("\n")
|
109 |
-
|
110 |
-
|
111 |
-
line = lines[i].strip()
|
112 |
|
113 |
# Extract space name
|
114 |
if line.startswith("# Space:"):
|
115 |
space_info["repo_name"] = line.replace("# Space:", "").strip()
|
116 |
if "/" in space_info["repo_name"]:
|
117 |
space_info["owner"], space_info["repo_name"] = space_info["repo_name"].split("/", 1)
|
118 |
-
i += 1
|
119 |
continue
|
120 |
|
121 |
# Skip file structure header
|
122 |
if line.startswith("## File Structure"):
|
123 |
-
i += 1
|
124 |
continue
|
125 |
|
126 |
-
#
|
127 |
if line.startswith("π") or line.startswith("π"):
|
128 |
if current_file and file_content and in_file_content:
|
129 |
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
@@ -131,11 +126,9 @@ def parse_markdown(markdown_input):
|
|
131 |
current_file = line[2:].strip()
|
132 |
in_file_content = False
|
133 |
in_code_block = False
|
134 |
-
language = None
|
135 |
-
i += 1
|
136 |
continue
|
137 |
|
138 |
-
#
|
139 |
if line.startswith("### File:"):
|
140 |
if current_file and file_content and in_file_content:
|
141 |
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
@@ -143,42 +136,40 @@ def parse_markdown(markdown_input):
|
|
143 |
current_file = line.replace("### File:", "").strip()
|
144 |
in_file_content = True
|
145 |
in_code_block = False
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
165 |
else:
|
166 |
# Handle non-code-block content (e.g., binary file)
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
or
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
else:
|
179 |
-
i += 1
|
180 |
-
|
181 |
-
# Save the last file if present
|
182 |
if current_file and file_content and in_file_content:
|
183 |
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
184 |
|
|
|
62 |
|
63 |
|
64 |
|
|
|
|
|
65 |
def process_commented_markdown(commented_input):
|
66 |
"""Process a commented markdown string by stripping '# ' from lines where present if '# # Space:' is detected."""
|
67 |
# Check for '# # Space:' or '# Space:' to identify a space definition
|
|
|
76 |
# Detect code block boundaries (handle '# ```' or '```')
|
77 |
if line.strip() in ("# ```", "```"):
|
78 |
in_code_block = not in_code_block
|
|
|
79 |
cleaned_line = line.lstrip("# ").rstrip()
|
80 |
cleaned_lines.append(cleaned_line)
|
81 |
continue
|
|
|
91 |
|
92 |
return "\n".join(cleaned_lines)
|
93 |
|
94 |
+
|
95 |
+
|
96 |
def parse_markdown(markdown_input):
|
97 |
"""Parse markdown input to extract space details and file structure."""
|
98 |
+
# Preprocess to handle commented markdown
|
99 |
cleaned_markdown = process_commented_markdown(markdown_input)
|
100 |
+
space_info = {"repo_name": "", "owner": "", "sdk": "gradio", "files": []}
|
101 |
current_file = None
|
102 |
file_content = []
|
103 |
in_file_content = False
|
104 |
in_code_block = False
|
|
|
105 |
|
106 |
lines = cleaned_markdown.strip().split("\n")
|
107 |
+
for line in lines:
|
108 |
+
line = line.strip()
|
|
|
109 |
|
110 |
# Extract space name
|
111 |
if line.startswith("# Space:"):
|
112 |
space_info["repo_name"] = line.replace("# Space:", "").strip()
|
113 |
if "/" in space_info["repo_name"]:
|
114 |
space_info["owner"], space_info["repo_name"] = space_info["repo_name"].split("/", 1)
|
|
|
115 |
continue
|
116 |
|
117 |
# Skip file structure header
|
118 |
if line.startswith("## File Structure"):
|
|
|
119 |
continue
|
120 |
|
121 |
+
# Detect file in structure
|
122 |
if line.startswith("π") or line.startswith("π"):
|
123 |
if current_file and file_content and in_file_content:
|
124 |
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
|
|
126 |
current_file = line[2:].strip()
|
127 |
in_file_content = False
|
128 |
in_code_block = False
|
|
|
|
|
129 |
continue
|
130 |
|
131 |
+
# Detect file content section
|
132 |
if line.startswith("### File:"):
|
133 |
if current_file and file_content and in_file_content:
|
134 |
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
|
|
136 |
current_file = line.replace("### File:", "").strip()
|
137 |
in_file_content = True
|
138 |
in_code_block = False
|
139 |
+
continue
|
140 |
+
|
141 |
+
# Handle code block boundaries
|
142 |
+
if in_file_content and line == "```":
|
143 |
+
in_code_block = not in_code_block
|
144 |
+
if in_code_block:
|
145 |
+
file_content = [] # Start fresh for code block content
|
146 |
+
else:
|
147 |
+
# End of code block, save content if any
|
148 |
+
if current_file and file_content:
|
149 |
+
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
150 |
+
file_content = []
|
151 |
+
in_file_content = False
|
152 |
+
continue
|
153 |
+
|
154 |
+
# Collect content
|
155 |
+
if in_file_content:
|
156 |
+
if in_code_block:
|
157 |
+
# Skip language identifier (first non-empty line after opening backtick)
|
158 |
+
if not file_content and line and not line.startswith("```"):
|
159 |
+
continue # Skip language line (e.g., `md`, `py`)
|
160 |
+
file_content.append(line)
|
161 |
else:
|
162 |
# Handle non-code-block content (e.g., binary file)
|
163 |
+
if line:
|
164 |
+
file_content.append(line)
|
165 |
+
else:
|
166 |
+
# End non-code-block content at empty line or next section
|
167 |
+
if current_file and file_content:
|
168 |
+
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
169 |
+
file_content = []
|
170 |
+
in_file_content = False
|
171 |
+
|
172 |
+
# Append the last file
|
|
|
|
|
|
|
|
|
|
|
173 |
if current_file and file_content and in_file_content:
|
174 |
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
175 |
|