serJD/speckleAggregateBranches
0
1import copy2 3import gradio as gr4from huggingface_hub import webhook_endpoint, WebhookPayload5from fastapi import Request6 7 8#import other libaries9from specklepy.api.client import SpeckleClient10from specklepy.api.credentials import get_default_account, get_local_accounts11from specklepy.transports.server import ServerTransport12from specklepy.api import operations13from specklepy.objects.geometry import Polyline, Point14 15import pandas as pd16import numpy as np17import json18import os19from utils import * 20 21speckle_token = os.environ.get("SPECKLE_TOKEN")22 23current_directory = os.path.dirname(os.path.abspath(__file__))24# Path to the config.json file25config_file_path = os.path.join(current_directory, "config.json")26with open(config_file_path, 'r') as f:27 config = json.load(f)28 29 30 31CLIENT = SpeckleClient(host="https://speckle.xyz/")32CLIENT.authenticate_with_token(token=speckle_token)33 34 35print(config.keys())36branchA = config["branchA"]37branchB = config["branchB"]38STREAM_ID = config["streamID"]39 40 41@webhook_endpoint42async def mergeStreams(request: Request):43 44 # Initialize flag45 should_continue = False46 47 # Read the request body as JSON48 payload = await request.json()49 50 print("============= payload =============")51 print(payload)52 print("============= config =============")53 print(config)54 payload = payload["payload"]55 56 57 # webhook calls can come from different sources 58 if payload.get('source') == 'notionTrigger':59 action = payload.get('action')60 streamName = payload.get('streamName')61 branchName = payload.get('branchName')62 update_source = "notionTrigger"63 64 should_continue = True65 66 67 else:68 update_source = "speckleWebhook"69 event_name = payload["event"]["event_name"]70 streamid = payload.get("stream", {}).get("id")71 # Extract branchName for commit_update events from the "old" commit data72 if event_name == "commit_update":73 branchName = payload.get("event", {}).get("data", {}).get("old", {}).get("branchName")74 else:75 branchName = payload.get("event", {}).get("data", {}).get("commit", {}).get("branchName")76 77 # List of valid event types78 valid_event_types = ["commit_create", "commit_delete", "commit_update"]79 80 if event_name in valid_event_types:81 if streamid == STREAM_ID:82 if branchName == branchA:83 should_continue = True84 else:85 print(f"Branch name {branchName} not found in config.")86 else:87 print(f"Stream name {streamid} not found in config.")88 else:89 print(f"Event type {event_name} is not one of the specified types.")90 91 # If the flag is True, continue running the main part of the code92 if should_continue:93 # get stream94 stream = getSpeckleStream(STREAM_ID,95 branchA,96 CLIENT,97 commit_id = "")98 99 # navigate to list with speckle objects of interest100 try:101 stream_data = stream["@Data"]["@{0}"]102 except:103 print("something went wrong, try again with non-capital d")104 try:105 stream_data = stream["@data"]["@{0}"]106 except:107 print("check on speckle.com how to access the data")108 109 # transform stream_data to dataframe (create a backup copy of this dataframe)110 df = get_dataframe(stream_data, return_original_df=False)111 df_A = df.copy()112 113 # get stream114 stream = getSpeckleStream(STREAM_ID,115 branchB,116 CLIENT,117 commit_id = "")118 119 # navigate to list with speckle objects of interest120 try:121 stream_data = stream["@Data"]["@{0}"]122 except:123 print("something went wrong, try again with non-capital d")124 try:125 stream_data = stream["@data"]["@{0}"]126 except:127 print("check on speckle.com how to access the data")128 129 # transform stream_data to dataframe (create a backup copy of this dataframe)130 df = get_dataframe(stream_data, return_original_df=False)131 df_B = df.copy()132 133 excludeCol = config["EXCLUDE_COLS"]134 uuidCol = config["UUID_COL"]135 refCol = config["REFERENCE_COL"]136 aggregated_df_b, log_dict = aggregate_data_optimized(df_A.copy(), df_B.copy(), uuidCol, refCol,excludeCol)137 138 139 # additional cleanups, remove geometry and fill na140 try: 141 aggregated_df_b_noGeo = aggregated_df_b.drop(columns=['@geometry'])142 except:143 pass144 try: 145 aggregated_df_b_noGeo = aggregated_df_b.drop(columns=['@Geometry'])146 except:147 pass148 aggregated_df_b_noGeo = aggregated_df_b.fillna("NA")149 150 151 print (aggregated_df_b_noGeo)152 commit_id = updateStreamAnalysisFast(153 client = CLIENT,154 stream_id =STREAM_ID,155 branch_name = branchB,156 new_data=aggregated_df_b_noGeo ,157 geometryGroupPath=["@Data", "@{0}"],158 match_by_id="id",159 #openai_key =None,160 return_original = False,161 comm_message="auto commit from HF; Triggered by:" + update_source)162 163 return "https://speckle.xyz/streams/" + STREAM_ID + "/commits/" + commit_id