Pamudu13 commited on
Commit
86e3d75
·
verified ·
1 Parent(s): ca1bf94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py CHANGED
@@ -122,6 +122,38 @@ def search_images(query, num_images=5):
122
  logger.error(f"An error occurred: {str(e)}")
123
  return []
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  @app.route('/search_images', methods=['GET'])
126
  def api_search_images():
127
  try:
 
122
  logger.error(f"An error occurred: {str(e)}")
123
  return []
124
 
125
+
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
+ return restart_space(space_id)
156
+
157
  @app.route('/search_images', methods=['GET'])
158
  def api_search_images():
159
  try: