harvesthealth commited on
Commit
89c0093
·
verified ·
1 Parent(s): 2b64364

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +105 -8
app.py CHANGED
@@ -19,6 +19,7 @@ from tools_api import search_web_tool, search_hf_spaces_tool
19
  from langchain.tools import Tool
20
  from git import Repo
21
  import shutil
 
22
 
23
  # LangGraph imports
24
  from langgraph.graph import StateGraph, START, END, MessagesState
@@ -112,32 +113,28 @@ chat_llm = ChatOpenAI(
112
  model="alias-large",
113
  base_url=HELMHOLTZ_BASE_URL,
114
  api_key=api_key,
115
- max_tokens=2048,
116
- disable_streaming=True
117
  )
118
 
119
  code_llm = ChatOpenAI(
120
  model="alias-code",
121
  base_url=HELMHOLTZ_BASE_URL,
122
  api_key=api_key,
123
- max_tokens=1024,
124
- disable_streaming=True
125
  )
126
 
127
  fast_llm = ChatOpenAI(
128
  model="alias-fast",
129
  base_url=HELMHOLTZ_BASE_URL,
130
  api_key=api_key,
131
- max_tokens=512,
132
- disable_streaming=True
133
  )
134
 
135
  huge_llm = ChatOpenAI(
136
  model="alias-huge",
137
  base_url=HELMHOLTZ_BASE_URL,
138
  api_key=api_key,
139
- max_tokens=1024,
140
- disable_streaming=True
141
  )
142
 
143
  # Memory Stores
@@ -802,6 +799,96 @@ async def handle_monitor_session(session_id):
802
  except Exception as e:
803
  return f"Error: {e}", "ERROR", "---"
804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
805
  async def update_fields_from_log(log_file):
806
  if not log_file: return "", "", "", "docker"
807
  log_path = os.path.join(LOG_DIR, log_file)
@@ -1068,6 +1155,16 @@ with gr.Blocks() as demo:
1068
  outputs=[repo_input]
1069
  ).then(fn=None, js="() => { document.querySelectorAll('button').forEach(b => { if(b.textContent.includes('Jules Communication')) b.click(); }); }")
1070
 
 
 
 
 
 
 
 
 
 
 
1071
  with gr.Tab("System Test"):
1072
  test_btn = gr.Button("Run Connectivity Test")
1073
  test_output = gr.Textbox(label="Results", lines=10)
 
19
  from langchain.tools import Tool
20
  from git import Repo
21
  import shutil
22
+ import subprocess
23
 
24
  # LangGraph imports
25
  from langgraph.graph import StateGraph, START, END, MessagesState
 
113
  model="alias-large",
114
  base_url=HELMHOLTZ_BASE_URL,
115
  api_key=api_key,
116
+ max_tokens=2048
 
117
  )
118
 
119
  code_llm = ChatOpenAI(
120
  model="alias-code",
121
  base_url=HELMHOLTZ_BASE_URL,
122
  api_key=api_key,
123
+ max_tokens=1024
 
124
  )
125
 
126
  fast_llm = ChatOpenAI(
127
  model="alias-fast",
128
  base_url=HELMHOLTZ_BASE_URL,
129
  api_key=api_key,
130
+ max_tokens=512
 
131
  )
132
 
133
  huge_llm = ChatOpenAI(
134
  model="alias-huge",
135
  base_url=HELMHOLTZ_BASE_URL,
136
  api_key=api_key,
137
+ max_tokens=1024
 
138
  )
139
 
140
  # Memory Stores
 
799
  except Exception as e:
800
  return f"Error: {e}", "ERROR", "---"
801
 
