Spaces:
Running
on
Zero
Update app.py
Browse filesImprovements Made:
Error Handling:
Added handling for invalid URLs and failed API responses to return structured error messages.
Data Structure Integrity:
Ensured that extracted_content always contains dictionaries with a proper structure by initializing error messages as dictionaries.
Debugging Support:
Added checks in format_output to handle unexpected data formats gracefully and indicate potential data structure issues.Improvements Made:
Error Handling:
Added handling for invalid URLs and failed API responses to return structured error messages.
Data Structure Integrity:
Ensured that extracted_content always contains dictionaries with a proper structure by initializing error messages as dictionaries.
Debugging Support:
Added checks in format_output to handle unexpected data formats gracefully and indicate potential data structure issues.
@@ -1,5 +1,6 @@
|
|
1 |
import requests
|
2 |
import gradio as gr
|
|
|
3 |
|
4 |
def get_file_summary(file_info):
|
5 |
return {
|
@@ -10,7 +11,7 @@ def get_file_summary(file_info):
|
|
10 |
|
11 |
def extract_repo_content(url):
|
12 |
if "huggingface.co" not in url:
|
13 |
-
return "Invalid URL. Please provide a valid Hugging Face URL."
|
14 |
|
15 |
repo_name = url.split('/')[-2]
|
16 |
repo_type = url.split('/')[-3]
|
@@ -18,7 +19,7 @@ def extract_repo_content(url):
|
|
18 |
|
19 |
response = requests.get(api_url)
|
20 |
if response.status_code != 200:
|
21 |
-
return f"Failed to fetch repository content. Status code: {response.status_code}"
|
22 |
|
23 |
repo_content = response.json()
|
24 |
extracted_content = []
|
@@ -44,11 +45,14 @@ def extract_repo_content(url):
|
|
44 |
def format_output(extracted_content):
|
45 |
formatted_output = ""
|
46 |
for file_data in extracted_content:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
52 |
return formatted_output
|
53 |
|
54 |
def extract_and_display(url):
|
|
|
1 |
import requests
|
2 |
import gradio as gr
|
3 |
+
import json
|
4 |
|
5 |
def get_file_summary(file_info):
|
6 |
return {
|
|
|
11 |
|
12 |
def extract_repo_content(url):
|
13 |
if "huggingface.co" not in url:
|
14 |
+
return [{"header": {"name": "Error", "type": "error", "size": 0}, "content": "Invalid URL. Please provide a valid Hugging Face URL."}]
|
15 |
|
16 |
repo_name = url.split('/')[-2]
|
17 |
repo_type = url.split('/')[-3]
|
|
|
19 |
|
20 |
response = requests.get(api_url)
|
21 |
if response.status_code != 200:
|
22 |
+
return [{"header": {"name": "Error", "type": "error", "size": 0}, "content": f"Failed to fetch repository content. Status code: {response.status_code}"}]
|
23 |
|
24 |
repo_content = response.json()
|
25 |
extracted_content = []
|
|
|
45 |
def format_output(extracted_content):
|
46 |
formatted_output = ""
|
47 |
for file_data in extracted_content:
|
48 |
+
if isinstance(file_data, dict) and 'header' in file_data:
|
49 |
+
formatted_output += f"### File: {file_data['header']['name']}\n"
|
50 |
+
formatted_output += f"**Type:** {file_data['header']['type']}\n"
|
51 |
+
formatted_output += f"**Size:** {file_data['header']['size']} bytes\n"
|
52 |
+
formatted_output += "#### Content:\n"
|
53 |
+
formatted_output += f"```\n{file_data['content']}\n```\n\n"
|
54 |
+
else:
|
55 |
+
formatted_output += "Error in file data format.\n"
|
56 |
return formatted_output
|
57 |
|
58 |
def extract_and_display(url):
|