Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,20 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, jsonify, send_file
|
| 2 |
-
from .core import create_markdown_document
|
| 3 |
-
import markdown
|
| 4 |
import os
|
| 5 |
-
import pkg_resources
|
| 6 |
import sys
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
except Exception as e:
|
| 14 |
-
# Fallback for running from source (development)
|
| 15 |
-
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 16 |
-
template_path = os.path.join(current_dir, "../templates")
|
| 17 |
-
template_path = os.path.abspath(template_path)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
if not os.path.exists(template_path):
|
| 21 |
-
# Try an alternative path within the package directory
|
| 22 |
-
package_dir = os.path.dirname(os.path.abspath(__file__))
|
| 23 |
-
alternative_path = os.path.join(package_dir, "templates")
|
| 24 |
-
if os.path.exists(alternative_path):
|
| 25 |
-
template_path = alternative_path
|
| 26 |
-
else:
|
| 27 |
-
# One last attempt: check if installed but in a different structure
|
| 28 |
-
site_packages = os.path.join(os.path.dirname(sys.executable), "site-packages")
|
| 29 |
-
installed_path = os.path.join(site_packages, "repo_to_md", "templates")
|
| 30 |
-
if os.path.exists(installed_path):
|
| 31 |
-
template_path = installed_path
|
| 32 |
-
else:
|
| 33 |
-
raise FileNotFoundError(
|
| 34 |
-
f"Template directory not found at: {template_path}, {alternative_path}, or {installed_path}"
|
| 35 |
-
)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
template_path = find_template_path()
|
| 42 |
-
|
| 43 |
-
# Create Flask app with the resolved template folder
|
| 44 |
-
app = Flask(__name__, template_folder=template_path)
|
| 45 |
-
|
| 46 |
-
@app.route('/')
|
| 47 |
-
def index():
|
| 48 |
-
return render_template('index.html')
|
| 49 |
-
|
| 50 |
-
@app.route('/process', methods=['POST'])
|
| 51 |
-
def process():
|
| 52 |
-
response_data = {'markdown': '', 'html': '', 'filename': '', 'error': None}
|
| 53 |
-
|
| 54 |
-
try:
|
| 55 |
-
if 'files[]' in request.files:
|
| 56 |
-
files = request.files.getlist('files[]')
|
| 57 |
-
if not files:
|
| 58 |
-
response_data['error'] = 'No files uploaded'
|
| 59 |
-
return jsonify(response_data), 400
|
| 60 |
-
|
| 61 |
-
markdown_content = create_markdown_document(files=files)
|
| 62 |
-
response_data['markdown'] = markdown_content
|
| 63 |
-
response_data['html'] = markdown.markdown(markdown_content)
|
| 64 |
-
response_data['filename'] = "uploaded_files_summary.md"
|
| 65 |
-
else:
|
| 66 |
-
repo_url = request.json.get('repo_url', '').strip()
|
| 67 |
-
if not repo_url:
|
| 68 |
-
response_data['error'] = 'Please provide a repository URL or upload files'
|
| 69 |
-
return jsonify(response_data), 400
|
| 70 |
-
|
| 71 |
-
markdown_content = create_markdown_document(repo_url)
|
| 72 |
-
if markdown_content.startswith("Error:"):
|
| 73 |
-
response_data['error'] = markdown_content
|
| 74 |
-
return jsonify(response_data), 400
|
| 75 |
-
|
| 76 |
-
response_data['markdown'] = markdown_content
|
| 77 |
-
response_data['html'] = markdown.markdown(markdown_content)
|
| 78 |
-
owner, repo = repo_url.rstrip('/').split('/')[-2:]
|
| 79 |
-
response_data['filename'] = f"{owner}_{repo}_summary.md"
|
| 80 |
-
|
| 81 |
-
except Exception as e:
|
| 82 |
-
response_data['error'] = f"Server error processing request: {str(e)}"
|
| 83 |
-
return jsonify(response_data), 500
|
| 84 |
-
|
| 85 |
-
return jsonify(response_data)
|
| 86 |
-
|
| 87 |
-
@app.route('/download', methods=['POST'])
|
| 88 |
-
def download():
|
| 89 |
-
markdown_content = request.json.get('markdown', '')
|
| 90 |
-
filename = request.json.get('filename', 'document.md')
|
| 91 |
-
|
| 92 |
-
buffer = io.BytesIO()
|
| 93 |
-
buffer.write(markdown_content.encode('utf-8'))
|
| 94 |
-
buffer.seek(0)
|
| 95 |
-
|
| 96 |
-
return send_file(
|
| 97 |
-
buffer,
|
| 98 |
-
as_attachment=True,
|
| 99 |
-
download_name=filename,
|
| 100 |
-
mimetype='text/markdown'
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
app.run(host=host, port=port, debug=debug)
|
| 104 |
-
|
| 105 |
-
if __name__ == '__main__':
|
| 106 |
-
run_demo()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import sys
|
| 3 |
+
import subprocess
|
| 4 |
|
| 5 |
+
try:
|
| 6 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "git+https://github.com/broadfield-dev/repo_to_md.git#egg=repo_to_md[demo]"])
|
| 7 |
+
except subprocess.CalledProcessError as e:
|
| 8 |
+
print(f"Failed to install package: {e}")
|
| 9 |
+
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
from repo_to_md import create_markdown_document
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
try:
|
| 14 |
+
from repo_to_md import run_demo
|
| 15 |
+
except ImportError as e:
|
| 16 |
+
print(f"Failed to import run_demo: {e}")
|
| 17 |
+
sys.exit(1)
|
| 18 |
|
| 19 |
+
# Run the demo (default: http://localhost:7860)
|
| 20 |
+
run_demo()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|