Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,8 @@ import os
|
|
4 |
import gradio as gr
|
5 |
from dotenv import load_dotenv
|
6 |
|
|
|
|
|
7 |
from llm_interface import ERROR_503_DICT # Import error dict
|
8 |
from llm_interface import parse_qwen_response, query_qwen_endpoint
|
9 |
|
@@ -30,13 +32,14 @@ load_dotenv()
|
|
30 |
|
31 |
# --- Constants ---
|
32 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
33 |
DATASET_ID = "yjernite/spaces-privacy-reports"
|
34 |
CACHE_INFO_MSG = "\n\n*(Report retrieved from cache)*"
|
35 |
DEFAULT_SELECTION = "HuggingFaceTB/SmolVLM2"
|
36 |
|
37 |
TRUNCATION_WARNING = """**⚠️ Warning:** The input data (code and/or prior analysis) was too long for the AI model's context limit and had to be truncated. The analysis below may be incomplete or based on partial information.\n\n---\n\n"""
|
38 |
|
39 |
-
ERROR_503_USER_MESSAGE = """**
|
40 |
|
41 |
You have a few options:
|
42 |
|
@@ -237,9 +240,53 @@ def _run_live_analysis(space_id: str, progress=gr.Progress(track_tqdm=True)):
|
|
237 |
gr.update(visible=True, open=False),
|
238 |
)
|
239 |
|
240 |
-
# --- Step 2:
|
241 |
-
progress(2 / steps, desc="Step 2/8:
|
242 |
-
logging.info("Step 2/8:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
code_files = get_space_code_files(space_id)
|
244 |
if not code_files:
|
245 |
error_msg = f"Could not retrieve code files for '{space_id}'. Check ID and ensure it's a public Space."
|
@@ -252,11 +299,11 @@ def _run_live_analysis(space_id: str, progress=gr.Progress(track_tqdm=True)):
|
|
252 |
)
|
253 |
return # End generation on error
|
254 |
|
255 |
-
# --- Step
|
256 |
progress(
|
257 |
-
|
258 |
)
|
259 |
-
logging.info("Step
|
260 |
yield (
|
261 |
gr.update(value="Generating detailed privacy report...", visible=True),
|
262 |
gr.update(value="Generating detailed privacy report via AI...", visible=True),
|
@@ -307,9 +354,6 @@ def _run_live_analysis(space_id: str, progress=gr.Progress(track_tqdm=True)):
|
|
307 |
gr.update(visible=True, open=True),
|
308 |
)
|
309 |
|
310 |
-
# --- Step 4: Extract Model IDs ---
|
311 |
-
progress(4 / steps, desc="Step 4/8: Extracting model IDs...")
|
312 |
-
logging.info("Step 4/8: Extracting potential model IDs...")
|
313 |
|
314 |
# --- Step 5: Fetch Model Descriptions ---
|
315 |
progress(5 / steps, desc="Step 5/8: Fetching model descriptions...")
|
|
|
4 |
import gradio as gr
|
5 |
from dotenv import load_dotenv
|
6 |
|
7 |
+
from huggingface_hub import HfApi
|
8 |
+
|
9 |
from llm_interface import ERROR_503_DICT # Import error dict
|
10 |
from llm_interface import parse_qwen_response, query_qwen_endpoint
|
11 |
|
|
|
32 |
|
33 |
# --- Constants ---
|
34 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
35 |
+
ENDPOINT_NAME = "qwen2-5-coder-32b-instruct-pmf"
|
36 |
DATASET_ID = "yjernite/spaces-privacy-reports"
|
37 |
CACHE_INFO_MSG = "\n\n*(Report retrieved from cache)*"
|
38 |
DEFAULT_SELECTION = "HuggingFaceTB/SmolVLM2"
|
39 |
|
40 |
TRUNCATION_WARNING = """**⚠️ Warning:** The input data (code and/or prior analysis) was too long for the AI model's context limit and had to be truncated. The analysis below may be incomplete or based on partial information.\n\n---\n\n"""
|
41 |
|
42 |
+
ERROR_503_USER_MESSAGE = """**Service Unavailable**: It appears that the analysis model endpoint is currently down or starting up.
|
43 |
|
44 |
You have a few options:
|
45 |
|
|
|
240 |
gr.update(visible=True, open=False),
|
241 |
)
|
242 |
|
243 |
+
# --- Step 2: Check Endpoint Status ---
|
244 |
+
progress(2 / steps, desc="Step 2/8: Checking endpoint status...")
|
245 |
+
logging.info("Step 2/8: Checking endpoint status...")
|
246 |
+
yield (
|
247 |
+
gr.update(value="Checking whether model endpoint is active...", visible=True),
|
248 |
+
gr.update(value="", visible=True),
|
249 |
+
gr.update(visible=True, open=True),
|
250 |
+
gr.update(visible=True, open=False),
|
251 |
+
)
|
252 |
+
|
253 |
+
endpoint_ready = False
|
254 |
+
if HF_TOKEN and HF_INFERENCE_ENDPOINT_URL:
|
255 |
+
try:
|
256 |
+
api = HfApi(token=HF_TOKEN)
|
257 |
+
endpoint = api.get_inference_endpoint(name=ENDPOINT_NAME)
|
258 |
+
status = endpoint.status
|
259 |
+
|
260 |
+
logging.info(f"Endpoint '{ENDPOINT_NAME}' status: {status}")
|
261 |
+
|
262 |
+
if status == 'running':
|
263 |
+
endpoint_ready = True
|
264 |
+
else:
|
265 |
+
logging.warning(f"Endpoint '{ENDPOINT_NAME}' is not ready (Status: {status}).")
|
266 |
+
if status == 'scaledToZero':
|
267 |
+
logging.info(f"Endpoint '{ENDPOINT_NAME}' is scaled to zero. Attempting to resume...")
|
268 |
+
endpoint.resume()
|
269 |
+
msg_503 = f"The status of the Qwen2.5-Coder-32B-Instruct endpoint powering the analysis is currently: {status}\n\n" + ERROR_503_USER_MESSAGE
|
270 |
+
yield (
|
271 |
+
gr.update(value=msg_503, visible=True),
|
272 |
+
gr.update(value="", visible=False),
|
273 |
+
gr.update(visible=True, open=True),
|
274 |
+
gr.update(visible=False)
|
275 |
+
)
|
276 |
+
return # Stop analysis, user needs to retry
|
277 |
+
except Exception as e:
|
278 |
+
logging.error(f"Error checking endpoint status for {ENDPOINT_NAME}: {e}")
|
279 |
+
yield (
|
280 |
+
gr.update(value=f"Error checking analysis endpoint status: {e}", visible=True),
|
281 |
+
gr.update(value="", visible=False),
|
282 |
+
gr.update(visible=True, open=True),
|
283 |
+
gr.update(visible=False)
|
284 |
+
)
|
285 |
+
return # Stop analysis
|
286 |
+
|
287 |
+
# --- Step 3: Fetch Code Files (if not cached) ---
|
288 |
+
progress(3 / steps, desc="Step 3/8: Fetching code files...")
|
289 |
+
logging.info("Step 3/8: Fetching code files...")
|
290 |
code_files = get_space_code_files(space_id)
|
291 |
if not code_files:
|
292 |
error_msg = f"Could not retrieve code files for '{space_id}'. Check ID and ensure it's a public Space."
|
|
|
299 |
)
|
300 |
return # End generation on error
|
301 |
|
302 |
+
# --- Step 4: Generate DETAILED Privacy Report (LLM Call 1) ---
|
303 |
progress(
|
304 |
+
4 / steps, desc="Step 4/8: Generating detailed privacy report (AI Call 1)..."
|
305 |
)
|
306 |
+
logging.info("Step 4/8: Generating detailed privacy analysis report...")
|
307 |
yield (
|
308 |
gr.update(value="Generating detailed privacy report...", visible=True),
|
309 |
gr.update(value="Generating detailed privacy report via AI...", visible=True),
|
|
|
354 |
gr.update(visible=True, open=True),
|
355 |
)
|
356 |
|
|
|
|
|
|
|
357 |
|
358 |
# --- Step 5: Fetch Model Descriptions ---
|
359 |
progress(5 / steps, desc="Step 5/8: Fetching model descriptions...")
|