Pamudu13 commited on
Commit
9aadbe4
·
verified ·
1 Parent(s): cd81c16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
app.py CHANGED
@@ -161,34 +161,27 @@ def api_restart_space():
161
  @app.route('/get_live_space_status', methods=['GET'])
162
  def get_live_space_status():
163
  """API route to stream live status of a Hugging Face Space."""
164
- space_id = 'Pamudu13/web-scraper'
165
-
166
- if not space_id:
167
- return jsonify({'error': 'space_id parameter is required'}), 400
168
 
169
  def generate():
170
- hfapi = HfApi()
171
-
172
  while True:
173
  try:
174
- # Get information about the space
175
- space_info = hfapi.get_space_info(space_id, token=HF_TOKEN)
176
 
177
- # Extract the status of the space (adjust according to the response structure)
178
- status = space_info.get('status', 'Unknown')
179
- description = space_info.get('description', 'No description available')
180
 
181
  # Send the status as a Server-Sent Event
182
  yield f"data: {status}\n\n"
183
-
184
- # Optionally send more detailed info
185
- yield f"data: {description}\n\n"
186
 
187
  # Delay before checking the status again
188
- time.sleep(5) # Adjust polling interval as needed (e.g., 5 seconds)
189
 
190
  except Exception as e:
191
- # Handle error, you could send a failure message or stop the stream
192
  yield f"data: Error: {str(e)}\n\n"
193
  break # Stop the stream in case of an error
194
 
@@ -196,6 +189,7 @@ def get_live_space_status():
196
 
197
 
198
 
 
199
  @app.route('/search_images', methods=['GET'])
200
  def api_search_images():
201
  try:
 
161
  @app.route('/get_live_space_status', methods=['GET'])
162
  def get_live_space_status():
163
  """API route to stream live status of a Hugging Face Space."""
164
+ space_id = request.args.get('space_id', 'Pamudu13/web-scraper') # Default to 'Pamudu13/web-scraper' if not provided
 
 
 
165
 
166
  def generate():
 
 
167
  while True:
168
  try:
169
+ # Fetch the current runtime status of the Space
170
+ space_runtime = hf_api.get_space_runtime(repo_id=space_id)
171
 
172
+ # Extract relevant details
173
+ status = space_runtime.stage # e.g., 'BUILDING', 'RUNNING', etc.
174
+ hardware = space_runtime.hardware # e.g., 'cpu-basic', 't4-medium', etc.
175
 
176
  # Send the status as a Server-Sent Event
177
  yield f"data: {status}\n\n"
178
+ yield f"data: {hardware}\n\n"
 
 
179
 
180
  # Delay before checking the status again
181
+ time.sleep(5) # Adjust polling interval as needed
182
 
183
  except Exception as e:
184
+ # Handle errors and send an error message
185
  yield f"data: Error: {str(e)}\n\n"
186
  break # Stop the stream in case of an error
187
 
 
189
 
190
 
191
 
192
+
193
  @app.route('/search_images', methods=['GET'])
194
  def api_search_images():
195
  try: