bstraehle commited on
Commit
2799787
·
verified ·
1 Parent(s): ddd42e3

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +246 -0
app.py CHANGED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # References:
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,
11
+ get_dataset
12
+ )
13
+
14
+ # Configuration
15
+
16
+ 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 = ""):
23
+ """
24
+ Ask General AI Assistant a question to answer.
25
+
26
+ Args:
27
+ question (str): The question to answer
28
+ openai_api_key (str): OpenAI API key (always used)
29
+ gemini_api_key (str): Gemini API key (always used)
30
+ anthropic_api_key (str): Anthropic API key (only used by Stagehand tool)
31
+ file_name (str): Optional file name
32
+
33
+ Returns:
34
+ str: The answer to the question
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}"
54
+
55
+ try:
56
+ # API keys are provided thanks to sponsor credit.
57
+
58
+ openai_key = openai_api_key if openai_api_key and openai_api_key != "*" else os.environ.get("OPENAI_API_KEY")
59
+ gemini_key = gemini_api_key if gemini_api_key and gemini_api_key != "*" else os.environ.get("GEMINI_API_KEY")
60
+ anthropic_key = anthropic_api_key if anthropic_api_key and anthropic_api_key != "*" else os.environ.get("ANTHROPIC_API_KEY")
61
+
62
+ os.environ["OPENAI_API_KEY"] = openai_key
63
+ os.environ["GEMINI_API_KEY"] = gemini_key
64
+ os.environ["ANTHROPIC_API_KEY"] = anthropic_key
65
+
66
+ return run_crew(question, file_name)
67
+ except Exception as e:
68
+ gr.Warning(str(e))
69
+ return ""
70
+
71
+ # Helper functions
72
+
73
+ def update_file_link(file_name):
74
+ if file_name:
75
+ return f"<a href='{BASE_URL}/files/{file_name}' target='_blank'>Open File</a>"
76
+
77
+ return ""
78
+
79
+ def watchfn(*args, **kwargs):
80
+ pass
81
+
82
+ gradio.utils.watchfn_spaces = watchfn
83
+
84
+ # Graphical user interface
85
+
86
+ DESCRIPTION = (
87
+ f"<strong>Multi-agent, multi-modal, multi-model AI platform</strong> with high agency, "
88
+ f"including code generation & execution, browser automation, and multi-modal reasoning. "
89
+ f"The system can solve <a href='https://arxiv.org/pdf/2311.12983'>GAIA Benchmark</a> "
90
+ f"Level 1, 2, 3 and <a href='https://arxiv.org/pdf/2501.14249'>Humanity's Last Exam</a> "
91
+ f"problems. To get started with the <strong>GUI</strong>, select from the examples below. "
92
+ f"To use via <strong>API</strong> or <strong>MCP</strong>, see the link below. "
93
+ f"API keys are provided thanks to sponsor credit. "
94
+ f"<a href='{BASE_URL}/README.md'>Documentation</a></p>"
95
+ )
96
+
97
+ DEFAULT_QUESTION = (
98
+ f"What is the first name of the person who submitted a General AI Assistant in MCP's 1st Birthday Hackathon, "
99
+ f"and how many public GitHub repos do they have?"
100
+ )
101
+
102
+ DEFAULT_GROUND_TRUTH = "Bernd, 18"
103
+
104
+ CSS_FULL_WIDTH = """
105
+ <style>
106
+ html,
107
+ body,
108
+ main,
109
+ .gradio-app {
110
+ width: 100% !important;
111
+ max-width: 100% !important;
112
+ margin: 0 !important;
113
+ padding: 0 !important;
114
+ overflow-x: hidden !important;
115
+ }
116
+
117
+ .full-width-app {
118
+ width: 100% !important;
119
+ max-width: 100% !important;
120
+ margin: 0 !important;
121
+ padding: 0 !important;
122
+ }
123
+
124
+ .content-padding {
125
+ padding: 0 1.5rem 0;
126
+ }
127
+ </style>
128
+ """
129
+
130
+ with gr.Blocks(elem_classes=["full-width-app"]) as gaia:
131
+ gr.HTML(CSS_FULL_WIDTH)
132
+
133
+ with gr.Column(elem_classes=["content-padding"]):
134
+ gr.Markdown("## General AI Assistant")
135
+
136
+ gr.Markdown(DESCRIPTION)
137
+
138
+ with gr.Row():
139
+ with gr.Column(scale=3):
140
+ with gr.Row():
141
+ question = gr.Textbox(
142
+ label="Question *",
143
+ value=DEFAULT_QUESTION,
144
+ interactive=True,
145
+ lines=2,
146
+ max_lines=5
147
+ )
148
+ with gr.Row():
149
+ ground_truth = gr.Textbox(
150
+ label="Ground Truth",
151
+ value=DEFAULT_GROUND_TRUTH,
152
+ interactive=True,
153
+ lines=1,
154
+ max_lines=2
155
+ )
156
+ file_name = gr.Textbox(
157
+ label="File Name",
158
+ interactive=True,
159
+ scale=2,
160
+ lines=1,
161
+ max_lines=2
162
+ )
163
+ file_link = gr.HTML(
164
+ label="File Link",
165
+ value=""
166
+ )
167
+ with gr.Row():
168
+ openai_api_key = gr.Textbox(
169
+ label="OpenAI API Key *",
170
+ type="password",
171
+ placeholder="sk‑...",
172
+ value="*", # API keys are provided thanks to sponsor credit
173
+ interactive=True
174
+ )
175
+ gemini_api_key = gr.Textbox(
176
+ label="Gemini API Key *",
177
+ type="password",
178
+ value="*", # API keys are provided thanks to sponsor credit
179
+ interactive=True
180
+ )
181
+ anthropic_api_key = gr.Textbox(
182
+ label="Anthropic API Key *",
183
+ type="password",
184
+ placeholder="sk-ant-...",
185
+ value="*", # API keys are provided thanks to sponsor credit
186
+ interactive=True
187
+ )
188
+ with gr.Row():
189
+ clear_btn = gr.ClearButton(
190
+ components=[question, ground_truth, file_name, file_link]
191
+ )
192
+ submit_btn = gr.Button("Submit", variant="primary")
193
+ with gr.Column(scale=1):
194
+ answer = gr.Textbox(
195
+ label="Answer",
196
+ interactive=False,
197
+ lines=2,
198
+ max_lines=5
199
+ )
200
+
201
+ submit_btn.click(
202
+ fn=ask,
203
+ inputs=[question, openai_api_key, gemini_api_key, anthropic_api_key, file_name],
204
+ outputs=answer
205
+ )
206
+
207
+ file_name.change(
208
+ fn=update_file_link,
209
+ inputs=[file_name],
210
+ outputs=[file_link]
211
+ )
212
+
213
+ with gr.Tabs():
214
+ with gr.TabItem("GAIA Benchmark Level 1"):
215
+ gr.Examples(
216
+ examples=get_dataset(DATASET_TYPE_GAIA, 1),
217
+ inputs=[question, ground_truth, file_name, "", "", ""],
218
+ examples_per_page=3,
219
+ cache_examples=False
220
+ )
221
+
222
+ with gr.TabItem("GAIA Benchmark Level 2"):
223
+ gr.Examples(
224
+ examples=get_dataset(DATASET_TYPE_GAIA, 2),
225
+ inputs=[question, ground_truth, file_name, "", "", ""],
226
+ examples_per_page=3,
227
+ cache_examples=False
228
+ )
229
+
230
+ with gr.TabItem("GAIA Benchmark Level 3"):
231
+ gr.Examples(
232
+ examples=get_dataset(DATASET_TYPE_GAIA, 3),
233
+ inputs=[question, ground_truth, file_name, "", "", ""],
234
+ examples_per_page=3,
235
+ cache_examples=False
236
+ )
237
+
238
+ with gr.TabItem("Humanity's Last Exam"):
239
+ gr.Examples(
240
+ examples=get_dataset(DATASET_TYPE_HLE, 0),
241
+ inputs=[question, ground_truth, file_name, "", "", ""],
242
+ examples_per_page=3,
243
+ cache_examples=False
244
+ )
245
+
246
+ gaia.launch(mcp_server=True, ssr_mode=False)