Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,25 +5,13 @@ 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 |
from code_agent import run_agent
|
| 22 |
|
| 23 |
-
#
|
| 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 |
|
|
@@ -55,43 +43,54 @@ def _get_demo():
|
|
| 55 |
logger.error("Failed to import gradio: %s", e)
|
| 56 |
raise
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 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 |
-
)
|
|
|
|
| 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 |
import os
|
| 10 |
import sys
|
| 11 |
import logging
|
| 12 |
from code_agent import run_agent
|
| 13 |
|
| 14 |
+
# (Gradio update/analytics env flags removed per user request)
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
|
|
|
|
| 43 |
logger.error("Failed to import gradio: %s", e)
|
| 44 |
raise
|
| 45 |
|
| 46 |
+
# We'll provide a readonly prompt field populated from the scoring API
|
| 47 |
+
# and buttons to fetch a random task and run the agent on it.
|
| 48 |
+
def fetch_random_task():
|
| 49 |
+
try:
|
| 50 |
+
from evaluation_client import ScoringAPIClient
|
| 51 |
+
client = ScoringAPIClient()
|
| 52 |
+
q = client.get_random_question()
|
| 53 |
+
if not q:
|
| 54 |
+
return "(no question returned)"
|
| 55 |
+
# extract prompt safely
|
| 56 |
+
for key in ("question", "prompt", "input", "text", "task"):
|
| 57 |
+
if key in q and isinstance(q[key], str):
|
| 58 |
+
return q[key]
|
| 59 |
+
return str(q)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
logger.error("Failed to fetch random question: %s", e)
|
| 62 |
+
return f"(fetch error) {type(e).__name__}: {str(e)[:200]}"
|
| 63 |
|
| 64 |
+
def run_on_current(prompt_text: str):
|
| 65 |
+
if not prompt_text or not prompt_text.strip():
|
| 66 |
+
return "(no prompt to run on)"
|
| 67 |
+
try:
|
| 68 |
+
return respond(prompt_text)
|
| 69 |
+
except Exception as e:
|
| 70 |
+
logger.error("Agent run failed: %s", e)
|
| 71 |
+
return f"Agent error: {type(e).__name__}: {str(e)[:200]}"
|
| 72 |
+
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("# Agents Course — Final Agent Demo")
|
| 75 |
+
with gr.Row():
|
| 76 |
+
prompt_box = gr.Textbox(label="Fetched Prompt (read-only)", lines=6)
|
| 77 |
+
with gr.Row():
|
| 78 |
+
fetch_btn = gr.Button("Fetch Random Task")
|
| 79 |
+
run_btn = gr.Button("Run Agent on Fetched Task")
|
| 80 |
+
output_box = gr.Textbox(label="Agent Response", lines=12)
|
| 81 |
+
|
| 82 |
+
fetch_btn.click(fn=fetch_random_task, inputs=[], outputs=[prompt_box])
|
| 83 |
+
run_btn.click(fn=run_on_current, inputs=[prompt_box], outputs=[output_box])
|
| 84 |
+
|
| 85 |
+
_demo = demo
|
| 86 |
+
return _demo
|
| 87 |
|
|
|
|
|
|
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
+
# Launch unconditionally when executed as a script.
|
| 93 |
+
try:
|
| 94 |
+
_get_demo().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
logger.error("Failed to launch Gradio demo: %s", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|