Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,7 +50,6 @@ def aggregate_datasets(api_key, entries):
|
|
| 50 |
imgs, cls_map = fetch_metadata(api_key, ws, proj, ver)
|
| 51 |
total_images += imgs
|
| 52 |
for cls, cnt in cls_map.items():
|
| 53 |
-
# normalize class names to lowercase
|
| 54 |
norm = cls.strip().lower()
|
| 55 |
class_counts[norm] = class_counts.get(norm, 0) + cnt
|
| 56 |
class_sources.setdefault(norm, set()).add(url)
|
|
@@ -86,7 +85,7 @@ def load_datasets(api_key, file_objs):
|
|
| 86 |
raw = None
|
| 87 |
try:
|
| 88 |
raw = fobj.read()
|
| 89 |
-
except:
|
| 90 |
raw = fobj.get('data') if isinstance(fobj, dict) else None
|
| 91 |
if raw is None and isinstance(fobj, str):
|
| 92 |
with open(fobj, 'rb') as f:
|
|
@@ -118,7 +117,7 @@ def load_datasets(api_key, file_objs):
|
|
| 118 |
def update_classes(df_data):
|
| 119 |
"""
|
| 120 |
Combine edited classes (merge duplicates, lowercase) and recalc.
|
| 121 |
-
Returns: total_images, plot_fig, json_counts, markdown_summary.
|
| 122 |
"""
|
| 123 |
combined = {}
|
| 124 |
for row in df_data:
|
|
@@ -127,14 +126,17 @@ def update_classes(df_data):
|
|
| 127 |
name = row[0].strip().lower()
|
| 128 |
try:
|
| 129 |
cnt = int(row[1])
|
| 130 |
-
except:
|
| 131 |
-
|
| 132 |
combined[name] = combined.get(name, 0) + cnt
|
| 133 |
|
| 134 |
total = sum(combined.values())
|
|
|
|
|
|
|
|
|
|
| 135 |
fig = make_bar_chart(combined)
|
| 136 |
-
|
| 137 |
-
return str(total), fig, json.dumps(combined, indent=2),
|
| 138 |
|
| 139 |
|
| 140 |
def build_ui():
|
|
@@ -145,21 +147,24 @@ def build_ui():
|
|
| 145 |
api_input = gr.Textbox(label="Roboflow API Key", type="password")
|
| 146 |
files = gr.Files(label="Upload .txt files of Roboflow URLs", file_types=[".txt"])
|
| 147 |
|
| 148 |
-
|
| 149 |
total_out = gr.Textbox(label="Total Images", interactive=False)
|
| 150 |
-
df = gr.Dataframe(headers=["Class Name", "Count"], interactive=True)
|
| 151 |
plot = gr.Plot()
|
| 152 |
json_out = gr.Textbox(label="Counts (JSON)", interactive=False)
|
| 153 |
md_out = gr.Markdown(label="Class Sources")
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
inputs=[df],
|
| 162 |
-
outputs=[total_out, plot, json_out, md_out])
|
| 163 |
|
| 164 |
return demo
|
| 165 |
|
|
|
|
| 50 |
imgs, cls_map = fetch_metadata(api_key, ws, proj, ver)
|
| 51 |
total_images += imgs
|
| 52 |
for cls, cnt in cls_map.items():
|
|
|
|
| 53 |
norm = cls.strip().lower()
|
| 54 |
class_counts[norm] = class_counts.get(norm, 0) + cnt
|
| 55 |
class_sources.setdefault(norm, set()).add(url)
|
|
|
|
| 85 |
raw = None
|
| 86 |
try:
|
| 87 |
raw = fobj.read()
|
| 88 |
+
except Exception:
|
| 89 |
raw = fobj.get('data') if isinstance(fobj, dict) else None
|
| 90 |
if raw is None and isinstance(fobj, str):
|
| 91 |
with open(fobj, 'rb') as f:
|
|
|
|
| 117 |
def update_classes(df_data):
|
| 118 |
"""
|
| 119 |
Combine edited classes (merge duplicates, lowercase) and recalc.
|
| 120 |
+
Returns: total_images, updated_dataframe, plot_fig, json_counts, markdown_summary.
|
| 121 |
"""
|
| 122 |
combined = {}
|
| 123 |
for row in df_data:
|
|
|
|
| 126 |
name = row[0].strip().lower()
|
| 127 |
try:
|
| 128 |
cnt = int(row[1])
|
| 129 |
+
except Exception:
|
| 130 |
+
cnt = 0
|
| 131 |
combined[name] = combined.get(name, 0) + cnt
|
| 132 |
|
| 133 |
total = sum(combined.values())
|
| 134 |
+
# Build updated DataFrame
|
| 135 |
+
updated_df = [[cls, combined[cls]] for cls in combined]
|
| 136 |
+
|
| 137 |
fig = make_bar_chart(combined)
|
| 138 |
+
md_summary = "\n".join(f"- **{cls}** ({combined[cls]} images)" for cls in combined)
|
| 139 |
+
return str(total), updated_df, fig, json.dumps(combined, indent=2), md_summary
|
| 140 |
|
| 141 |
|
| 142 |
def build_ui():
|
|
|
|
| 147 |
api_input = gr.Textbox(label="Roboflow API Key", type="password")
|
| 148 |
files = gr.Files(label="Upload .txt files of Roboflow URLs", file_types=[".txt"])
|
| 149 |
|
| 150 |
+
load_btn = gr.Button("Load Datasets")
|
| 151 |
total_out = gr.Textbox(label="Total Images", interactive=False)
|
| 152 |
+
df = gr.Dataframe(headers=["Class Name", "Count"], row_count=(1, None), col_count=2, interactive=True)
|
| 153 |
plot = gr.Plot()
|
| 154 |
json_out = gr.Textbox(label="Counts (JSON)", interactive=False)
|
| 155 |
md_out = gr.Markdown(label="Class Sources")
|
| 156 |
|
| 157 |
+
update_btn = gr.Button("Apply Class Edits")
|
| 158 |
+
|
| 159 |
+
# Load datasets
|
| 160 |
+
load_btn.click(fn=load_datasets,
|
| 161 |
+
inputs=[api_input, files],
|
| 162 |
+
outputs=[total_out, df, plot, json_out, md_out])
|
| 163 |
|
| 164 |
+
# Apply edits and refresh all outputs (including table)
|
| 165 |
+
update_btn.click(fn=update_classes,
|
| 166 |
+
inputs=[df],
|
| 167 |
+
outputs=[total_out, df, plot, json_out, md_out])
|
|
|
|
|
|
|
| 168 |
|
| 169 |
return demo
|
| 170 |
|