broadfield-dev commited on
Commit
18d0b22
·
verified ·
1 Parent(s): f723f6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -4,7 +4,9 @@ import json
4
  from huggingface_hub import HfApi, create_repo, upload_file
5
  import random
6
  import string
7
- import assembler_docs
 
 
8
 
9
  app = Flask(__name__)
10
 
@@ -15,12 +17,11 @@ if not HF_TOKEN:
15
 
16
  hf_api = HfApi()
17
 
18
- # Documentation as a string
19
- DOCUMENTATION = assembler_docs.DOCUMENTATION
20
  def generate_space_name():
21
  """Generate a unique Space name."""
22
  random_suffix = ''.join(random.choices(string.ascii_lowercase + string.digits, k=6))
23
  return f"GeneratedSpace-{random_suffix}"
 
24
  @app.route('/create-space', methods=['POST'])
25
  def create_hf_space():
26
  try:
@@ -28,23 +29,24 @@ def create_hf_space():
28
  data = request.get_json()
29
  if not data:
30
  return jsonify({"error": "No JSON data provided"}), 400
 
31
  # Extract parameters
32
  space_type = data.get("space_type", "gradio") # Default to gradio if not specified
33
  files = data.get("files", {}) # Dictionary of filename: content
34
  params = data.get("parameters", {}) # Optional parameters
35
-
36
  if not files:
37
  return jsonify({"error": "No files provided in JSON"}), 400
38
-
39
  # Validate space_type
40
  valid_space_types = ["gradio", "static", "docker", "streamlit"]
41
  if space_type not in valid_space_types:
42
  return jsonify({"error": f"Invalid space_type. Must be one of {valid_space_types}"}), 400
43
-
44
- # Create a unique Space name and repo
45
  space_name = generate_space_name()
46
- full_repo_id = f"{hf_api.whoami(HF_TOKEN)['name']}/{space_name}"
47
-
48
  create_repo(
49
  repo_id=space_name,
50
  repo_type="space",
@@ -52,7 +54,7 @@ def create_hf_space():
52
  token=HF_TOKEN,
53
  private=False
54
  )
55
-
56
  # Handle multi-file uploads
57
  for filename, content in files.items():
58
  # Write content to a temporary file
@@ -61,7 +63,7 @@ def create_hf_space():
61
  # Inject parameters into Python files if present
62
  content = f"PARAMS = {json.dumps(params)}\n\n{content}"
63
  f.write(content)
64
-
65
  # Upload to the new Space
66
  upload_file(
67
  path_or_fileobj=f"temp_{filename}",
@@ -71,7 +73,7 @@ def create_hf_space():
71
  token=HF_TOKEN
72
  )
73
  os.remove(f"temp_{filename}")
74
-
75
  # Add requirements.txt if not provided (basic defaults)
76
  if "requirements.txt" not in files:
77
  default_requirements = {
@@ -90,17 +92,17 @@ def create_hf_space():
90
  token=HF_TOKEN
91
  )
92
  os.remove("temp_requirements.txt")
93
-
94
  # Special handling for Docker Spaces
95
  if space_type == "docker" and "Dockerfile" not in files:
96
  default_dockerfile = """
97
- FROM python:3.10-slim
98
- WORKDIR /app
99
- COPY . .
100
- RUN pip install -r requirements.txt
101
- EXPOSE 7860
102
- CMD ["python", "app.py"]
103
- """
104
  with open("temp_Dockerfile", "w") as f:
105
  f.write(default_dockerfile)
106
  upload_file(
@@ -111,7 +113,8 @@ def create_hf_space():
111
  token=HF_TOKEN
112
  )
113
  os.remove("temp_Dockerfile")
114
- space_url = f"https://huggingface.co/spaces/{full_repo_id}"
 
