Pamudu13 commited on
Commit
cd81c16
·
verified ·
1 Parent(s): 84fa013

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -158,37 +158,42 @@ def api_restart_space():
158
  'message': f"Error: {str(e)}"
159
  }), 500
160
 
161
- @app.route('/get_space_status', methods=['GET'])
162
- def get_space_status():
163
- """API route to get the status of a Hugging Face Space."""
164
- space_id = 'Pamudu13/web-scraper' # Expecting the space_id as a query parameter
165
 
166
  if not space_id:
167
  return jsonify({'error': 'space_id parameter is required'}), 400
168
 
169
- try:
170
  hfapi = HfApi()
171
 
172
- # Get information about the space
173
- space_info = hfapi.get_space_info(space_id, token=HF_TOKEN)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
 
175
- # Extract relevant details from the space_info (you can modify this based on your needs)
176
- status = space_info.get('status', 'Unknown') # Assuming 'status' field is present
177
- description = space_info.get('description', 'No description available')
 
178
 
179
- return jsonify({
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'])