Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ from io import BytesIO
|
|
| 12 |
from googlesearch import search
|
| 13 |
import logging
|
| 14 |
import queue
|
|
|
|
| 15 |
|
| 16 |
# Create a logging filter to suppress socket warnings
|
| 17 |
class SocketWarningFilter(logging.Filter):
|
|
@@ -126,33 +127,36 @@ def search_images(query, num_images=5):
|
|
| 126 |
|
| 127 |
HF_TOKEN = os.getenv("HF_TOKEN") # Make sure you set the HF_TOKEN in your environment
|
| 128 |
|
| 129 |
-
def restart_space(space_id):
|
| 130 |
-
"""Restart a Hugging Face Space using the API."""
|
| 131 |
-
api_url = f"https://api.huggingface.co/spaces/{space_id}/restart" # Corrected API endpoint for restarting Spaces
|
| 132 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 133 |
-
try:
|
| 134 |
-
response = requests.post(api_url, headers=headers)
|
| 135 |
-
if response.status_code == 200:
|
| 136 |
-
logger.info(f"Successfully restarted Space: {space_id}")
|
| 137 |
-
return jsonify({"success": True, "message": f"Successfully restarted Space: {space_id}"}), 200
|
| 138 |
-
else:
|
| 139 |
-
logger.error(f"Failed to restart Space: {space_id} - {response.status_code} - {response.text}")
|
| 140 |
-
return jsonify({"success": False, "message": f"Failed to restart Space: {space_id} - {response.status_code} - {response.text}"}), 500
|
| 141 |
-
except Exception as e:
|
| 142 |
-
logger.error(f"Error restarting space: {str(e)}")
|
| 143 |
-
return jsonify({"success": False, "message": f"Error: {str(e)}"}), 500
|
| 144 |
-
|
| 145 |
@app.route('/restart_space', methods=['POST'])
|
| 146 |
def api_restart_space():
|
| 147 |
"""API route to restart a Hugging Face Space."""
|
| 148 |
space_id = 'Pamudu13/web-scraper'
|
| 149 |
-
|
| 150 |
-
# Expecting the space_id in the JSON body
|
| 151 |
|
| 152 |
if not space_id:
|
| 153 |
return jsonify({'error': 'space_id parameter is required'}), 400
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
@app.route('/search_images', methods=['GET'])
|
| 158 |
def api_search_images():
|
|
|
|
| 12 |
from googlesearch import search
|
| 13 |
import logging
|
| 14 |
import queue
|
| 15 |
+
from huggingface_hub import HfApi
|
| 16 |
|
| 17 |
# Create a logging filter to suppress socket warnings
|
| 18 |
class SocketWarningFilter(logging.Filter):
|
|
|
|
| 127 |
|
| 128 |
HF_TOKEN = os.getenv("HF_TOKEN") # Make sure you set the HF_TOKEN in your environment
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
@app.route('/restart_space', methods=['POST'])
|
| 131 |
def api_restart_space():
|
| 132 |
"""API route to restart a Hugging Face Space."""
|
| 133 |
space_id = 'Pamudu13/web-scraper'
|
| 134 |
+
factory_reboot = request.json.get('factory_reboot', False) # Optional: Set to True if you want a factory reboot
|
|
|
|
| 135 |
|
| 136 |
if not space_id:
|
| 137 |
return jsonify({'error': 'space_id parameter is required'}), 400
|
| 138 |
|
| 139 |
+
try:
|
| 140 |
+
hfapi = HfApi()
|
| 141 |
+
|
| 142 |
+
# Call the restart_space method
|
| 143 |
+
res = hfapi.restart_space(
|
| 144 |
+
space_id,
|
| 145 |
+
token=HF_TOKEN,
|
| 146 |
+
factory_reboot=factory_reboot
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
return jsonify({
|
| 150 |
+
'success': True,
|
| 151 |
+
'message': f"Successfully restarted Space: {space_id}",
|
| 152 |
+
'response': res
|
| 153 |
+
}), 200
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
return jsonify({
|
| 157 |
+
'success': False,
|
| 158 |
+
'message': f"Error: {str(e)}"
|
| 159 |
+
}), 500
|
| 160 |
|
| 161 |
@app.route('/search_images', methods=['GET'])
|
| 162 |
def api_search_images():
|