115
  return jsonify({
116
  "message": "New Space created",
117
  "url": space_url,
@@ -122,9 +125,11 @@ def create_hf_space():
122
  return jsonify({"error": "Invalid JSON format"}), 400
123
  except Exception as e:
124
  return jsonify({"error": str(e)}), 500
 
125
  @app.route('/docs', methods=['GET'])
126
  def get_docs():
127
  """Return the API documentation as plain text."""
128
  return Response(DOCUMENTATION, mimetype='text/plain'), 200
129
- if '__name__'== '__main__':
 
130
  app.run(host='0.0.0.0', port=7860)
 
4
  from huggingface_hub import HfApi, create_repo, upload_file
5
  import random
6
  import string
7
+
8
+ # Import documentation from assembler_docs.py
9
+ from assembler_docs import DOCUMENTATION
10
 
11
  app = Flask(__name__)
12
 
 
17
 
18
  hf_api = HfApi()
19
 
 
 
20
  def generate_space_name():
21
  """Generate a unique Space name."""
22
  random_suffix = ''.join(random.choices(string.ascii_lowercase + string.digits, k=6))
23
  return f"GeneratedSpace-{random_suffix}"
24
+
25
  @app.route('/create-space', methods=['POST'])
26
  def create_hf_space():
27
  try:
 
29
  data = request.get_json()
30
  if not data:
31
  return jsonify({"error": "No JSON data provided"}), 400
32
+
33
  # Extract parameters
34
  space_type = data.get("space_type", "gradio") # Default to gradio if not specified
35
  files = data.get("files", {}) # Dictionary of filename: content
36
  params = data.get("parameters", {}) # Optional parameters
37
+
38
  if not files:
39
  return jsonify({"error": "No files provided in JSON"}), 400
40
+
41
  # Validate space_type
42
  valid_space_types = ["gradio", "static", "docker", "streamlit"]
43
  if space_type not in valid_space_types:
44
  return jsonify({"error": f"Invalid space_type. Must be one of {valid_space_types}"}), 400
45
+
46
+ # Create a unique Space name and repo under Space-Share namespace
47
  space_name = generate_space_name()
48
+ full_repo_id = f"Space-Share/{space_name}"
49
+
50
  create_repo(
51
  repo_id=space_name,
52
  repo_type="space",
 
54
  token=HF_TOKEN,
55
  private=False
56
  )
57
+
58
  # Handle multi-file uploads
59
  for filename, content in files.items():
60
  # Write content to a temporary file
 
63
  # Inject parameters into Python files if present
64
  content = f"PARAMS = {json.dumps(params)}\n\n{content}"
65
  f.write(content)
66
+
67
  # Upload to the new Space
68
  upload_file(
69
  path_or_fileobj=f"temp_{filename}",
 
73
  token=HF_TOKEN
74
  )
75
  os.remove(f"temp_{filename}")
76
+
77
  # Add requirements.txt if not provided (basic defaults)
78
  if "requirements.txt" not in files:
79
  default_requirements = {
 
92
  token=HF_TOKEN
93
  )
94
  os.remove("temp_requirements.txt")
95
+
96
  # Special handling for Docker Spaces
97
  if space_type == "docker" and "Dockerfile" not in files:
98
  default_dockerfile = """
99
+ FROM python:3.10-slim
100
+ WORKDIR /app
101
+ COPY . .
102
+ RUN pip install -r requirements.txt
103
+ EXPOSE 7860
104
+ CMD ["python", "app.py"]
105
+ """
106
  with open("temp_Dockerfile", "w") as f:
107
  f.write(default_dockerfile)
108
  upload_file(
 
113
  token=HF_TOKEN
114
  )
115
  os.remove("temp_Dockerfile")
116
+
117
+ space_url = f"https://huggingface.co/spaces/Space-Share/{space_name}"
118
  return jsonify({
119
  "message": "New Space created",
120
  "url": space_url,
 
125
  return jsonify({"error": "Invalid JSON format"}), 400
126
  except Exception as e:
127
  return jsonify({"error": str(e)}), 500
128
+
129
  @app.route('/docs', methods=['GET'])
130
  def get_docs():
131
  """Return the API documentation as plain text."""
132
  return Response(DOCUMENTATION, mimetype='text/plain'), 200
133
+
134
+ if __name__ == '__main__':
135
  app.run(host='0.0.0.0', port=7860)