802
+ async def handle_upload_space_to_git(hf_space_full, git_repo_full, branch):
803
+ if not hf_space_full or "/" not in hf_space_full:
804
+ return "Invalid HF Space ID. Expected {hf_profileID}/{spaceID}"
805
+
806
+ hf_parts = hf_space_full.split("/", 1)
807
+ hf_profile = hf_parts[0]
808
+ hf_space = hf_parts[1]
809
+
810
+ if "/" in git_repo_full:
811
+ git_parts = git_repo_full.split("/", 1)
812
+ git_profile = git_parts[0]
813
+ git_repo = git_parts[1]
814
+ else:
815
+ git_profile = git_repo_full
816
+ git_repo = ""
817
+
818
+ if not git_repo:
819
+ git_repo = hf_space
820
+
821
+ if not branch:
822
+ branch = "main"
823
+
824
+ hf_token = os.environ.get(hf_profile)
825
+ git_token = os.environ.get(git_profile)
826
+
827
+ if not hf_token:
828
+ return f"Secret '{hf_profile}' (HF Token) not found in environment."
829
+ if not git_token:
830
+ return f"Secret '{git_profile}' (GitHub Token) not found in environment."
831
+
832
+ upload_dir = os.path.join(WORK_DIR, f"upload_{uuid.uuid4().hex}")
833
+ os.makedirs(upload_dir, exist_ok=True)
834
+
835
+ try:
836
+ # 1. Download HF Space
837
+ download_cmd = [
838
+ "hf", "download",
839
+ "--repo-type", "space",
840
+ hf_space_full,
841
+ "--local-dir", upload_dir,
842
+ "--token", hf_token
843
+ ]
844
+ result = subprocess.run(download_cmd, capture_output=True, text=True)
845
+ if result.returncode != 0:
846
+ return f"Error downloading Space: {result.stderr or result.stdout}"
847
+
848
+ # 2. Check/Create GitHub Repo
849
+ target_url = f"https://{git_token}@github.com/{git_profile}/{git_repo}.git"
850
+ check_cmd = ["git", "ls-remote", target_url]
851
+ check_res = subprocess.run(check_cmd, capture_output=True)
852
+
853
+ if check_res.returncode != 0:
854
+ # Repo might not exist, try to create it via API
855
+ headers = {"Authorization": f"token {git_token}", "Accept": "application/vnd.github.v3+json"}
856
+ # Try create as user repo
857
+ res = requests.post("https://api.github.com/user/repos", json={"name": git_repo, "private": False}, headers=headers)
858
+ if res.status_code != 201:
859
+ # Try create as org repo
860
+ res = requests.post(f"https://api.github.com/orgs/{git_profile}/repos", json={"name": git_repo, "private": False}, headers=headers)
861
+ if res.status_code != 201:
862
+ # If it failed because it already exists (and git ls-remote failed for some other reason), we continue
863
+ if "already exists" not in res.text:
864
+ return f"Failed to create GitHub repository: {res.text}"
865
+
866
+ # 3. Push to GitHub
867
+
868
+ # Initialize and push
869
+ repo = Repo.init(upload_dir)
870
+ # Setup branch
871
+ repo.git.checkout("-b", branch) if branch not in [h.name for h in repo.heads] else repo.git.checkout(branch)
872
+
873
+ repo.git.add(A=True)
874
+ # Commit only if there are changes
875
+ if repo.git.status("--short"):
876
+ repo.index.commit(f"Import from HF Space {hf_space_full}")
877
+
878
+ if "origin" in [remote.name for remote in repo.remotes]:
879
+ origin = repo.remote("origin")
880
+ origin.set_url(target_url)
881
+ else:
882
+ origin = repo.create_remote("origin", target_url)
883
+
884
+ repo.git.push("origin", branch, force=True)
885
+
886
+ return f"Success! {hf_space_full} uploaded to https://github.com/{git_profile}/{git_repo} (branch: {branch})"
887
+
888
+ except Exception as e:
889
+ logger.error(f"Upload failed: {e}")
890
+ return f"Error: {str(e)}"
891
+
892
  async def update_fields_from_log(log_file):
893
  if not log_file: return "", "", "", "docker"
894
  log_path = os.path.join(LOG_DIR, log_file)
 
1155
  outputs=[repo_input]
1156
  ).then(fn=None, js="() => { document.querySelectorAll('button').forEach(b => { if(b.textContent.includes('Jules Communication')) b.click(); }); }")
1157
 
1158
+ with gr.Tab("Upload"):
1159
+ hf_space_id_in = gr.Textbox(label="Hugging Face Space ID ({hf_profileID}/{spaceID})")
1160
+ with gr.Row():
1161
+ git_repo_id_in = gr.Textbox(label="GitHub Repo ID ({git_profileID}/{repoID})", scale=3)
1162
+ git_branch_in = gr.Textbox(label="Branch (default: main)", scale=1)
1163
+ upload_btn = gr.Button("Upload Space to GitHub")
1164
+ upload_output = gr.Textbox(label="Status")
1165
+
1166
+ upload_btn.click(handle_upload_space_to_git, [hf_space_id_in, git_repo_id_in, git_branch_in], [upload_output])
1167
+
1168
  with gr.Tab("System Test"):
1169
  test_btn = gr.Button("Run Connectivity Test")
1170
  test_output = gr.Textbox(label="Results", lines=10)