alfulanny commited on
Commit
c44e5a2
·
verified ·
1 Parent(s): 9d7a209

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -19
app.py CHANGED
@@ -18,9 +18,15 @@ explicit local behavior.
18
  import os
19
  import sys
20
  import logging
21
- import gradio as gr
22
  from code_agent import run_agent
23
 
 
 
 
 
 
 
 
24
  logger = logging.getLogger(__name__)
25
  logging.basicConfig(level=logging.INFO)
26
 
@@ -35,31 +41,57 @@ def respond(prompt: str) -> str:
35
  return f"Agent error: {type(e).__name__}: {str(e)[:200]}"
36
 
37
 
38
- demo = gr.Interface(
39
- fn=respond,
40
- inputs=gr.Textbox(lines=5, label="User Prompt"),
41
- outputs=gr.Textbox(lines=10, label="Agent Response"),
42
- title="Agents Course Final Agent Demo",
43
- description="smolagents CodeAgent demo for the course final project.",
44
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
 
47
- def _should_launch_local() -> bool:
48
- # Explicit CLI flag takes precedence
49
- if "--run-local" in sys.argv:
 
 
 
 
 
 
 
 
50
  return True
51
- # Environment variable opt-in
52
- if os.environ.get("RUN_LOCAL", "0") == "1":
53
  return True
54
  return False
55
 
56
 
57
  if __name__ == "__main__":
58
- if _should_launch_local():
59
- # Launch only when explicitly allowed
60
- logger.info("Launching Gradio locally (RUN_LOCAL=1 or --run-local detected)")
61
- demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
 
 
 
62
  else:
63
- logger.warning(
64
- "Local Gradio launch prevented. To run locally set RUN_LOCAL=1 or pass --run-local."
65
  )
 
18
  import os
19
  import sys
20
  import logging
 
21
  from code_agent import run_agent
22
 
23
+ # Prevent Gradio from contacting its update/analytics endpoints on import.
24
+ # These environment variables should be set before importing `gradio`.
25
+ os.environ.setdefault("GRADIO_CHECK_FOR_UPDATES", "false")
26
+ os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "false")
27
+
28
+
29
+
30
  logger = logging.getLogger(__name__)
31
  logging.basicConfig(level=logging.INFO)
32
 
 
41
  return f"Agent error: {type(e).__name__}: {str(e)[:200]}"
42
 
43
 
44
+ _demo = None
45
+
46
+
47
+ def _get_demo():
48
+ """Lazily import gradio and construct the demo to avoid network calls on import."""
49
+ global _demo
50
+ if _demo is not None:
51
+ return _demo
52
+ try:
53
+ import gradio as gr
54
+ except Exception as e:
55
+ logger.error("Failed to import gradio: %s", e)
56
+ raise
57
+
58
+ _demo = gr.Interface(
59
+ fn=respond,
60
+ inputs=gr.Textbox(lines=5, label="User Prompt"),
61
+ outputs=gr.Textbox(lines=10, label="Agent Response"),
62
+ title="Agents Course — Final Agent Demo",
63
+ description="smolagents CodeAgent demo for the course final project.",
64
+ )
65
+ return _demo
66
 
67
 
68
+ def _should_launch_in_space() -> bool:
69
+ """Decide whether to launch inside a Hugging Face Space or similar environment.
70
+
71
+ We intentionally DO NOT support local launches. To run in a Space, set one of:
72
+ - environment variable `RUN_IN_SPACE=1`, or
73
+ - environment variable `HF_SPACE` (any value), or
74
+ - environment variable `SPACE_ID` (any value)
75
+ """
76
+ if os.environ.get("RUN_IN_SPACE", "0") == "1":
77
+ return True
78
+ if os.environ.get("HF_SPACE"):
79
  return True
80
+ if os.environ.get("SPACE_ID"):
 
81
  return True
82
  return False
83
 
84
 
85
  if __name__ == "__main__":
86
+ # Never launch locally. Only allow launch when explicitly running inside
87
+ # a Space or when RUN_IN_SPACE is set. This prevents accidental local runs.
88
+ if _should_launch_in_space():
89
+ logger.info("Launching Gradio in Space mode")
90
+ try:
91
+ _get_demo().launch(server_name="0.0.0.0", server_port=7860, share=False)
92
+ except Exception as e:
93
+ logger.error("Failed to launch Gradio demo in Space mode: %s", e)
94
  else:
95
+ logger.error(
96
+ "Local launch disabled. To run the app in a Space set RUN_IN_SPACE=1 or set HF_SPACE/SPACE_ID in the environment."
97
  )