Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,38 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
|
|
|
|
|
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
from code_agent import run_agent
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def respond(prompt: str):
|
| 15 |
-
|
|
|
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
return ans
|
| 19 |
except Exception as e:
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
demo = gr.Interface(
|
|
@@ -29,5 +44,22 @@ demo = gr.Interface(
|
|
| 29 |
)
|
| 30 |
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
if __name__ == "__main__":
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
Gradio interface for the smolagents CodeAgent.
|
| 3 |
|
| 4 |
+
By default this script will NOT launch a local Gradio server. To allow
|
| 5 |
+
local runs (for testing) set the environment variable `RUN_LOCAL=1` or pass
|
| 6 |
+
`--run-local` on the command line. This prevents accidental local launches
|
| 7 |
+
when you intended to deploy to Hugging Face Spaces.
|
| 8 |
+
|
| 9 |
+
Run locally (explicit):
|
| 10 |
+
RUN_LOCAL=1 python app.py
|
| 11 |
+
or
|
| 12 |
+
python app.py --run-local
|
| 13 |
+
|
| 14 |
+
When running in a Space you can set `RUN_LOCAL=1` in Space secrets if desired,
|
| 15 |
+
but Spaces typically run the app directly; set `RUN_LOCAL` only when you want
|
| 16 |
+
explicit local behavior.
|
| 17 |
"""
|
| 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 |
+
|
| 27 |
|
| 28 |
+
def respond(prompt: str) -> str:
|
| 29 |
+
if not prompt or not prompt.strip():
|
| 30 |
+
return "Please provide a prompt."
|
| 31 |
try:
|
| 32 |
+
return run_agent(prompt)
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
+
logger.error("Agent failed: %s", e)
|
| 35 |
+
return f"Agent error: {type(e).__name__}: {str(e)[:200]}"
|
| 36 |
|
| 37 |
|
| 38 |
demo = gr.Interface(
|
|
|
|
| 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 |
+
)
|