Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,9 @@ from utils.utils import (
|
|
| 10 |
DATASET_TYPE_HLE,
|
| 11 |
get_dataset
|
| 12 |
)
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Configuration
|
| 15 |
|
|
@@ -17,6 +20,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 +49,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 +81,20 @@ 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 |
|
|
@@ -183,9 +211,6 @@ with gr.Blocks(elem_classes=["full-width-app"]) as gaia:
|
|
| 183 |
interactive=True
|
| 184 |
)
|
| 185 |
with gr.Row():
|
| 186 |
-
clear_btn = gr.ClearButton(
|
| 187 |
-
components=[question, ground_truth, file_name, file_link]
|
| 188 |
-
)
|
| 189 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 190 |
with gr.Column(scale=1):
|
| 191 |
answer = gr.Textbox(
|
|
@@ -194,11 +219,27 @@ with gr.Blocks(elem_classes=["full-width-app"]) as gaia:
|
|
| 194 |
lines=2,
|
| 195 |
max_lines=5
|
| 196 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
submit_btn.click(
|
| 199 |
fn=ask,
|
| 200 |
inputs=[question, openai_api_key, gemini_api_key, anthropic_api_key, file_name],
|
| 201 |
-
outputs=answer
|
| 202 |
)
|
| 203 |
|
| 204 |
file_name.change(
|
|
|
|
| 10 |
DATASET_TYPE_HLE,
|
| 11 |
get_dataset
|
| 12 |
)
|
| 13 |
+
import sys
|
| 14 |
+
from io import StringIO
|
| 15 |
+
from contextlib import contextmanager
|
| 16 |
|
| 17 |
# Configuration
|
| 18 |
|
|
|
|
| 20 |
|
| 21 |
BASE_URL = f"https://huggingface.co/spaces/{SPACE_ID}/blob/main"
|
| 22 |
|
| 23 |
+
# Console output capture
|
| 24 |
+
|
| 25 |
+
@contextmanager
|
| 26 |
+
def capture_output():
|
| 27 |
+
"""Context manager to capture stdout and stderr"""
|
| 28 |
+
old_stdout = sys.stdout
|
| 29 |
+
old_stderr = sys.stderr
|
| 30 |
+
try:
|
| 31 |
+
sys.stdout = StringIO()
|
| 32 |
+
sys.stderr = StringIO()
|
| 33 |
+
yield sys.stdout, sys.stderr
|
| 34 |
+
finally:
|
| 35 |
+
sys.stdout = old_stdout
|
| 36 |
+
sys.stderr = old_stderr
|
| 37 |
+
|
| 38 |
# MCP server functions
|
| 39 |
|
| 40 |
def ask(question, openai_api_key, gemini_api_key, anthropic_api_key, file_name = ""):
|
|
|
|
| 49 |
file_name (str): Optional file name
|
| 50 |
|
| 51 |
Returns:
|
| 52 |
+
tuple: (answer, console_logs) - The answer to the question and captured console output
|
| 53 |
"""
|
| 54 |
if not question:
|
| 55 |
gr.Warning("Question is required.")
|
| 56 |
+
return "", ""
|
| 57 |
|
| 58 |
if not openai_api_key:
|
| 59 |
gr.Warning("OpenAI API Key is required.")
|
| 60 |
+
return "", ""
|
| 61 |
|
| 62 |
if not gemini_api_key:
|
| 63 |
gr.Warning("Gemini API Key is required.")
|
| 64 |
+
return "", ""
|
| 65 |
|
| 66 |
if not anthropic_api_key:
|
| 67 |
gr.Warning("Anthropic API Key is required.")
|
| 68 |
+
return "", ""
|
| 69 |
|
| 70 |
if file_name:
|
| 71 |
file_name = f"files/{file_name}"
|
|
|
|
| 81 |
os.environ["GEMINI_API_KEY"] = gemini_key
|
| 82 |
os.environ["ANTHROPIC_API_KEY"] = anthropic_key
|
| 83 |
|
| 84 |
+
# Capture console output
|
| 85 |
+
with capture_output() as (stdout, stderr):
|
| 86 |
+
answer = run_crew(question, file_name)
|
| 87 |
+
|
| 88 |
+
# Get captured logs
|
| 89 |
+
stdout_logs = stdout.getvalue()
|
| 90 |
+
stderr_logs = stderr.getvalue()
|
| 91 |
+
console_logs = stdout_logs + ("\n" + stderr_logs if stderr_logs else "")
|
| 92 |
+
|
| 93 |
+
return answer, console_logs
|
| 94 |
except Exception as e:
|
| 95 |
+
error_msg = str(e)
|
| 96 |
+
gr.Warning(error_msg)
|
| 97 |
+
return "", f"Error: {error_msg}"
|
| 98 |
|
| 99 |
# Helper functions
|
| 100 |
|
|
|
|
| 211 |
interactive=True
|
| 212 |
)
|
| 213 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
| 214 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 215 |
with gr.Column(scale=1):
|
| 216 |
answer = gr.Textbox(
|
|
|
|
| 219 |
lines=2,
|
| 220 |
max_lines=5
|
| 221 |
)
|
| 222 |
+
|
| 223 |
+
# Console logs section
|
| 224 |
+
with gr.Accordion("Console Logs & Tool Calls", open=False):
|
| 225 |
+
console_logs = gr.Textbox(
|
| 226 |
+
label="Console Output",
|
| 227 |
+
interactive=False,
|
| 228 |
+
lines=15,
|
| 229 |
+
max_lines=30,
|
| 230 |
+
show_copy_button=True
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
# Clear button (after all components are defined)
|
| 234 |
+
with gr.Row():
|
| 235 |
+
clear_btn = gr.ClearButton(
|
| 236 |
+
components=[question, ground_truth, file_name, file_link, answer, console_logs]
|
| 237 |
+
)
|
| 238 |
|
| 239 |
submit_btn.click(
|
| 240 |
fn=ask,
|
| 241 |
inputs=[question, openai_api_key, gemini_api_key, anthropic_api_key, file_name],
|
| 242 |
+
outputs=[answer, console_logs]
|
| 243 |
)
|
| 244 |
|
| 245 |
file_name.change(
|