Upload src/demo/app.py with huggingface_hub
Browse files- src/demo/app.py +41 -8
src/demo/app.py
CHANGED
|
@@ -766,6 +766,27 @@ def mock_frontier_response(scenario: dict) -> tuple[str, str]:
|
|
| 766 |
return response, wrong_name
|
| 767 |
|
| 768 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 769 |
# ---------------------------------------------------------------------------
|
| 770 |
# Core demo functions
|
| 771 |
# ---------------------------------------------------------------------------
|
|
@@ -865,18 +886,30 @@ def run_comparison(narrative: str, question: str, scenario_json: str, provider:
|
|
| 865 |
agent_answer = ""
|
| 866 |
agent_hops = 0
|
| 867 |
try:
|
| 868 |
-
#
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 875 |
# Fall back to Gemini API
|
| 876 |
from src.agent.rlm_agent import make_gemini_generate_fn
|
| 877 |
generate_fn = make_gemini_generate_fn("gemini-2.5-flash")
|
|
|
|
| 878 |
print("[run_comparison] Using Gemini 2.5 Flash API (no GPU)")
|
| 879 |
-
result = run_agent(generate_fn, tools, narrative, question, hop_budget=6)
|
| 880 |
agent_answer = result.answer or "No answer"
|
| 881 |
agent_hops = result.hops
|
| 882 |
|
|
|
|
| 766 |
return response, wrong_name
|
| 767 |
|
| 768 |
|
| 769 |
+
# ---------------------------------------------------------------------------
|
| 770 |
+
# ZeroGPU inference — decorated with @spaces.GPU when available
|
| 771 |
+
# ---------------------------------------------------------------------------
|
| 772 |
+
|
| 773 |
+
def _gpu_agent_inference_impl(narrative: str, question: str, scenario: dict):
|
| 774 |
+
"""Run the fine-tuned model on ZeroGPU. Called within a @spaces.GPU context."""
|
| 775 |
+
from src.demo.model_loader import make_local_generate_fn
|
| 776 |
+
from src.graph.kg_tools import RdflibTools
|
| 777 |
+
generate_fn = make_local_generate_fn()
|
| 778 |
+
graph = build_rdflib_graph(scenario)
|
| 779 |
+
tools = RdflibTools(graph)
|
| 780 |
+
return run_agent(generate_fn, tools, narrative, question, hop_budget=6)
|
| 781 |
+
|
| 782 |
+
# Try to apply @spaces.GPU decorator if available
|
| 783 |
+
try:
|
| 784 |
+
import spaces
|
| 785 |
+
_gpu_agent_inference = spaces.GPU(duration=300)(_gpu_agent_inference_impl)
|
| 786 |
+
except ImportError:
|
| 787 |
+
_gpu_agent_inference = _gpu_agent_inference_impl
|
| 788 |
+
|
| 789 |
+
|
| 790 |
# ---------------------------------------------------------------------------
|
| 791 |
# Core demo functions
|
| 792 |
# ---------------------------------------------------------------------------
|
|
|
|
| 886 |
agent_answer = ""
|
| 887 |
agent_hops = 0
|
| 888 |
try:
|
| 889 |
+
# Check if running on HF Spaces with ZeroGPU
|
| 890 |
+
use_local_model = False
|
| 891 |
+
try:
|
| 892 |
+
import spaces
|
| 893 |
+
# On ZeroGPU Spaces — use the decorated GPU function
|
| 894 |
+
result = _gpu_agent_inference(narrative, question, scenario)
|
| 895 |
+
use_local_model = True
|
| 896 |
+
print("[run_comparison] Using ZeroGPU fine-tuned Gemma 4B")
|
| 897 |
+
except ImportError:
|
| 898 |
+
# Not on Spaces — check for local GPU
|
| 899 |
+
import torch
|
| 900 |
+
if torch.cuda.is_available():
|
| 901 |
+
from src.demo.model_loader import make_local_generate_fn
|
| 902 |
+
generate_fn = make_local_generate_fn()
|
| 903 |
+
result = run_agent(generate_fn, tools, narrative, question, hop_budget=6)
|
| 904 |
+
use_local_model = True
|
| 905 |
+
print("[run_comparison] Using LOCAL fine-tuned Gemma 4B")
|
| 906 |
+
|
| 907 |
+
if not use_local_model:
|
| 908 |
# Fall back to Gemini API
|
| 909 |
from src.agent.rlm_agent import make_gemini_generate_fn
|
| 910 |
generate_fn = make_gemini_generate_fn("gemini-2.5-flash")
|
| 911 |
+
result = run_agent(generate_fn, tools, narrative, question, hop_budget=6)
|
| 912 |
print("[run_comparison] Using Gemini 2.5 Flash API (no GPU)")
|
|
|
|
| 913 |
agent_answer = result.answer or "No answer"
|
| 914 |
agent_hops = result.hops
|
| 915 |
|