Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,25 +4,19 @@ import sys # Add this import to fix the error
|
|
| 4 |
import datetime
|
| 5 |
import time
|
| 6 |
import requests
|
| 7 |
-
|
| 8 |
-
from ui.statusui import StatusUI
|
| 9 |
-
from ui.contentagentui import ContentAgentUI
|
| 10 |
-
from ui.gradioui import GradioUI, create_failed_gradio_ui
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
from checks.failed_check import create_failed_gradio_ui
|
| 14 |
-
from checks.endpoint_check import check_public_endpoint
|
| 15 |
-
from checks.health_check import check_model_endpoint, should_launch_ui
|
| 16 |
-
|
| 17 |
import os
|
| 18 |
import pytz # Had to give it permission in Code agent
|
| 19 |
|
| 20 |
-
from tools.tools import load_tools
|
| 21 |
-
|
| 22 |
from agents.model import load_huggingface_model
|
| 23 |
from agents.prompts import load_prompts
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def initialize_agent(endpoint_uri: str):
|
| 28 |
|
|
@@ -50,81 +44,16 @@ def initialize_agent(endpoint_uri: str):
|
|
| 50 |
additional_authorized_imports=["pytz"]
|
| 51 |
)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
def wake_up_endpoint(endpoint_uri, ui, max_wait=300):
|
| 55 |
-
"""Poll the endpoint until it responds OK or timeout."""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# Enforce minimum wait time of 60 seconds
|
| 59 |
-
max_wait = max(max_wait, 60)
|
| 60 |
-
# Get token from environment variable or secrets store
|
| 61 |
-
hf_token = os.environ.get("HF_TOKEN") # Or replace with your secret loading logic
|
| 62 |
-
headers = {"Authorization": f"Bearer {hf_token}"} if hf_token else {}
|
| 63 |
-
|
| 64 |
-
payload = {"inputs": "ping"} # Adjust to minimal valid input
|
| 65 |
-
start = time.time()
|
| 66 |
-
|
| 67 |
-
while time.time() - start < max_wait:
|
| 68 |
-
try:
|
| 69 |
-
ui.append("Pinging endpoint...")
|
| 70 |
-
ui.append(endpoint_uri)
|
| 71 |
-
response = requests.post(endpoint_uri, headers=headers, json=payload, timeout=5)
|
| 72 |
-
|
| 73 |
-
if response.ok:
|
| 74 |
-
ui.append("✅ Endpoint is awake.")
|
| 75 |
-
return True
|
| 76 |
-
|
| 77 |
-
# If it's a 503 or 504, it's likely still warming up
|
| 78 |
-
ui.append(f"Response: {response.status_code}, retrying...")
|
| 79 |
-
except requests.exceptions.RequestException as e:
|
| 80 |
-
ui.append(f"Exception: {e}, retrying...")
|
| 81 |
-
|
| 82 |
-
time.sleep(3) # fixed short backoff; can be exponential if needed
|
| 83 |
-
|
| 84 |
-
ui.append("❌ Timed out waiting for endpoint to wake up.")
|
| 85 |
-
return False
|
| 86 |
-
|
| 87 |
def main():
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
#
|
| 93 |
-
#
|
| 94 |
-
|
| 95 |
-
#ui.
|
| 96 |
-
|
| 97 |
-
# Load endpoint and check health
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
# Prechecks
|
| 101 |
-
ui.append("Starting prechecks...")
|
| 102 |
-
ui.append("Checking endpoint..")
|
| 103 |
-
|
| 104 |
-
endpoint_uri = load_huggingface_model() # Get the URI for the endpoint
|
| 105 |
-
ui.append(endpoint_uri)
|
| 106 |
-
|
| 107 |
-
# Wake it up before health check
|
| 108 |
-
wake_up_successful = wake_up_endpoint(endpoint_uri, ui)
|
| 109 |
-
|
| 110 |
-
if not wake_up_successful:
|
| 111 |
-
ui.append("Warning: Could not wake up the endpoint. Continuing.")
|
| 112 |
-
else:
|
| 113 |
-
ui.append("✅ End point responded OK.")
|
| 114 |
-
|
| 115 |
-
is_healthy, status_info = check_model_endpoint(endpoint_uri) # Test the endpoint
|
| 116 |
-
|
| 117 |
-
if not is_healthy:
|
| 118 |
-
interface = create_failed_gradio_ui(status_info)
|
| 119 |
-
interface.launch(show_error=True, share=True)
|
| 120 |
-
return
|
| 121 |
-
|
| 122 |
-
# Initialize and run the agent
|
| 123 |
-
agent = initialize_agent(endpoint_uri)
|
| 124 |
-
# Create an instance of the ContentAgentUI class
|
| 125 |
-
ui = ContentAgentUI()
|
| 126 |
-
ui.pass_through_agent(agent) # Pass through the agent
|
| 127 |
-
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
|
|
|
|
| 4 |
import datetime
|
| 5 |
import time
|
| 6 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import os
|
| 8 |
import pytz # Had to give it permission in Code agent
|
| 9 |
|
|
|
|
|
|
|
| 10 |
from agents.model import load_huggingface_model
|
| 11 |
from agents.prompts import load_prompts
|
| 12 |
|
| 13 |
+
from ui.contentagentui import ContentAgentUI
|
| 14 |
+
from ui.gradioui import GradioUI, create_failed_gradio_ui
|
| 15 |
+
|
| 16 |
+
from smolagents import CodeAgent, HfApiModel
|
| 17 |
+
from status_check import run_status_checks
|
| 18 |
+
from tools.tools import load_tools
|
| 19 |
+
from agents.prompts import load_prompts
|
| 20 |
|
| 21 |
def initialize_agent(endpoint_uri: str):
|
| 22 |
|
|
|
|
| 44 |
additional_authorized_imports=["pytz"]
|
| 45 |
)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def main():
|
| 48 |
+
endpoint_uri = run_status_checks()
|
| 49 |
+
|
| 50 |
+
if endpoint_uri:
|
| 51 |
+
# Initialize and run the agent
|
| 52 |
+
#agent = initialize_agent(endpoint_uri)
|
| 53 |
+
# Create an instance of the ContentAgentUI class
|
| 54 |
+
#ui = ContentAgentUI()
|
| 55 |
+
#ui.pass_through_agent(agent) # Pass through the agent
|
| 56 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
|