Nymbo commited on
Commit
4c82165
·
verified ·
1 Parent(s): d9e3bb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -46
app.py CHANGED
@@ -6,6 +6,7 @@
6
  # 5) Memory Manager — lightweight JSON-based local memory store
7
  # 6) Image Generation - HF serverless inference providers (requires HF_READ_TOKEN)
8
  # 7) Video Generation - HF serverless inference providers (requires HF_READ_TOKEN)
 
9
 
10
  from __future__ import annotations
11
 
@@ -514,42 +515,6 @@ def _log_call_end(func_name: str, output_desc: str) -> None:
514
  print(f"[TOOL RESULT] {func_name} (failed to log output: {e})", flush=True)
515
 
516
 
517
- # ==============================
518
- # HF Space file URL helper
519
- # ==============================
520
-
521
- def _public_file_url(local_path: str) -> str:
522
- """Return an absolute Hugging Face Space URL for a locally saved file.
523
-
524
- This ensures that download links point to the Space host (SPACE_HOST) rather than any
525
- reverse-proxy origin (e.g., mcp.nymbo.net), avoiding 404s when proxies don’t expose /file=.
526
-
527
- If SPACE_HOST is not set (e.g., running locally), falls back to a relative "/file=" URL.
528
- """
529
- try:
530
- from urllib.parse import quote
531
- host = os.getenv("SPACE_HOST", "").strip()
532
- abs_path = os.path.abspath(local_path)
533
-
534
- # Detect Windows absolute path (e.g., C:\...) or running on Windows
535
- is_windows_path = bool(re.match(r"^[a-zA-Z]:\\", abs_path)) or os.name == "nt"
536
- is_posix_abs = abs_path.startswith("/")
537
-
538
- # Only construct an absolute hf.space URL when:
539
- # - We have a SPACE_HOST
540
- # - The file path is POSIX-absolute (we're running inside the Space container)
541
- if host and is_posix_abs and not is_windows_path:
542
- quoted = quote(abs_path)
543
- return f"https://{host}/file={quoted}"
544
-
545
- # Otherwise, prefer returning the raw path and let Gradio serve it in the same origin
546
- # (UI components like gr.File/gr.Video handle this).
547
- return local_path
548
- except Exception:
549
- # Last-resort: return the original path to avoid breaking UI
550
- return local_path
551
-
552
-
553
  # ==============================
554
  # Deep Research helpers: slow-host detection
555
  # ==============================
@@ -1812,7 +1777,7 @@ kokoro_interface = gr.Interface(
1812
  "Supports unlimited text length by processing all segments. Voice examples: 'af_heart' (US female), 'am_onyx' (US male), "
1813
  "'bf_emma' (British female), 'af_sky' (US female), 'af_nicole' (US female), "
1814
  "Parameters: text (str), speed (float 0.5–2.0, default 1.25x), voice (str from 54 available options, default 'af_heart'). "
1815
- "Return the generated media to the user in this format `[Generated Speech](URL)`"
1816
  ),
1817
  flagging_mode="never",
1818
  )
