File size: 2,675 Bytes
4006c1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import requests
import zipfile
import io
import gradio as gr

def get_file_summary(file_info):
    return {
        "name": file_info.filename,
        "type": "binary" if file_info.file_size > 1024 * 1024 else "text",
        "size": file_info.file_size,
    }

def extract_repo_content(url):
    if "huggingface.co" not in url:
        return "Invalid URL. Please provide a valid Hugging Face URL."
    
    repo_name = url.split('/')[-2]
    repo_type = url.split('/')[-3]
    api_url = f"https://huggingface.co/api/{repo_type}/{repo_name}/tree/main"
    
    response = requests.get(api_url)
    if response.status_code != 200:
        return f"Failed to fetch repository content. Status code: {response.status_code}"
    
    repo_content = response.json()
    extracted_content = []
    headers = []
    
    for file_info in repo_content:
        file_summary = get_file_summary(file_info)
        headers.append(file_summary)
        
        if file_summary["type"] == "text" and file_summary["size"] <= 1024 * 1024:
            file_url = f"https://huggingface.co/{repo_type}/{repo_name}/resolve/main/{file_info['filename']}"
            file_response = requests.get(file_url)
            if file_response.status_code == 200:
                file_content = file_response.text
                extracted_content.append({"header": file_summary, "content": file_content})
            else:
                extracted_content.append({"header": file_summary, "content": "Failed to fetch file content."})
        else:
            extracted_content.append({"header": file_summary, "content": "File too large or binary, content not captured."})
    
    return extracted_content

def format_output(extracted_content):
    formatted_output = ""
    for file_data in extracted_content:
        formatted_output += f"### File: {file_data['header']['name']}\n"
        formatted_output += f"**Type:** {file_data['header']['type']}\n"
        formatted_output += f"**Size:** {file_data['header']['size']} bytes\n"
        formatted_output += "#### Content:\n"
        formatted_output += f"```\n{file_data['content']}\n```\n\n"
    return formatted_output

def extract_and_display(url):
    extracted_content = extract_repo_content(url)
    formatted_output = format_output(extracted_content)
    return formatted_output

app = gr.Blocks()

with app:
    gr.Markdown("# Gradio Space/Model Content Extractor")
    url_input = gr.Textbox(label="Hugging Face Space/Model URL")
    output_display = gr.Markdown()
    extract_button = gr.Button("Extract Content")
    
    extract_button.click(fn=extract_and_display, inputs=url_input, outputs=output_display)

app.launch()