Update app.py
Browse files
app.py
CHANGED
@@ -25,7 +25,6 @@ def search_hub(query: str, search_type: str) -> pd.DataFrame:
|
|
25 |
else:
|
26 |
data = []
|
27 |
|
28 |
-
# Add numbering and format the link
|
29 |
for i, item in enumerate(data, 1):
|
30 |
item['number'] = i
|
31 |
item['formatted_link'] = format_link(item, i, search_type)
|
@@ -64,38 +63,38 @@ async def download_readme(session: aiohttp.ClientSession, item: Dict) -> tuple[s
|
|
64 |
except Exception as e:
|
65 |
return item_id.replace('/', '_'), f"# Error downloading README for {item_id}\nError: {str(e)}"
|
66 |
|
67 |
-
async def download_all_readmes(data: List[Dict]) -> str:
|
68 |
"""Download all README files and create a zip archive."""
|
|
|
|
|
|
|
69 |
zip_buffer = io.BytesIO()
|
|
|
70 |
|
71 |
async with aiohttp.ClientSession() as session:
|
72 |
-
# Download all READMEs concurrently
|
73 |
tasks = [download_readme(session, item) for item in data]
|
74 |
results = await asyncio.gather(*tasks)
|
75 |
|
76 |
-
# Create zip file
|
77 |
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
78 |
for filename, content in results:
|
79 |
zip_file.writestr(f"{filename}.md", content)
|
80 |
|
81 |
-
# Convert to base64
|
82 |
zip_buffer.seek(0)
|
83 |
base64_zip = base64.b64encode(zip_buffer.getvalue()).decode()
|
84 |
-
|
85 |
-
|
86 |
-
def create_download_link(base64_zip: str) -> str:
|
87 |
-
"""Create an HTML download link for the zip file."""
|
88 |
download_link = f"""
|
89 |
-
<
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
97 |
"""
|
98 |
-
|
|
|
99 |
|
100 |
def display_results(df: pd.DataFrame):
|
101 |
if df is not None and not df.empty:
|
@@ -138,51 +137,52 @@ with gr.Blocks() as demo:
|
|
138 |
search_button = gr.Button("Search")
|
139 |
|
140 |
results_html = gr.HTML(label="Search Results")
|
141 |
-
|
|
|
|
|
142 |
metadata_output = gr.Textbox(label="Metadata", lines=10)
|
143 |
aggregated_output = gr.JSON(label="Aggregated Content")
|
144 |
|
145 |
-
current_results = gr.State([])
|
146 |
|
147 |
-
|
148 |
df = search_hub(query, search_type)
|
149 |
data = df.to_dict('records')
|
150 |
aggregated = SwarmyTime(data)
|
151 |
html_results = display_results(df)
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
cursor: pointer;">
|
162 |
-
📚 Download All READMEs
|
163 |
-
</button>
|
164 |
-
"""
|
165 |
-
|
166 |
-
return html_results, download_button, aggregated, data
|
167 |
|
168 |
-
async def
|
169 |
if not data:
|
170 |
-
return "No results to download"
|
171 |
-
|
172 |
-
|
173 |
-
return create_download_link(base64_zip)
|
174 |
|
175 |
search_button.click(
|
176 |
search_and_aggregate,
|
177 |
inputs=[search_query, search_type],
|
178 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
)
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
download_readmes,
|
184 |
inputs=[current_results],
|
185 |
-
outputs=[
|
186 |
)
|
187 |
|
188 |
demo.launch(debug=True)
|
|
|
25 |
else:
|
26 |
data = []
|
27 |
|
|
|
28 |
for i, item in enumerate(data, 1):
|
29 |
item['number'] = i
|
30 |
item['formatted_link'] = format_link(item, i, search_type)
|
|
|
63 |
except Exception as e:
|
64 |
return item_id.replace('/', '_'), f"# Error downloading README for {item_id}\nError: {str(e)}"
|
65 |
|
66 |
+
async def download_all_readmes(data: List[Dict]) -> tuple[str, str]:
|
67 |
"""Download all README files and create a zip archive."""
|
68 |
+
if not data:
|
69 |
+
return "", "No results to download"
|
70 |
+
|
71 |
zip_buffer = io.BytesIO()
|
72 |
+
status_message = "Downloading READMEs..."
|
73 |
|
74 |
async with aiohttp.ClientSession() as session:
|
|
|
75 |
tasks = [download_readme(session, item) for item in data]
|
76 |
results = await asyncio.gather(*tasks)
|
77 |
|
|
|
78 |
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
79 |
for filename, content in results:
|
80 |
zip_file.writestr(f"{filename}.md", content)
|
81 |
|
|
|
82 |
zip_buffer.seek(0)
|
83 |
base64_zip = base64.b64encode(zip_buffer.getvalue()).decode()
|
84 |
+
|
|
|
|
|
|
|
85 |
download_link = f"""
|
86 |
+
<div style="margin-top: 10px;">
|
87 |
+
<a href="data:application/zip;base64,{base64_zip}"
|
88 |
+
download="readmes.zip"
|
89 |
+
style="display: inline-block; padding: 10px 20px;
|
90 |
+
background-color: #4CAF50; color: white;
|
91 |
+
text-decoration: none; border-radius: 5px;">
|
92 |
+
📥 Download READMEs Archive
|
93 |
+
</a>
|
94 |
+
</div>
|
95 |
"""
|
96 |
+
|
97 |
+
return download_link, "READMEs ready for download!"
|
98 |
|
99 |
def display_results(df: pd.DataFrame):
|
100 |
if df is not None and not df.empty:
|
|
|
137 |
search_button = gr.Button("Search")
|
138 |
|
139 |
results_html = gr.HTML(label="Search Results")
|
140 |
+
download_button = gr.Button("📚 Download All READMEs", visible=False)
|
141 |
+
download_status = gr.Markdown("", label="Download Status")
|
142 |
+
download_area = gr.HTML("", label="Download Link")
|
143 |
metadata_output = gr.Textbox(label="Metadata", lines=10)
|
144 |
aggregated_output = gr.JSON(label="Aggregated Content")
|
145 |
|
146 |
+
current_results = gr.State([])
|
147 |
|
148 |
+
def search_and_aggregate(query, search_type):
|
149 |
df = search_hub(query, search_type)
|
150 |
data = df.to_dict('records')
|
151 |
aggregated = SwarmyTime(data)
|
152 |
html_results = display_results(df)
|
153 |
+
show_download = len(data) > 0
|
154 |
+
return [
|
155 |
+
html_results, # results_html
|
156 |
+
show_download, # download_button visible
|
157 |
+
"", # download_status
|
158 |
+
"", # download_area
|
159 |
+
aggregated, # aggregated_output
|
160 |
+
data # current_results
|
161 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
+
async def handle_download(data):
|
164 |
if not data:
|
165 |
+
return ["No results to download", ""]
|
166 |
+
download_link, status = await download_all_readmes(data)
|
167 |
+
return [status, download_link]
|
|
|
168 |
|
169 |
search_button.click(
|
170 |
search_and_aggregate,
|
171 |
inputs=[search_query, search_type],
|
172 |
+
outputs=[
|
173 |
+
results_html,
|
174 |
+
download_button,
|
175 |
+
download_status,
|
176 |
+
download_area,
|
177 |
+
aggregated_output,
|
178 |
+
current_results
|
179 |
+
]
|
180 |
)
|
181 |
|
182 |
+
download_button.click(
|
183 |
+
handle_download,
|
|
|
184 |
inputs=[current_results],
|
185 |
+
outputs=[download_status, download_area]
|
186 |
)
|
187 |
|
188 |
demo.launch(debug=True)
|