Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -158,37 +158,42 @@ def api_restart_space():
|
|
158 |
'message': f"Error: {str(e)}"
|
159 |
}), 500
|
160 |
|
161 |
-
@app.route('/
|
162 |
-
def
|
163 |
-
"""API route to
|
164 |
-
space_id = 'Pamudu13/web-scraper'
|
165 |
|
166 |
if not space_id:
|
167 |
return jsonify({'error': 'space_id parameter is required'}), 400
|
168 |
|
169 |
-
|
170 |
hfapi = HfApi()
|
171 |
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
178 |
|
179 |
-
|
180 |
-
'success': True,
|
181 |
-
'space_id': space_id,
|
182 |
-
'status': status,
|
183 |
-
'description': description,
|
184 |
-
'space_info': space_info
|
185 |
-
}), 200
|
186 |
|
187 |
-
except Exception as e:
|
188 |
-
return jsonify({
|
189 |
-
'success': False,
|
190 |
-
'message': f"Error: {str(e)}"
|
191 |
-
}), 500
|
192 |
|
193 |
|
194 |
@app.route('/search_images', methods=['GET'])
|
|
|
158 |
'message': f"Error: {str(e)}"
|
159 |
}), 500
|
160 |
|
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 |
|
195 |
+
return Response(stream_with_context(generate()), mimetype='text/event-stream')
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
|
|
|
|
|
|
|
|
|
|
197 |
|
198 |
|
199 |
@app.route('/search_images', methods=['GET'])
|