Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,11 @@ import subprocess
|
|
3 |
import gradio as gr
|
4 |
from magika import Magika
|
5 |
|
|
|
|
|
|
|
|
|
|
|
6 |
def clone_repo(url, repo_dir):
|
7 |
env = os.environ.copy()
|
8 |
env['GIT_LFS_SKIP_SMUDGE'] = '1'
|
@@ -41,13 +46,16 @@ def validate_file_types(directory):
|
|
41 |
return file_types
|
42 |
|
43 |
def extract_repo_content(url):
|
|
|
|
|
|
|
44 |
repo_dir = "./temp_repo"
|
45 |
if os.path.exists(repo_dir):
|
46 |
subprocess.run(["rm", "-rf", repo_dir])
|
47 |
|
48 |
success, error = clone_repo(url, repo_dir)
|
49 |
if not success:
|
50 |
-
return [{"header": {"name": "Error", "type": "error", "size": 0}, "content": error}]
|
51 |
|
52 |
file_types = validate_file_types(repo_dir)
|
53 |
extracted_content = []
|
@@ -55,7 +63,7 @@ def extract_repo_content(url):
|
|
55 |
file_summary = get_file_summary(file_path, file_type)
|
56 |
content = {"header": file_summary}
|
57 |
|
58 |
-
if file_type in
|
59 |
try:
|
60 |
content["content"] = read_file_content(file_path)
|
61 |
except Exception as e:
|
@@ -65,6 +73,9 @@ def extract_repo_content(url):
|
|
65 |
|
66 |
extracted_content.append(content)
|
67 |
|
|
|
|
|
|
|
68 |
return extracted_content
|
69 |
|
70 |
def format_output(extracted_content):
|
|
|
3 |
import gradio as gr
|
4 |
from magika import Magika
|
5 |
|
6 |
+
SUPPORTED_FILE_TYPES = ["txt", "python", "markdown", "yaml", "json", "csv", "tsv", "xml", "html"]
|
7 |
+
|
8 |
+
def validate_url(url):
|
9 |
+
return url.startswith('https://')
|
10 |
+
|
11 |
def clone_repo(url, repo_dir):
|
12 |
env = os.environ.copy()
|
13 |
env['GIT_LFS_SKIP_SMUDGE'] = '1'
|
|
|
46 |
return file_types
|
47 |
|
48 |
def extract_repo_content(url):
|
49 |
+
if not validate_url(url):
|
50 |
+
return [{"header": {"name": "Error", "type": "error", "size": 0}, "content": "Invalid URL"}]
|
51 |
+
|
52 |
repo_dir = "./temp_repo"
|
53 |
if os.path.exists(repo_dir):
|
54 |
subprocess.run(["rm", "-rf", repo_dir])
|
55 |
|
56 |
success, error = clone_repo(url, repo_dir)
|
57 |
if not success:
|
58 |
+
return [{"header": {"name": "Error", "type": "error", "size": 0}, "content": f"Failed to clone repository: {error}"}]
|
59 |
|
60 |
file_types = validate_file_types(repo_dir)
|
61 |
extracted_content = []
|
|
|
63 |
file_summary = get_file_summary(file_path, file_type)
|
64 |
content = {"header": file_summary}
|
65 |
|
66 |
+
if file_type in SUPPORTED_FILE_TYPES and file_summary["size"] <= 1024 * 1024:
|
67 |
try:
|
68 |
content["content"] = read_file_content(file_path)
|
69 |
except Exception as e:
|
|
|
73 |
|
74 |
extracted_content.append(content)
|
75 |
|
76 |
+
# Cleanup temporary directory
|
77 |
+
subprocess.run(["rm", "-rf", repo_dir])
|
78 |
+
|
79 |
return extracted_content
|
80 |
|
81 |
def format_output(extracted_content):
|