@@ -2200,10 +2165,8 @@ def Generate_Video( # <-- MCP tool #6 (Generate Video)
2200
  size = os.path.getsize(path)
2201
  except Exception:
2202
  size = -1
2203
- # Prefer HF Space absolute URL when running in a Space, otherwise return local path
2204
- url = _public_file_url(path)
2205
- _log_call_end("Generate_Video", f"provider={provider} path={os.path.basename(path)} bytes={size} url={url}")
2206
- return url
2207
  except Exception as e:
2208
  last_error = e
2209
  continue
@@ -2249,7 +2212,7 @@ video_generation_interface = gr.Interface(
2249
  "'time-lapse of clouds moving across a blue sky'. Default model: Wan2.2-T2V-A14B (2-6 second videos). "
2250
  "Parameters: prompt (str), model_id (str), negative_prompt (str), steps (int), cfg_scale (float), seed (int), "
2251
  "width/height (int), fps (int), duration (float in seconds). Returns MP4 file path. "
2252
- "Return the generated media to the user in this format `[Generated Video](URL)`"
2253
  ),
2254
  flagging_mode="never",
2255
  # Only expose to MCP when HF token is provided; UI tab is always visible
@@ -2741,8 +2704,6 @@ def Deep_Research(
2741
  # Print explicit timing and include in structured log output
2742
  print(f"[TIMING] Deep_Research elapsed: {elapsed:.2f}s", flush=True)
2743
  _log_call_end("Deep_Research", f"urls={len(pages)} file={os.path.basename(file_path)} duration={elapsed:.2f}s")
2744
- # Keep return signature for existing UI (report, links, file path). MCP clients can prefer the URL by
2745
- # reading the api_description instruction or deriving it from the path using the same rule.
2746
  return report, links_text, file_path
2747
 
2748
 
@@ -2775,8 +2736,7 @@ deep_research_interface = gr.Interface(
2775
  "Runs 1–5 DDG searches (URLs only), caps total results to 50 (when exceeding, each query returns 10). "
2776
  "Fetches all URLs (3000 chars each) and calls the Researcher to write a research report. "
2777
  "Returns the report (Markdown), the list of sources, and a downloadable text file path. "
2778
- "When sharing a link, always use the Space URL constructed as: https://{SPACE_HOST}/file=<abs_path> (we handle this automatically). "
2779
- "Provide the user with one-paragraph summary of the research report and the txt file in this format `[research_report.txt](URL)`"
2780
  ),
2781
  flagging_mode="never",
2782
  show_api=bool(HF_TEXTGEN_TOKEN),
 
6
  # 5) Memory Manager — lightweight JSON-based local memory store
7
  # 6) Image Generation - HF serverless inference providers (requires HF_READ_TOKEN)
8
  # 7) Video Generation - HF serverless inference providers (requires HF_READ_TOKEN)
9
+ # 8) Deep Research
10
 
11
  from __future__ import annotations
12
 
 
515
  print(f"[TOOL RESULT] {func_name} (failed to log output: {e})", flush=True)
516
 
517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
  # ==============================
519
  # Deep Research helpers: slow-host detection
520
  # ==============================
 
1777
  "Supports unlimited text length by processing all segments. Voice examples: 'af_heart' (US female), 'am_onyx' (US male), "
1778
  "'bf_emma' (British female), 'af_sky' (US female), 'af_nicole' (US female), "
1779
  "Parameters: text (str), speed (float 0.5–2.0, default 1.25x), voice (str from 54 available options, default 'af_heart'). "
1780
+ "Return the generated media to the user in this format `![Alt text](URL)`"
1781
  ),
1782
  flagging_mode="never",
1783
  )
 
2165
  size = os.path.getsize(path)
2166
  except Exception:
2167
  size = -1
2168
+ _log_call_end("Generate_Video", f"provider={provider} path={os.path.basename(path)} bytes={size}")
2169
+ return path
 
 
2170
  except Exception as e:
2171
  last_error = e
2172
  continue
 
2212
  "'time-lapse of clouds moving across a blue sky'. Default model: Wan2.2-T2V-A14B (2-6 second videos). "
2213
  "Parameters: prompt (str), model_id (str), negative_prompt (str), steps (int), cfg_scale (float), seed (int), "
2214
  "width/height (int), fps (int), duration (float in seconds). Returns MP4 file path. "
2215
+ "Return the generated media to the user in this format `![Alt text](URL)`"
2216
  ),
2217
  flagging_mode="never",
2218
  # Only expose to MCP when HF token is provided; UI tab is always visible
 
2704
  # Print explicit timing and include in structured log output
2705
  print(f"[TIMING] Deep_Research elapsed: {elapsed:.2f}s", flush=True)
2706
  _log_call_end("Deep_Research", f"urls={len(pages)} file={os.path.basename(file_path)} duration={elapsed:.2f}s")
 
 
2707
  return report, links_text, file_path
2708
 
2709
 
 
2736
  "Runs 1–5 DDG searches (URLs only), caps total results to 50 (when exceeding, each query returns 10). "
2737
  "Fetches all URLs (3000 chars each) and calls the Researcher to write a research report. "
2738
  "Returns the report (Markdown), the list of sources, and a downloadable text file path. "
2739
+ "Provide the user with one-paragraph summary of the research report and the txt file in this format `![research_report.txt](URL)`"
 
2740
  ),
2741
  flagging_mode="never",
2742
  show_api=bool(HF_TEXTGEN_TOKEN),