Spaces:
Sleeping
Sleeping
earlsab commited on
Commit ·
7656bed
1
Parent(s): 12b745b
update retry progress meter
Browse files
app.py
CHANGED
|
@@ -66,74 +66,100 @@ def call_api(endpoint_url: str, payload: Dict[str, Any], max_retries: int = 5, r
|
|
| 66 |
return {}
|
| 67 |
|
| 68 |
def wake_servers(progress=gr.Progress()):
|
| 69 |
-
"""Send wake-up requests to all endpoints"""
|
| 70 |
results = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
def try_wake_endpoint(name, url
|
| 73 |
"""Helper function to wake endpoint with retry logic"""
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 81 |
-
timeout=45
|
| 82 |
-
)
|
| 83 |
-
if response.status_code == 200:
|
| 84 |
-
return f"Status: {response.status_code}"
|
| 85 |
-
elif retry_count < max_retries:
|
| 86 |
-
# Calculate retry delay - 20 seconds for first retry, 5 seconds for subsequent retries
|
| 87 |
-
retry_delay = 20 if retry_count == 0 else 5
|
| 88 |
-
|
| 89 |
-
# Update progress with retry information
|
| 90 |
-
progress_desc = f"Retrying {name} endpoint (attempt {retry_count+1}/{max_retries}, waiting {retry_delay}s)..."
|
| 91 |
-
progress(progress.value, desc=progress_desc)
|
| 92 |
-
print(progress_desc)
|
| 93 |
-
|
| 94 |
-
# Wait before retrying
|
| 95 |
-
time.sleep(retry_delay)
|
| 96 |
-
|
| 97 |
-
# Retry
|
| 98 |
-
return try_wake_endpoint(name, url, retry_count + 1, max_retries)
|
| 99 |
-
else:
|
| 100 |
-
# All retries failed
|
| 101 |
-
return f"Status: {response.status_code} (Failed after {max_retries} retries)"
|
| 102 |
-
except Exception as e:
|
| 103 |
-
if retry_count < max_retries:
|
| 104 |
-
# Calculate retry delay - 20 seconds for first retry, 5 seconds for subsequent retries
|
| 105 |
-
retry_delay = 20 if retry_count == 0 else 5
|
| 106 |
-
|
| 107 |
-
# Update progress with retry information
|
| 108 |
-
progress_desc = f"Error connecting to {name} endpoint. Retrying (attempt {retry_count+1}/{max_retries}, waiting {retry_delay}s)..."
|
| 109 |
-
progress(progress.value, desc=progress_desc)
|
| 110 |
-
print(progress_desc)
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
|
|
|
| 129 |
progress(1.0, desc="Complete!")
|
| 130 |
-
|
| 131 |
-
for name, status in results.items():
|
| 132 |
-
status_color = "green" if "Status: 200" in status else "red"
|
| 133 |
-
status_html += f"<li><strong>{name}</strong>: <span style='color:{status_color}'>{status}</span></li>"
|
| 134 |
-
status_html += "</ul>"
|
| 135 |
-
|
| 136 |
-
return status_html
|
| 137 |
|
| 138 |
def process_job_description(job_description: str) -> Dict:
|
| 139 |
"""Process job description and extract skills using the job endpoint"""
|
|
@@ -679,8 +705,9 @@ with gr.Blocks(title="Beyond Keywords: Resume Analysis System", js=js_func) as d
|
|
| 679 |
gr.Markdown("# Beyond Keywords: Job Description and Resume Analyzer")
|
| 680 |
gr.Markdown("Upload a job description and resume(s) to analyze skill matches and quality.")
|
| 681 |
|
| 682 |
-
wake_btn = gr.Button("Wake Servers (Do this first!)")
|
| 683 |
-
wake_status = gr.HTML(label="Server Status", value="<div style='color: #666;'>Click 'Wake Servers'<br>to initialize the system...</div>")
|
|
|
|
| 684 |
gr.Markdown("""<div style="height: 20px;"></div>""")
|
| 685 |
|
| 686 |
with gr.Row():
|
|
|
|
| 66 |
return {}
|
| 67 |
|
| 68 |
def wake_servers(progress=gr.Progress()):
|
| 69 |
+
"""Send wake-up requests to all endpoints in parallel with real-time updates"""
|
| 70 |
results = {}
|
| 71 |
+
status_html = "<h3>Server Wake-up Results:</h3><ul>"
|
| 72 |
+
for name in ENDPOINTS.keys():
|
| 73 |
+
results[name] = "Pending..."
|
| 74 |
+
status_html += f"<li><strong>{name}</strong>: <span style='color:gray'>Pending...</span></li>"
|
| 75 |
+
status_html += "</ul>"
|
| 76 |
+
|
| 77 |
+
# Initial status HTML
|
| 78 |
+
yield status_html
|
| 79 |
+
|
| 80 |
+
def update_status_html():
|
| 81 |
+
"""Generate HTML for current status"""
|
| 82 |
+
html = "<h3>Server Wake-up Results:</h3><ul>"
|
| 83 |
+
for name, status in results.items():
|
| 84 |
+
status_color = "green" if "Status: 200" in status else "red" if "Error" in status or "Failed" in status else "gray"
|
| 85 |
+
html += f"<li><strong>{name}</strong>: <span style='color:{status_color}'>{status}</span></li>"
|
| 86 |
+
html += "</ul>"
|
| 87 |
+
return html
|
| 88 |
|
| 89 |
+
def try_wake_endpoint(name, url):
|
| 90 |
"""Helper function to wake endpoint with retry logic"""
|
| 91 |
+
retry_delays = [30, 20, 20, 20, 20, 20, 20, 20, 20, 20] # Seconds to wait between retries
|
| 92 |
+
|
| 93 |
+
for retry_count, retry_delay in enumerate(retry_delays):
|
| 94 |
+
try:
|
| 95 |
+
# Update status to show attempt
|
| 96 |
+
results[name] = f"Attempting to connect... (try {retry_count+1}/{len(retry_delays)+1})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# Send a small payload just to wake up the server
|
| 99 |
+
minimal_payload = {"inputs": "Hello"}
|
| 100 |
+
response = requests.post(
|
| 101 |
+
url,
|
| 102 |
+
json=minimal_payload,
|
| 103 |
+
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 104 |
+
timeout=45
|
| 105 |
+
)
|
| 106 |
|
| 107 |
+
if response.status_code == 200:
|
| 108 |
+
results[name] = f"Status: {response.status_code}"
|
| 109 |
+
return
|
| 110 |
+
else:
|
| 111 |
+
# Non-200 response, prepare for retry
|
| 112 |
+
if retry_count < len(retry_delays):
|
| 113 |
+
results[name] = f"Status: {response.status_code}, retrying in {retry_delay}s... (attempt {retry_count+1}/{len(retry_delays)})"
|
| 114 |
+
time.sleep(retry_delay)
|
| 115 |
+
else:
|
| 116 |
+
# All retries failed
|
| 117 |
+
results[name] = f"Status: {response.status_code} (Failed after {len(retry_delays)} retries)"
|
| 118 |
+
return
|
| 119 |
+
|
| 120 |
+
except Exception as e:
|
| 121 |
+
# Connection error, prepare for retry
|
| 122 |
+
if retry_count < len(retry_delays):
|
| 123 |
+
results[name] = f"Error connecting, retrying in {retry_delay}s... (attempt {retry_count+1}/{len(retry_delays)})"
|
| 124 |
+
time.sleep(retry_delay)
|
| 125 |
+
else:
|
| 126 |
+
# All retries failed
|
| 127 |
+
results[name] = f"Error: {str(e)} (Failed after {len(retry_delays)} retries)"
|
| 128 |
+
return
|
| 129 |
+
|
| 130 |
+
# Function to process a single endpoint and update UI
|
| 131 |
+
def process_endpoint(name, url):
|
| 132 |
+
try:
|
| 133 |
+
try_wake_endpoint(name, url)
|
| 134 |
+
finally:
|
| 135 |
+
# Return the updated status HTML
|
| 136 |
+
return update_status_html()
|
| 137 |
|
| 138 |
+
# Create a thread pool to wake up servers in parallel
|
| 139 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 140 |
+
# Start all tasks
|
| 141 |
+
futures = {executor.submit(process_endpoint, name, url): name
|
| 142 |
+
for name, url in ENDPOINTS.items()}
|
| 143 |
|
| 144 |
+
# Process results as they complete
|
| 145 |
+
for future in concurrent.futures.as_completed(futures):
|
| 146 |
+
name = futures[future]
|
| 147 |
+
try:
|
| 148 |
+
# Get the updated status HTML
|
| 149 |
+
status_html = future.result()
|
| 150 |
+
# Update progress
|
| 151 |
+
progress(sum(1 for r in results.values() if "Status: 200" in r) / len(ENDPOINTS),
|
| 152 |
+
desc=f"Waking up servers ({sum(1 for r in results.values() if r != 'Pending...')} of {len(ENDPOINTS)} processed)")
|
| 153 |
+
# Yield the updated status to show in real-time
|
| 154 |
+
yield status_html
|
| 155 |
+
except Exception as e:
|
| 156 |
+
print(f"Error processing {name}: {str(e)}")
|
| 157 |
+
results[name] = f"Error: Internal processing error"
|
| 158 |
+
yield update_status_html()
|
| 159 |
|
| 160 |
+
# Final update after all are complete
|
| 161 |
progress(1.0, desc="Complete!")
|
| 162 |
+
yield update_status_html()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
def process_job_description(job_description: str) -> Dict:
|
| 165 |
"""Process job description and extract skills using the job endpoint"""
|
|
|
|
| 705 |
gr.Markdown("# Beyond Keywords: Job Description and Resume Analyzer")
|
| 706 |
gr.Markdown("Upload a job description and resume(s) to analyze skill matches and quality.")
|
| 707 |
|
| 708 |
+
wake_btn = gr.Button("Wake Servers (Do this first!) - Might take a 1-2 minutes.")
|
| 709 |
+
wake_status = gr.HTML(label="Server Status", value="<div style='color: #666;'>Click 'Wake Servers'<br>to initialize the system...</div>")
|
| 710 |
+
wake_btn.click(fn=wake_servers, inputs=None, outputs=wake_status)
|
| 711 |
gr.Markdown("""<div style="height: 20px;"></div>""")
|
| 712 |
|
| 713 |
with gr.Row():
|