Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,10 @@
|
|
1 |
import argparse
|
2 |
import json
|
3 |
import os
|
|
|
4 |
import threading
|
|
|
|
|
5 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
6 |
from datetime import datetime
|
7 |
from pathlib import Path
|
@@ -43,7 +46,6 @@ from smolagents import (
|
|
43 |
from smolagents.agent_types import AgentText, AgentImage, AgentAudio
|
44 |
from smolagents.gradio_ui import pull_messages_from_step, handle_agent_output_types
|
45 |
|
46 |
-
|
47 |
AUTHORIZED_IMPORTS = [
|
48 |
"requests",
|
49 |
"zipfile",
|
@@ -86,13 +88,11 @@ _ = """
|
|
86 |
eval_ds = datasets.load_dataset("gaia-benchmark/GAIA", "2023_all")[SET]
|
87 |
eval_ds = eval_ds.rename_columns({"Question": "question", "Final answer": "true_answer", "Level": "task"})
|
88 |
|
89 |
-
|
90 |
def preprocess_file_paths(row):
|
91 |
if len(row["file_name"]) > 0:
|
92 |
row["file_name"] = f"data/gaia/{SET}/" + row["file_name"]
|
93 |
return row
|
94 |
|
95 |
-
|
96 |
eval_ds = eval_ds.map(preprocess_file_paths)
|
97 |
eval_df = pd.DataFrame(eval_ds)
|
98 |
print("Loaded evaluation dataset:")
|
@@ -113,13 +113,19 @@ BROWSER_CONFIG = {
|
|
113 |
|
114 |
os.makedirs(f"./{BROWSER_CONFIG['downloads_folder']}", exist_ok=True)
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
model = LiteLLMModel(
|
117 |
-
# "gpt-4o",
|
118 |
os.getenv("MODEL_ID", "gpt-4o-mini"),
|
119 |
custom_role_conversions=custom_role_conversions,
|
120 |
-
api_base=
|
121 |
api_key=os.getenv("OPENAI_API_KEY"),
|
122 |
)
|
|
|
123 |
|
124 |
text_limit = 20000
|
125 |
ti_tool = TextInspectorTool(model, text_limit)
|
@@ -149,23 +155,6 @@ agent = CodeAgent(
|
|
149 |
document_inspection_tool = TextInspectorTool(model, 20000)
|
150 |
|
151 |
|
152 |
-
# augmented_question = """You have one question to answer. It is paramount that you provide a correct answer.
|
153 |
-
# Give it all you can: I know for a fact that you have access to all the relevant tools to solve it and find the correct answer (the answer does exist). Failure or 'I cannot answer' or 'None found' will not be tolerated, success will be rewarded.
|
154 |
-
# Run verification steps if that's needed, you must make sure you find the correct answer!
|
155 |
-
# Here is the task:
|
156 |
-
# """ + example["question"]
|
157 |
-
|
158 |
-
# if example["file_name"]:
|
159 |
-
# prompt_use_files = "\n\nTo solve the task above, you will have to use this attached file:"
|
160 |
-
# prompt_use_files += get_single_file_description(
|
161 |
-
# example["file_name"], example["question"], visual_inspection_tool, document_inspection_tool
|
162 |
-
# )
|
163 |
-
# augmented_question += prompt_use_files
|
164 |
-
|
165 |
-
|
166 |
-
# final_result = agent.run(augmented_question)
|
167 |
-
|
168 |
-
|
169 |
def stream_to_gradio(
|
170 |
agent,
|
171 |
task: str,
|
@@ -174,9 +163,7 @@ def stream_to_gradio(
|
|
174 |
):
|
175 |
"""Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
|
176 |
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
|
177 |
-
for message in pull_messages_from_step(
|
178 |
-
step_log,
|
179 |
-
):
|
180 |
yield message
|
181 |
|
182 |
final_answer = step_log # Last log is the run's final_answer
|
@@ -245,9 +232,7 @@ class GradioUI:
|
|
245 |
|
246 |
# Sanitize file name
|
247 |
original_name = os.path.basename(file.name)
|
248 |
-
sanitized_name = re.sub(
|
249 |
-
r"[^\w\-.]", "_", original_name
|
250 |
-
) # Replace any non-alphanumeric, non-dash, or non-dot characters with underscores
|
251 |
|
252 |
type_to_ext = {}
|
253 |
for ext, t in mimetypes.types_map.items():
|
@@ -315,4 +300,4 @@ You can try a simplified version below. ๐""")
|
|
315 |
|
316 |
demo.launch(debug=True, share=True, **kwargs)
|
317 |
|
318 |
-
GradioUI(agent).launch()
|
|
|
1 |
import argparse
|
2 |
import json
|
3 |
import os
|
4 |
+
import re
|
5 |
import threading
|
6 |
+
import mimetypes
|
7 |
+
import shutil
|
8 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
9 |
from datetime import datetime
|
10 |
from pathlib import Path
|
|
|
46 |
from smolagents.agent_types import AgentText, AgentImage, AgentAudio
|
47 |
from smolagents.gradio_ui import pull_messages_from_step, handle_agent_output_types
|
48 |
|
|
|
49 |
AUTHORIZED_IMPORTS = [
|
50 |
"requests",
|
51 |
"zipfile",
|
|
|
88 |
eval_ds = datasets.load_dataset("gaia-benchmark/GAIA", "2023_all")[SET]
|
89 |
eval_ds = eval_ds.rename_columns({"Question": "question", "Final answer": "true_answer", "Level": "task"})
|
90 |
|
|
|
91 |
def preprocess_file_paths(row):
|
92 |
if len(row["file_name"]) > 0:
|
93 |
row["file_name"] = f"data/gaia/{SET}/" + row["file_name"]
|
94 |
return row
|
95 |
|
|
|
96 |
eval_ds = eval_ds.map(preprocess_file_paths)
|
97 |
eval_df = pd.DataFrame(eval_ds)
|
98 |
print("Loaded evaluation dataset:")
|
|
|
113 |
|
114 |
os.makedirs(f"./{BROWSER_CONFIG['downloads_folder']}", exist_ok=True)
|
115 |
|
116 |
+
# --- ์์ ๋ ๋ถ๋ถ: OPENAI_API_BASE์ ์๋ชป๋ ์๋ํฌ์ธํธ ์ ๊ฑฐ ---
|
117 |
+
openai_api_base = os.getenv("OPENAI_API_BASE", "https://api.openai.com/v1")
|
118 |
+
# ๋ง์ฝ ํ๊ฒฝ๋ณ์์ ๋ถํ์ํ ์๋ํฌ์ธํธ๊ฐ ํฌํจ๋์ด ์๋ค๋ฉด ์ ๊ฑฐํฉ๋๋ค.
|
119 |
+
if openai_api_base.endswith("/chat/completions"):
|
120 |
+
openai_api_base = openai_api_base.rsplit("/chat/completions", 1)[0]
|
121 |
+
|
122 |
model = LiteLLMModel(
|
|
|
123 |
os.getenv("MODEL_ID", "gpt-4o-mini"),
|
124 |
custom_role_conversions=custom_role_conversions,
|
125 |
+
api_base=openai_api_base,
|
126 |
api_key=os.getenv("OPENAI_API_KEY"),
|
127 |
)
|
128 |
+
# --- ์์ ๋ ---
|
129 |
|
130 |
text_limit = 20000
|
131 |
ti_tool = TextInspectorTool(model, text_limit)
|
|
|
155 |
document_inspection_tool = TextInspectorTool(model, 20000)
|
156 |
|
157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
def stream_to_gradio(
|
159 |
agent,
|
160 |
task: str,
|
|
|
163 |
):
|
164 |
"""Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
|
165 |
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
|
166 |
+
for message in pull_messages_from_step(step_log):
|
|
|
|
|
167 |
yield message
|
168 |
|
169 |
final_answer = step_log # Last log is the run's final_answer
|
|
|
232 |
|
233 |
# Sanitize file name
|
234 |
original_name = os.path.basename(file.name)
|
235 |
+
sanitized_name = re.sub(r"[^\w\-.]", "_", original_name) # Replace any non-alphanumeric, non-dash, or non-dot characters with underscores
|
|
|
|
|
236 |
|
237 |
type_to_ext = {}
|
238 |
for ext, t in mimetypes.types_map.items():
|
|
|
300 |
|
301 |
demo.launch(debug=True, share=True, **kwargs)
|
302 |
|
303 |
+
GradioUI(agent).launch()
|