Spaces:
Sleeping
Sleeping
Commit ·
dc9d402
1
Parent(s): 3868d8d
Oracle
Browse files19 unmodified lines202122232425142 unmodified lines1681691701711721731741 unmodified line1761771781791801811821831841851 unmodified line1871881891901911921931 unmodified line19519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522619 unmodified linesimport threadingimport loggingimport concurrent.futuresimport signalfrom tqdm import tqdmimport sys142 unmodified linesdef timeout_handler(signum, frame):raise TimeoutError("LLama model prediction timed out.")def assign_base_weight(df):print("[INFO] Starting base weight assignment using LLama model...", flush=True)logging.info("[INFO] Assigning base weights using LLama model...")start_time = time.time()1 unmodified linebase_weights = []llm_cache = {}for idx, row in tqdm(df.iterrows(), total=len(df), desc="Assigning weights"):repo = row.get("repo", "")print(f"[INFO] Assigning weight for repository {idx + 1}/{len(df)}: {repo}", flush=True)logging.info(f"[INFO] Processing repository {idx + 1}/{len(df)}: {repo}")parent = row.get("parent", "")stars = row.get("stars", 0)forks = row.get("forks", 0)1 unmodified lineissues = row.get("open_issues", 0)pulls = row.get("pulls", 0)activity = row.get("activity", "")prompt = (f"Repository: {repo}\n"f"GitHub Metrics: {stars} stars, {forks} forks, {watchers} watchers, {issues} open issues, {pulls} pull requests, activity: {activity}.\n"f"Parent or dependency: {parent}\n\n"1 unmodified line"that reflects how influential the repository is as a source relative to its parent. ""Only output the numeric value.")try:if repo in llm_cache:weight = llm_cache[repo]else:print(f"[INFO] Sending prompt to LLama model for repo: {repo}", flush=True)start_llama_time = time.time()response = llama.predict(prompt)# Use regex to extract the first valid float from the responsematch = re.search(r"[-+]?\d*\.\d+|\d+", response)if match:weight = float(match.group())weight = min(max(weight, 0), 1)else:raise ValueError(f"No valid float found in response: {response}")end_llama_time = time.time()print(f"[INFO] Received weight {weight} for {repo} in {end_llama_time - start_llama_time:.2f} seconds.", flush=True)logging.info(f"[INFO] Processed repository {repo} in {end_llama_time - start_llama_time:.2f} seconds. Weight: {weight}")llm_cache[repo] = weightexcept Exception as e:print(f"[ERROR] Failed to process repository {repo}: {e}", flush=True)logging.error(f"[ERROR] Failed to process repository {repo}: {e}")weight = 0.0 # Default weight in case of failure (set to 0 for no work)base_weights.append(weight)print(f"[PROGRESS] Finished {idx + 1}/{len(df)} repositories.", flush=True)df["base_weight"] = base_weightsend_time = time.time()print(f"[INFO] Base weights assigned successfully in {end_time - start_time:.2f} seconds.", flush=True)logging.info(f"[INFO] Base weights assigned successfully in {end_time - start_time:.2f} seconds.")19 unmodified lines20212223242526142 unmodified lines1691701711721731741751761771781 unmodified line1801811821831841851861871881891 unmodified line1911921931941951961971 unmodified line19920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023119 unmodified linesimport threadingimport loggingimport concurrent.futuresfrom concurrent.futures import ThreadPoolExecutorimport signalfrom tqdm import tqdmimport sys142 unmodified linesdef timeout_handler(signum, frame):raise TimeoutError("LLama model prediction timed out.")def assign_base_weight(df, max_workers=8):"""Assign base weights using LLama model in parallel."""print("[INFO] Starting base weight assignment using LLama model...", flush=True)logging.info("[INFO] Assigning base weights using LLama model...")start_time = time.time()1 unmodified linebase_weights = []llm_cache = {}# Prepare prompts for all repositoriesprompts = {}for idx, row in df.iterrows():repo = row.get("repo", "")parent = row.get("parent", "")stars = row.get("stars", 0)forks = row.get("forks", 0)1 unmodified lineissues = row.get("open_issues", 0)pulls = row.get("pulls", 0)activity = row.get("activity", "")prompts[idx] = (f"Repository: {repo}\n"f"GitHub Metrics: {stars} stars, {forks} forks, {watchers} watchers, {issues} open issues, {pulls} pull requests, activity: {activity}.\n"f"Parent or dependency: {parent}\n\n"1 unmodified line"that reflects how influential the repository is as a source relative to its parent. ""Only output the numeric value.")# Define the prediction functiondef _predict(idx, prompt):if idx in llm_cache:return idx, llm_cache[idx]try:resp = llama.predict(prompt)match = re.search(r"[-+]?\d*\.\d+|\d+", resp)weight = min(max(float(match.group()), 0), 1) if match else 0.0llm_cache[idx] = weightreturn idx, weightexcept Exception as e:print(f"[ERROR] Failed to process repository {idx}: {e}", flush=True)logging.error(f"[ERROR] Failed to process repository {idx}: {e}")return idx, 0.0 # Default weight in case of failure# Run predictions in parallelwith ThreadPoolExecutor(max_workers=max_workers) as executor:futures = [executor.submit(_predict, idx, prompt) for idx, prompt in prompts.items()]for fut in tqdm(concurrent.futures.as_completed(futures), total=len(futures), desc="LLM Prompts"):idx, weight = fut.result()base_weights.append((idx, weight))# Sort weights by index and assign to DataFramebase_weights.sort(key=lambda x: x[0])df["base_weight"] = [weight for _, weight in base_weights]end_time = time.time()print(f"[INFO] Base weights assigned successfully in {end_time - start_time:.2f} seconds.", flush=True)logging.info(f"[INFO] Base weights assigned successfully in {end_time - start_time:.2f} seconds.")
Oracle/deepfundingoracle.py+33-28