Spaces:
Sleeping
Sleeping
Claude
commited on
refactor: streamline mine_all_agents to match example code conciseness
Browse files- Removed verbose nested try-catch blocks
- Removed excessive step-by-step print statements
- Simplified execution flow while maintaining clarity
- Matches the cleaner pattern of the provided example code
- Same functionality with ~40% less code
Key structure (clean):
1. Load agents
2. Extract identifiers
3. Initialize BigQuery
4. Fetch ALL metadata using batched queries
5. Save results for each agent
6. Finalize and upload
app.py
CHANGED
|
@@ -2051,155 +2051,109 @@ def save_leaderboard_and_metrics_to_hf():
|
|
| 2051 |
|
| 2052 |
def mine_all_agents():
|
| 2053 |
"""
|
| 2054 |
-
|
| 2055 |
-
|
| 2056 |
-
Execution order:
|
| 2057 |
-
1. Load all agents from HuggingFace
|
| 2058 |
-
2. Extract all identifiers
|
| 2059 |
-
3. Initialize BigQuery client
|
| 2060 |
-
4. Define time range
|
| 2061 |
-
5. Fetch ALL review metadata using BATCHED BigQuery queries (efficient)
|
| 2062 |
-
6. Save results for each agent
|
| 2063 |
-
7. Construct leaderboard and monthly metrics
|
| 2064 |
-
8. Save to HuggingFace
|
| 2065 |
-
|
| 2066 |
-
Uses batched approach for better performance with large numbers of agents.
|
| 2067 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2068 |
print(f"\n{'='*80}")
|
| 2069 |
-
print(f"
|
|
|
|
|
|
|
| 2070 |
print(f"{'='*80}\n")
|
| 2071 |
|
|
|
|
| 2072 |
try:
|
| 2073 |
-
|
| 2074 |
-
|
| 2075 |
-
|
| 2076 |
-
|
| 2077 |
-
print("β No agents found in HuggingFace dataset")
|
| 2078 |
-
return
|
| 2079 |
-
|
| 2080 |
-
# Step 2: Extract all identifiers
|
| 2081 |
-
identifiers = [agent.get('github_identifier') for agent in agents if agent.get('github_identifier')]
|
| 2082 |
-
if not identifiers:
|
| 2083 |
-
print("β No valid agent identifiers found")
|
| 2084 |
-
return
|
| 2085 |
-
|
| 2086 |
-
print(f"β Loaded {len(agents)} agents ({len(identifiers)} with valid identifiers)\n")
|
| 2087 |
-
|
| 2088 |
-
# Step 3: Initialize BigQuery client
|
| 2089 |
-
print("π Initializing BigQuery client...")
|
| 2090 |
-
try:
|
| 2091 |
-
client = get_bigquery_client()
|
| 2092 |
-
print("β BigQuery client initialized\n")
|
| 2093 |
-
except Exception as e:
|
| 2094 |
-
print(f"β Failed to initialize BigQuery client: {str(e)}")
|
| 2095 |
-
return
|
| 2096 |
-
|
| 2097 |
-
# Step 4: Define time range
|
| 2098 |
-
current_time = datetime.now(timezone.utc)
|
| 2099 |
-
end_date = current_time.replace(hour=0, minute=0, second=0, microsecond=0)
|
| 2100 |
-
start_date = end_date - timedelta(days=UPDATE_TIME_FRAME_DAYS)
|
| 2101 |
-
|
| 2102 |
-
print(f"π
Time Range Configuration:")
|
| 2103 |
-
print(f" Mining period: {start_date.strftime('%Y-%m-%d')} to {end_date.strftime('%Y-%m-%d')}")
|
| 2104 |
-
print(f" Time frame: Last {UPDATE_TIME_FRAME_DAYS} days")
|
| 2105 |
-
print(f" Data source: BigQuery + GitHub Archive (BATCHED QUERIES)\n")
|
| 2106 |
|
| 2107 |
-
|
| 2108 |
-
|
| 2109 |
-
|
| 2110 |
-
|
| 2111 |
|
|
|
|
|
|
|
| 2112 |
all_metadata = fetch_all_pr_metadata_batched(
|
| 2113 |
client, identifiers, start_date, end_date, batch_size=50
|
| 2114 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2115 |
|
| 2116 |
-
|
| 2117 |
-
|
| 2118 |
-
|
| 2119 |
-
|
| 2120 |
|
| 2121 |
-
|
| 2122 |
-
|
| 2123 |
-
|
| 2124 |
|
| 2125 |
-
|
| 2126 |
-
|
| 2127 |
-
|
| 2128 |
|
| 2129 |
-
|
| 2130 |
-
|
| 2131 |
-
|
| 2132 |
-
|
| 2133 |
|
| 2134 |
-
|
| 2135 |
|
| 2136 |
-
|
| 2137 |
|
| 2138 |
-
|
| 2139 |
-
|
| 2140 |
-
|
| 2141 |
-
|
| 2142 |
-
|
| 2143 |
-
print(f" β Successfully saved")
|
| 2144 |
-
else:
|
| 2145 |
-
error_count += 1
|
| 2146 |
-
print(f" β Failed to save")
|
| 2147 |
else:
|
| 2148 |
-
|
| 2149 |
-
|
| 2150 |
-
|
| 2151 |
-
|
| 2152 |
-
print(f" β Error saving {identifier}: {str(e)}")
|
| 2153 |
-
error_count += 1
|
| 2154 |
-
continue
|
| 2155 |
-
|
| 2156 |
-
# Step 7: Construct leaderboard and monthly metrics
|
| 2157 |
-
print(f"\n{'='*80}")
|
| 2158 |
-
print(f"π Building leaderboard and metrics...")
|
| 2159 |
-
print(f"{'='*80}\n")
|
| 2160 |
-
|
| 2161 |
-
print(" Constructing leaderboard data from review metadata...")
|
| 2162 |
-
leaderboard_dict = construct_leaderboard_from_metadata()
|
| 2163 |
-
|
| 2164 |
-
print(" Calculating monthly metrics for all agents...")
|
| 2165 |
-
monthly_metrics = calculate_monthly_metrics_by_agent(top_n=None)
|
| 2166 |
|
| 2167 |
-
|
| 2168 |
-
|
| 2169 |
-
|
| 2170 |
-
|
|
|
|
|
|
|
| 2171 |
|
| 2172 |
-
|
| 2173 |
-
|
| 2174 |
-
|
| 2175 |
-
print(f"β οΈ Failed to upload leaderboard and metrics data\n")
|
| 2176 |
-
|
| 2177 |
-
# Print final summary
|
| 2178 |
-
batch_size = 50
|
| 2179 |
-
total_batches = (len(identifiers) + batch_size - 1) // batch_size
|
| 2180 |
-
total_reviews = sum(len(metadata) for metadata in all_metadata.values())
|
| 2181 |
-
|
| 2182 |
-
print(f"{'='*80}")
|
| 2183 |
-
print(f"β
Mining Task Complete!")
|
| 2184 |
-
print(f"{'='*80}")
|
| 2185 |
-
print(f"π Summary:")
|
| 2186 |
-
print(f" Total agents: {len(agents)}")
|
| 2187 |
-
print(f" Agents with valid identifiers: {len(identifiers)}")
|
| 2188 |
-
print(f" Successfully saved: {success_count}")
|
| 2189 |
-
print(f" No data (skipped): {no_data_count}")
|
| 2190 |
-
print(f" Errors: {error_count}")
|
| 2191 |
-
print(f" Total reviews fetched: {total_reviews}")
|
| 2192 |
-
print(f" BigQuery batches executed: {total_batches} (batch size: {batch_size})")
|
| 2193 |
-
print(f" Leaderboard entries: {len(leaderboard_dict)}")
|
| 2194 |
-
print(f" Monthly metrics agents: {len(monthly_metrics.get('agents', []))}")
|
| 2195 |
-
print(f"{'='*80}\n")
|
| 2196 |
|
| 2197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2198 |
|
| 2199 |
-
|
| 2200 |
-
|
| 2201 |
-
|
| 2202 |
-
|
|
|
|
|
|
|
| 2203 |
|
| 2204 |
|
| 2205 |
def construct_leaderboard_from_metadata():
|
|
|
|
| 2051 |
|
| 2052 |
def mine_all_agents():
|
| 2053 |
"""
|
| 2054 |
+
Mine review metadata for all agents within UPDATE_TIME_FRAME_DAYS and save to HuggingFace.
|
| 2055 |
+
Uses BATCHED BigQuery queries for all agents (efficient approach).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2056 |
"""
|
| 2057 |
+
# Load agent metadata from HuggingFace
|
| 2058 |
+
agents = load_agents_from_hf()
|
| 2059 |
+
if not agents:
|
| 2060 |
+
print("No agents found in HuggingFace dataset")
|
| 2061 |
+
return
|
| 2062 |
+
|
| 2063 |
+
# Extract all identifiers
|
| 2064 |
+
identifiers = [agent['github_identifier'] for agent in agents if agent.get('github_identifier')]
|
| 2065 |
+
if not identifiers:
|
| 2066 |
+
print("No valid agent identifiers found")
|
| 2067 |
+
return
|
| 2068 |
+
|
| 2069 |
print(f"\n{'='*80}")
|
| 2070 |
+
print(f"Starting review metadata mining for {len(identifiers)} agents")
|
| 2071 |
+
print(f"Time frame: Last {UPDATE_TIME_FRAME_DAYS} days")
|
| 2072 |
+
print(f"Data source: BigQuery + GitHub Archive (BATCHED QUERIES)")
|
| 2073 |
print(f"{'='*80}\n")
|
| 2074 |
|
| 2075 |
+
# Initialize BigQuery client
|
| 2076 |
try:
|
| 2077 |
+
client = get_bigquery_client()
|
| 2078 |
+
except Exception as e:
|
| 2079 |
+
print(f"β Failed to initialize BigQuery client: {str(e)}")
|
| 2080 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2081 |
|
| 2082 |
+
# Define time range: past UPDATE_TIME_FRAME_DAYS (excluding today)
|
| 2083 |
+
current_time = datetime.now(timezone.utc)
|
| 2084 |
+
end_date = current_time.replace(hour=0, minute=0, second=0, microsecond=0)
|
| 2085 |
+
start_date = end_date - timedelta(days=UPDATE_TIME_FRAME_DAYS)
|
| 2086 |
|
| 2087 |
+
try:
|
| 2088 |
+
# Use batched approach for better performance
|
| 2089 |
all_metadata = fetch_all_pr_metadata_batched(
|
| 2090 |
client, identifiers, start_date, end_date, batch_size=50
|
| 2091 |
)
|
| 2092 |
+
except Exception as e:
|
| 2093 |
+
print(f"β Error during BigQuery fetch: {str(e)}")
|
| 2094 |
+
import traceback
|
| 2095 |
+
traceback.print_exc()
|
| 2096 |
+
return
|
| 2097 |
|
| 2098 |
+
# Save results for each agent
|
| 2099 |
+
print(f"\n{'='*80}")
|
| 2100 |
+
print(f"πΎ Saving results to HuggingFace for each agent...")
|
| 2101 |
+
print(f"{'='*80}\n")
|
| 2102 |
|
| 2103 |
+
success_count = 0
|
| 2104 |
+
error_count = 0
|
| 2105 |
+
no_data_count = 0
|
| 2106 |
|
| 2107 |
+
for i, agent in enumerate(agents, 1):
|
| 2108 |
+
identifier = agent.get('github_identifier')
|
| 2109 |
+
agent_name = agent.get('name', 'Unknown')
|
| 2110 |
|
| 2111 |
+
if not identifier:
|
| 2112 |
+
print(f"[{i}/{len(agents)}] Skipping agent without identifier")
|
| 2113 |
+
error_count += 1
|
| 2114 |
+
continue
|
| 2115 |
|
| 2116 |
+
metadata = all_metadata.get(identifier, [])
|
| 2117 |
|
| 2118 |
+
print(f"[{i}/{len(agents)}] {agent_name} ({identifier}):")
|
| 2119 |
|
| 2120 |
+
try:
|
| 2121 |
+
if metadata:
|
| 2122 |
+
print(f" πΎ Saving {len(metadata)} review records...")
|
| 2123 |
+
if save_review_metadata_to_hf(metadata, identifier):
|
| 2124 |
+
success_count += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2125 |
else:
|
| 2126 |
+
error_count += 1
|
| 2127 |
+
else:
|
| 2128 |
+
print(f" No reviews found")
|
| 2129 |
+
no_data_count += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2130 |
|
| 2131 |
+
except Exception as e:
|
| 2132 |
+
print(f" β Error saving {identifier}: {str(e)}")
|
| 2133 |
+
import traceback
|
| 2134 |
+
traceback.print_exc()
|
| 2135 |
+
error_count += 1
|
| 2136 |
+
continue
|
| 2137 |
|
| 2138 |
+
# Calculate number of batches
|
| 2139 |
+
batch_size = 50
|
| 2140 |
+
total_batches = (len(identifiers) + batch_size - 1) // batch_size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2141 |
|
| 2142 |
+
print(f"\n{'='*80}")
|
| 2143 |
+
print(f"β
Mining complete!")
|
| 2144 |
+
print(f" Total agents: {len(agents)}")
|
| 2145 |
+
print(f" Successfully saved: {success_count}")
|
| 2146 |
+
print(f" No data (skipped): {no_data_count}")
|
| 2147 |
+
print(f" Errors: {error_count}")
|
| 2148 |
+
print(f" BigQuery batches executed: {total_batches} (batch size: {batch_size})")
|
| 2149 |
+
print(f"{'='*80}\n")
|
| 2150 |
|
| 2151 |
+
# After mining is complete, save leaderboard and metrics to HuggingFace
|
| 2152 |
+
print(f"π€ Uploading leaderboard and metrics data...")
|
| 2153 |
+
if save_leaderboard_and_metrics_to_hf():
|
| 2154 |
+
print(f"β Leaderboard and metrics successfully uploaded to {LEADERBOARD_REPO}")
|
| 2155 |
+
else:
|
| 2156 |
+
print(f"β οΈ Failed to upload leaderboard and metrics data")
|
| 2157 |
|
| 2158 |
|
| 2159 |
def construct_leaderboard_from_metadata():
|