Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,6 +55,33 @@ def parse_changelog(text):
|
|
| 55 |
components.append({'type': 'version', 'filename': parts[i].replace('##', '').strip(), 'content': parts[i+1].strip()})
|
| 56 |
return components
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
@app.route('/parse', methods=['POST'])
|
| 59 |
def parse_endpoint():
|
| 60 |
text = request.form.get('markdown_text', '')
|
|
@@ -63,7 +90,9 @@ def parse_endpoint():
|
|
| 63 |
if not text: return jsonify({'error': 'No text or file provided.'}), 400
|
| 64 |
|
| 65 |
try:
|
| 66 |
-
if "##
|
|
|
|
|
|
|
| 67 |
format_name, components = "Repo2Markdown", parse_repo2markdown(text)
|
| 68 |
elif re.search(r'^## \[\d+\.\d+\.\d+.*?\].*?$', text, flags=re.MULTILINE):
|
| 69 |
format_name, components = "Changelog", parse_changelog(text)
|
|
@@ -71,6 +100,7 @@ def parse_endpoint():
|
|
| 71 |
format_name, components = "Standard README", parse_standard_readme(text)
|
| 72 |
else:
|
| 73 |
format_name, components = "Unknown", [{'type': 'text', 'filename': 'Full Text', 'content': text}]
|
|
|
|
| 74 |
return jsonify({'format': format_name, 'components': components})
|
| 75 |
except Exception as e:
|
| 76 |
return jsonify({'error': f'Failed to parse: {e}'}), 500
|
|
|
|
| 55 |
components.append({'type': 'version', 'filename': parts[i].replace('##', '').strip(), 'content': parts[i+1].strip()})
|
| 56 |
return components
|
| 57 |
|
| 58 |
+
def parse_agent_action(text):
|
| 59 |
+
components = []
|
| 60 |
+
|
| 61 |
+
# Extract Actions
|
| 62 |
+
action_pattern = re.compile(r'^### HF_ACTION: (.*)$', re.MULTILINE)
|
| 63 |
+
for match in action_pattern.finditer(text):
|
| 64 |
+
components.append({'type': 'action', 'filename': 'Agent Command', 'content': match.group(1).strip()})
|
| 65 |
+
|
| 66 |
+
# Extract File Structure
|
| 67 |
+
structure_match = re.search(r'## File Structure\n(```[\s\S]*?```)', text)
|
| 68 |
+
if structure_match:
|
| 69 |
+
components.append({'type': 'structure', 'filename': 'Project Structure', 'content': structure_match.group(1).strip()})
|
| 70 |
+
|
| 71 |
+
# Extract Files (using the existing logic from Repo2Markdown)
|
| 72 |
+
file_pattern = re.compile(r'### File: (.*?)\n([\s\S]*?)(?=\n### File:|\n## File Structure|\n### HF_ACTION:|\Z)', re.MULTILINE)
|
| 73 |
+
for match in file_pattern.finditer(text):
|
| 74 |
+
filename = match.group(1).strip()
|
| 75 |
+
content = match.group(2).strip()
|
| 76 |
+
is_code = "```" in content
|
| 77 |
+
components.append({'type': 'file', 'filename': filename, 'content': content, 'is_code_block': is_code})
|
| 78 |
+
|
| 79 |
+
# Instructions (Everything else)
|
| 80 |
+
if not components:
|
| 81 |
+
components.append({'type': 'instruction', 'filename': 'Instructions', 'content': text})
|
| 82 |
+
|
| 83 |
+
return components
|
| 84 |
+
|
| 85 |
@app.route('/parse', methods=['POST'])
|
| 86 |
def parse_endpoint():
|
| 87 |
text = request.form.get('markdown_text', '')
|
|
|
|
| 90 |
if not text: return jsonify({'error': 'No text or file provided.'}), 400
|
| 91 |
|
| 92 |
try:
|
| 93 |
+
if "### HF_ACTION:" in text or "File and Code Formatting:" in text:
|
| 94 |
+
format_name, components = "Agent Action", parse_agent_action(text)
|
| 95 |
+
elif "## File Structure" in text and "### File:" in text:
|
| 96 |
format_name, components = "Repo2Markdown", parse_repo2markdown(text)
|
| 97 |
elif re.search(r'^## \[\d+\.\d+\.\d+.*?\].*?$', text, flags=re.MULTILINE):
|
| 98 |
format_name, components = "Changelog", parse_changelog(text)
|
|
|
|
| 100 |
format_name, components = "Standard README", parse_standard_readme(text)
|
| 101 |
else:
|
| 102 |
format_name, components = "Unknown", [{'type': 'text', 'filename': 'Full Text', 'content': text}]
|
| 103 |
+
|
| 104 |
return jsonify({'format': format_name, 'components': components})
|
| 105 |
except Exception as e:
|
| 106 |
return jsonify({'error': f'Failed to parse: {e}'}), 500
|