Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,11 @@
|
|
| 2 |
|
| 3 |
# https://www.gradio.app/guides/quickstart
|
| 4 |
|
| 5 |
-
import gradio.utils, os
|
| 6 |
import gradio as gr
|
| 7 |
from agents.crew import run_crew
|
|
|
|
|
|
|
| 8 |
from utils.utils import (
|
| 9 |
DATASET_TYPE_GAIA,
|
| 10 |
DATASET_TYPE_HLE,
|
|
@@ -17,6 +19,21 @@ SPACE_ID = os.environ.get("SPACE_ID")
|
|
| 17 |
|
| 18 |
BASE_URL = f"https://huggingface.co/spaces/{SPACE_ID}/blob/main"
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# MCP server functions
|
| 21 |
|
| 22 |
def ask(question, openai_api_key, gemini_api_key, anthropic_api_key, file_name = ""):
|
|
@@ -31,23 +48,23 @@ def ask(question, openai_api_key, gemini_api_key, anthropic_api_key, file_name =
|
|
| 31 |
file_name (str): Optional file name
|
| 32 |
|
| 33 |
Returns:
|
| 34 |
-
|
| 35 |
"""
|
| 36 |
if not question:
|
| 37 |
gr.Warning("Question is required.")
|
| 38 |
-
return ""
|
| 39 |
|
| 40 |
if not openai_api_key:
|
| 41 |
gr.Warning("OpenAI API Key is required.")
|
| 42 |
-
return ""
|
| 43 |
|
| 44 |
if not gemini_api_key:
|
| 45 |
gr.Warning("Gemini API Key is required.")
|
| 46 |
-
return ""
|
| 47 |
|
| 48 |
if not anthropic_api_key:
|
| 49 |
gr.Warning("Anthropic API Key is required.")
|
| 50 |
-
return ""
|
| 51 |
|
| 52 |
if file_name:
|
| 53 |
file_name = f"files/{file_name}"
|
|
@@ -63,10 +80,18 @@ def ask(question, openai_api_key, gemini_api_key, anthropic_api_key, file_name =
|
|
| 63 |
os.environ["GEMINI_API_KEY"] = gemini_key
|
| 64 |
os.environ["ANTHROPIC_API_KEY"] = anthropic_key
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
except Exception as e:
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
# Helper functions
|
| 72 |
|
|
@@ -89,7 +114,9 @@ DESCRIPTION = (
|
|
| 89 |
f"The system can solve multiple <a href='https://arxiv.org/pdf/2311.12983'>GAIA Benchmark</a> "
|
| 90 |
f"Level 1, 2, 3 and even <a href='https://arxiv.org/pdf/2501.14249'>Humanity's Last Exam</a> "
|
| 91 |
f"problems. To get started, select from the examples below. "
|
| 92 |
-
f"
|
|
|
|
|
|
|
| 93 |
f"<a href='{BASE_URL}/README.md'>Documentation</a></p>"
|
| 94 |
)
|
| 95 |
|
|
@@ -138,22 +165,22 @@ with gr.Blocks(elem_classes=["full-width-app"]) as gaia:
|
|
| 138 |
label="Question *",
|
| 139 |
value=DEFAULT_QUESTION,
|
| 140 |
interactive=True,
|
| 141 |
-
lines=
|
| 142 |
-
max_lines=
|
| 143 |
)
|
| 144 |
with gr.Row():
|
| 145 |
ground_truth = gr.Textbox(
|
| 146 |
label="Ground Truth",
|
| 147 |
value=DEFAULT_GROUND_TRUTH,
|
| 148 |
interactive=True,
|
| 149 |
-
lines=
|
| 150 |
-
max_lines=
|
| 151 |
)
|
| 152 |
file_name = gr.Textbox(
|
| 153 |
label="File Name",
|
| 154 |
interactive=True,
|
| 155 |
-
lines=
|
| 156 |
-
max_lines=
|
| 157 |
scale=2
|
| 158 |
)
|
| 159 |
file_link = gr.HTML(
|
|
@@ -182,22 +209,32 @@ with gr.Blocks(elem_classes=["full-width-app"]) as gaia:
|
|
| 182 |
interactive=True
|
| 183 |
)
|
| 184 |
with gr.Row():
|
| 185 |
-
clear_btn = gr.ClearButton(
|
| 186 |
-
components=[question, ground_truth, file_name, file_link]
|
| 187 |
-
)
|
| 188 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 189 |
with gr.Column(scale=1):
|
| 190 |
answer = gr.Textbox(
|
| 191 |
label="Answer",
|
| 192 |
interactive=False,
|
| 193 |
-
lines=
|
| 194 |
-
max_lines=
|
| 195 |
)
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
submit_btn.click(
|
| 198 |
fn=ask,
|
| 199 |
inputs=[question, openai_api_key, gemini_api_key, anthropic_api_key, file_name],
|
| 200 |
-
outputs=answer
|
| 201 |
)
|
| 202 |
|
| 203 |
file_name.change(
|
|
@@ -211,7 +248,7 @@ with gr.Blocks(elem_classes=["full-width-app"]) as gaia:
|
|
| 211 |
gr.Examples(
|
| 212 |
examples=get_dataset(DATASET_TYPE_GAIA, 1),
|
| 213 |
inputs=[question, ground_truth, file_name, "", "", ""],
|
| 214 |
-
examples_per_page=
|
| 215 |
cache_examples=False
|
| 216 |
)
|
| 217 |
|
|
@@ -219,7 +256,7 @@ with gr.Blocks(elem_classes=["full-width-app"]) as gaia:
|
|
| 219 |
gr.Examples(
|
| 220 |
examples=get_dataset(DATASET_TYPE_GAIA, 2),
|
| 221 |
inputs=[question, ground_truth, file_name, "", "", ""],
|
| 222 |
-
examples_per_page=
|
| 223 |
cache_examples=False
|
| 224 |
)
|
| 225 |
|
|
@@ -227,7 +264,7 @@ with gr.Blocks(elem_classes=["full-width-app"]) as gaia:
|
|
| 227 |
gr.Examples(
|
| 228 |
examples=get_dataset(DATASET_TYPE_GAIA, 3),
|
| 229 |
inputs=[question, ground_truth, file_name, "", "", ""],
|
| 230 |
-
examples_per_page=
|
| 231 |
cache_examples=False
|
| 232 |
)
|
| 233 |
|
|
@@ -235,7 +272,7 @@ with gr.Blocks(elem_classes=["full-width-app"]) as gaia:
|
|
| 235 |
gr.Examples(
|
| 236 |
examples=get_dataset(DATASET_TYPE_HLE, 0),
|
| 237 |
inputs=[question, ground_truth, file_name, "", "", ""],
|
| 238 |
-
examples_per_page=
|
| 239 |
cache_examples=False
|
| 240 |
)
|
| 241 |
|
|
|
|
| 2 |
|
| 3 |
# https://www.gradio.app/guides/quickstart
|
| 4 |
|
| 5 |
+
import gradio.utils, os, sys
|
| 6 |
import gradio as gr
|
| 7 |
from agents.crew import run_crew
|
| 8 |
+
from contextlib import contextmanager
|
| 9 |
+
from io import StringIO
|
| 10 |
from utils.utils import (
|
| 11 |
DATASET_TYPE_GAIA,
|
| 12 |
DATASET_TYPE_HLE,
|
|
|
|
| 19 |
|
| 20 |
BASE_URL = f"https://huggingface.co/spaces/{SPACE_ID}/blob/main"
|
| 21 |
|
| 22 |
+
# Console output capture
|
| 23 |
+
|
| 24 |
+
@contextmanager
|
| 25 |
+
def capture_output():
|
| 26 |
+
old_stdout = sys.stdout
|
| 27 |
+
old_stderr = sys.stderr
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
sys.stdout = StringIO()
|
| 31 |
+
sys.stderr = StringIO()
|
| 32 |
+
yield sys.stdout, sys.stderr
|
| 33 |
+
finally:
|
| 34 |
+
sys.stdout = old_stdout
|
| 35 |
+
sys.stderr = old_stderr
|
| 36 |
+
|
| 37 |
# MCP server functions
|
| 38 |
|
| 39 |
def ask(question, openai_api_key, gemini_api_key, anthropic_api_key, file_name = ""):
|
|
|
|
| 48 |
file_name (str): Optional file name
|
| 49 |
|
| 50 |
Returns:
|
| 51 |
+
tuple: (answer, console_logs) - The answer to the question and captured console output
|
| 52 |
"""
|
| 53 |
if not question:
|
| 54 |
gr.Warning("Question is required.")
|
| 55 |
+
return "", ""
|
| 56 |
|
| 57 |
if not openai_api_key:
|
| 58 |
gr.Warning("OpenAI API Key is required.")
|
| 59 |
+
return "", ""
|
| 60 |
|
| 61 |
if not gemini_api_key:
|
| 62 |
gr.Warning("Gemini API Key is required.")
|
| 63 |
+
return "", ""
|
| 64 |
|
| 65 |
if not anthropic_api_key:
|
| 66 |
gr.Warning("Anthropic API Key is required.")
|
| 67 |
+
return "", ""
|
| 68 |
|
| 69 |
if file_name:
|
| 70 |
file_name = f"files/{file_name}"
|
|
|
|
| 80 |
os.environ["GEMINI_API_KEY"] = gemini_key
|
| 81 |
os.environ["ANTHROPIC_API_KEY"] = anthropic_key
|
| 82 |
|
| 83 |
+
with capture_output() as (stdout, stderr):
|
| 84 |
+
answer = run_crew(question, file_name)
|
| 85 |
+
|
| 86 |
+
stdout_logs = stdout.getvalue()
|
| 87 |
+
stderr_logs = stderr.getvalue()
|
| 88 |
+
console_logs = stdout_logs + (stderr_logs if stderr_logs else "")
|
| 89 |
+
|
| 90 |
+
return answer, console_logs
|
| 91 |
except Exception as e:
|
| 92 |
+
error_msg = str(e)
|
| 93 |
+
gr.Warning(error_msg)
|
| 94 |
+
return "", f"Error: {error_msg}"
|
| 95 |
|
| 96 |
# Helper functions
|
| 97 |
|
|
|
|
| 114 |
f"The system can solve multiple <a href='https://arxiv.org/pdf/2311.12983'>GAIA Benchmark</a> "
|
| 115 |
f"Level 1, 2, 3 and even <a href='https://arxiv.org/pdf/2501.14249'>Humanity's Last Exam</a> "
|
| 116 |
f"problems. To get started, select from the examples below. "
|
| 117 |
+
f"Processing can take minutes depending on question complexity. "
|
| 118 |
+
f"Console logs are provided below for transparency. "
|
| 119 |
+
f"API keys are provided temporarily thanks to sponsor credit. "
|
| 120 |
f"<a href='{BASE_URL}/README.md'>Documentation</a></p>"
|
| 121 |
)
|
| 122 |
|
|
|
|
| 165 |
label="Question *",
|
| 166 |
value=DEFAULT_QUESTION,
|
| 167 |
interactive=True,
|
| 168 |
+
lines=1,
|
| 169 |
+
max_lines=1
|
| 170 |
)
|
| 171 |
with gr.Row():
|
| 172 |
ground_truth = gr.Textbox(
|
| 173 |
label="Ground Truth",
|
| 174 |
value=DEFAULT_GROUND_TRUTH,
|
| 175 |
interactive=True,
|
| 176 |
+
lines=1,
|
| 177 |
+
max_lines=1
|
| 178 |
)
|
| 179 |
file_name = gr.Textbox(
|
| 180 |
label="File Name",
|
| 181 |
interactive=True,
|
| 182 |
+
lines=1,
|
| 183 |
+
max_lines=1,
|
| 184 |
scale=2
|
| 185 |
)
|
| 186 |
file_link = gr.HTML(
|
|
|
|
| 209 |
interactive=True
|
| 210 |
)
|
| 211 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
| 212 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 213 |
with gr.Column(scale=1):
|
| 214 |
answer = gr.Textbox(
|
| 215 |
label="Answer",
|
| 216 |
interactive=False,
|
| 217 |
+
lines=1,
|
| 218 |
+
max_lines=1
|
| 219 |
)
|
| 220 |
|
| 221 |
+
with gr.Accordion("Console Logs", open=False):
|
| 222 |
+
console_logs = gr.Textbox(
|
| 223 |
+
label="Output",
|
| 224 |
+
interactive=False,
|
| 225 |
+
lines=25,
|
| 226 |
+
max_lines=25
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
with gr.Row():
|
| 230 |
+
clear_btn = gr.ClearButton(
|
| 231 |
+
components=[question, ground_truth, file_name, file_link, answer, console_logs]
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
submit_btn.click(
|
| 235 |
fn=ask,
|
| 236 |
inputs=[question, openai_api_key, gemini_api_key, anthropic_api_key, file_name],
|
| 237 |
+
outputs=[answer, console_logs]
|
| 238 |
)
|
| 239 |
|
| 240 |
file_name.change(
|
|
|
|
| 248 |
gr.Examples(
|
| 249 |
examples=get_dataset(DATASET_TYPE_GAIA, 1),
|
| 250 |
inputs=[question, ground_truth, file_name, "", "", ""],
|
| 251 |
+
examples_per_page=5,
|
| 252 |
cache_examples=False
|
| 253 |
)
|
| 254 |
|
|
|
|
| 256 |
gr.Examples(
|
| 257 |
examples=get_dataset(DATASET_TYPE_GAIA, 2),
|
| 258 |
inputs=[question, ground_truth, file_name, "", "", ""],
|
| 259 |
+
examples_per_page=5,
|
| 260 |
cache_examples=False
|
| 261 |
)
|
| 262 |
|
|
|
|
| 264 |
gr.Examples(
|
| 265 |
examples=get_dataset(DATASET_TYPE_GAIA, 3),
|
| 266 |
inputs=[question, ground_truth, file_name, "", "", ""],
|
| 267 |
+
examples_per_page=5,
|
| 268 |
cache_examples=False
|
| 269 |
)
|
| 270 |
|
|
|
|
| 272 |
gr.Examples(
|
| 273 |
examples=get_dataset(DATASET_TYPE_HLE, 0),
|
| 274 |
inputs=[question, ground_truth, file_name, "", "", ""],
|
| 275 |
+
examples_per_page=5,
|
| 276 |
cache_examples=False
|
| 277 |
)
|
| 278 |
|