broadfield-dev commited on
Commit
18a9d30
·
verified ·
1 Parent(s): 5afbff9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -11
app.py CHANGED
@@ -44,6 +44,37 @@ def verify_organization_access(organization: str, token: str) -> bool:
44
  except Exception as e:
45
  logger.error(f"No write access to organization {organization}: {str(e)}")
46
  return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  @app.route('/create-space', methods=['POST'])
49
  def create_hf_space():
@@ -72,17 +103,15 @@ def create_hf_space():
72
  full_repo_id = f"{ORGANIZATION}/{space_name}"
73
 
74
  # Verify token has access to Space-Share organization
75
- if not verify_organization_access(ORGANIZATION, HF_TOKEN):
76
- return jsonify({"error": f"No write access to organization {ORGANIZATION}. Check your HF_TOKEN permissions."}), 403
77
-
78
  try:
79
  # Attempt to create the repository with explicit organization context
80
- repo_info = create_repo(
81
- repo_id=space_name,
82
- organization=ORGANIZATION, # Explicitly specify the organization
83
  repo_type="space",
84
  space_sdk=space_type,
85
- token=HF_TOKEN,
86
  private=False,
87
  exist_ok=True # Allow creation even if the repo might exist
88
  )
@@ -110,12 +139,11 @@ def create_hf_space():
110
 
111
  # Upload to the new Space
112
  try:
113
- upload_file(
114
  path_or_fileobj=f"temp_{filename}",
115
  path_in_repo=filename,
116
  repo_id=full_repo_id,
117
  repo_type="space",
118
- token=HF_TOKEN
119
  )
120
  logger.info(f"Uploaded file: {filename}")
121
  except Exception as e:
@@ -135,7 +163,7 @@ def create_hf_space():
135
  with open("temp_requirements.txt", "w") as f:
136
  f.write(default_requirements)
137
  try:
138
- upload_file(
139
  path_or_fileobj="temp_requirements.txt",
140
  path_in_repo="requirements.txt",
141
  repo_id=full_repo_id,
@@ -161,7 +189,7 @@ CMD ["python", "app.py"]
161
  with open("temp_Dockerfile", "w") as f:
162
  f.write(default_dockerfile)
163
  try:
164
- upload_file(
165
  path_or_fileobj="temp_Dockerfile",
166
  path_in_repo="Dockerfile",
167
  repo_id=full_repo_id,
 
44
  except Exception as e:
45
  logger.error(f"No write access to organization {organization}: {str(e)}")
46
  return False
47
+ def build_space(repo_name,file_name,file_content,access_token=""):
48
+ try:
49
+ repo_path=user_+str(repo_)
50
+ access_token=os.environ['HF_TOKEN']
51
+ if not access_token:
52
+ return [{'role':'assistant','content': 'ENTER A HUGGINGFACE TOKEN'}]
53
+ api=HfApi(endpoint="https://huggingface.co", token=access_token)
54
+ repo_url = api.create_repo(
55
+ repo_id=repo_path,
56
+ repo_type="space",
57
+ space_sdk="gradio",
58
+ exist_ok=True,
59
+ private=False,
60
+ )
61
+
62
+ local_file_path=str(uuid.uuid4())
63
+ with open(file_name, 'w') as f:
64
+ f.write(str(file_content).strip("''").strip('""').strip("{}").format())
65
+ f.close()
66
+ # Upload a local file to the Space
67
+ commit_message = "Adding file test: "+ str(file_name)
68
+
69
+ api.upload_file(path_or_fileobj=file_name, path_in_repo=file_name.strip("./"), repo_id=repo_path, repo_type='space', commit_message=commit_message)
70
+ print("File uploaded successfully.")
71
+ # Commit changes
72
+ commit_message += "\nInitial commit to the repository."+ f'{repo_path}/' + f'{file_name}'
73
+ #api.commit_repo(space_id, message=commit_message)
74
+ return [{'role':'assistant','content': commit_message+'\nCommit Success' }]
75
+ except Exception as e:
76
+ print("ERROR ",e)
77
+ return [{'role':'assistant','content': 'There was an Error: ' + str(e)}]
78
 
79
  @app.route('/create-space', methods=['POST'])
80
  def create_hf_space():
 
103
  full_repo_id = f"{ORGANIZATION}/{space_name}"
104
 
105
  # Verify token has access to Space-Share organization
106
+ #if not verify_organization_access(ORGANIZATION, HF_TOKEN):
107
+ # return jsonify({"error": f"No write access to organization {ORGANIZATION}. Check your HF_TOKEN permissions."}), 403
108
+ api=HfApi(endpoint="https://huggingface.co", token=access_token)
109
  try:
110
  # Attempt to create the repository with explicit organization context
111
+ repo_info = api.create_repo(
112
+ repo_id=full_repo_id,
 
113
  repo_type="space",
114
  space_sdk=space_type,
 
115
  private=False,
116
  exist_ok=True # Allow creation even if the repo might exist
117
  )
 
139
 
140
  # Upload to the new Space
141
  try:
142
+ api.upload_file(
143
  path_or_fileobj=f"temp_{filename}",
144
  path_in_repo=filename,
145
  repo_id=full_repo_id,
146
  repo_type="space",
 
147
  )
148
  logger.info(f"Uploaded file: {filename}")
149
  except Exception as e:
 
163
  with open("temp_requirements.txt", "w") as f:
164
  f.write(default_requirements)
165
  try:
166
+ api.upload_file(
167
  path_or_fileobj="temp_requirements.txt",
168
  path_in_repo="requirements.txt",
169
  repo_id=full_repo_id,
 
189
  with open("temp_Dockerfile", "w") as f:
190
  f.write(default_dockerfile)
191
  try:
192
+ api.upload_file(
193
  path_or_fileobj="temp_Dockerfile",
194
  path_in_repo="Dockerfile",
195
  repo_id=full_repo_id,