Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os, sys
|
|
|
2 |
from enum import Enum
|
3 |
import gradio as gr
|
4 |
import requests
|
@@ -14,7 +15,6 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndB
|
|
14 |
from smolagents import CodeAgent, VisitWebpageTool, WebSearchTool, WikipediaSearchTool, PythonInterpreterTool
|
15 |
from smolagents.models import ChatMessage
|
16 |
from custom_tools import WebpageStructureAnalyzerTool
|
17 |
-
from system_prompts import CODE_AGENT_DEFAULT_SYSTEM_PROMPTS
|
18 |
|
19 |
subprocess.run(["playwright", "install"], check=True)
|
20 |
|
@@ -48,6 +48,45 @@ class PreloadedPythonTool(PythonInterpreterTool):
|
|
48 |
)
|
49 |
return super().run(preamble + code)
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def check_token_access():
|
52 |
token = os.environ.get("HF_TOKEN", "")
|
53 |
if not token:
|
@@ -199,6 +238,10 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
199 |
python_tool = PythonInterpreterTool()
|
200 |
html_parse_tool = VisitWebpageTool()
|
201 |
# (3) Create the system prompt
|
|
|
|
|
|
|
|
|
202 |
my_added_prompts = """
|
203 |
When you receive output from a tool (like wiki_tool or html_parse_tool), do not include the entire raw output in your next thought if it is very long.
|
204 |
Instead, first analyze it (possibly using another tool or by writing code to extract key parts) and only include essential snippets, summaries, or the extracted data relevant to your current plan in your thoughts and observations.
|
@@ -207,7 +250,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
207 |
Your thoughts and reasoning should precede the code block.
|
208 |
For web pages, always use web_structure_analyzer_tool to understand the page's layout before attempting to write detailed parsing code with bs4.
|
209 |
"""
|
210 |
-
combined_system_prompt =
|
211 |
# (4) Create the CodeAgent, passing the LLM wrapper and tools
|
212 |
agent = CodeAgent(model=llm_model,
|
213 |
tools=[web_structure_analyzer_tool, wiki_tool, search_tool, python_tool, html_parse_tool],
|
|
|
1 |
import os, sys
|
2 |
+
import yaml
|
3 |
from enum import Enum
|
4 |
import gradio as gr
|
5 |
import requests
|
|
|
15 |
from smolagents import CodeAgent, VisitWebpageTool, WebSearchTool, WikipediaSearchTool, PythonInterpreterTool
|
16 |
from smolagents.models import ChatMessage
|
17 |
from custom_tools import WebpageStructureAnalyzerTool
|
|
|
18 |
|
19 |
subprocess.run(["playwright", "install"], check=True)
|
20 |
|
|
|
48 |
)
|
49 |
return super().run(preamble + code)
|
50 |
|
51 |
+
# In your app.py (or a helper module like your system_prompts.py)
|
52 |
+
import yaml
|
53 |
+
import os
|
54 |
+
|
55 |
+
# --- Function to load the prompt from your YAML file ---
|
56 |
+
def load_prompt_from_yaml(file_path: str) -> str:
|
57 |
+
try:
|
58 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
59 |
+
data = yaml.safe_load(f)
|
60 |
+
# Now, how you extract the prompt depends on the YAML's structure:
|
61 |
+
|
62 |
+
# Scenario A: If codeagent.yaml's *entire content* is the system prompt string
|
63 |
+
# (i.e., it's not a dictionary, just a multi-line string)
|
64 |
+
if isinstance(data, str):
|
65 |
+
return data
|
66 |
+
|
67 |
+
# Scenario B: If codeagent.yaml is a dictionary with a specific key for the system prompt
|
68 |
+
# For example, if it looks like:
|
69 |
+
# system_prompt: |
|
70 |
+
# This is the prompt...
|
71 |
+
# It has multiple lines...
|
72 |
+
# other_key: ...
|
73 |
+
elif isinstance(data, dict):
|
74 |
+
# YOU NEED TO KNOW THE KEY NAME FOR THE SYSTEM PROMPT IN THE YAML
|
75 |
+
# Let's assume the key is 'system_prompt' for this example
|
76 |
+
if "system_prompt" in data: # Replace "system_prompt" with the actual key
|
77 |
+
return data["system_prompt"]
|
78 |
+
else:
|
79 |
+
raise ValueError("Key for system prompt not found in YAML dictionary.")
|
80 |
+
else:
|
81 |
+
raise ValueError("YAML content is not in an expected format (string or dict).")
|
82 |
+
|
83 |
+
except FileNotFoundError:
|
84 |
+
print(f"ERROR: YAML prompt file not found at {file_path}")
|
85 |
+
return "Fallback default system prompt: You are a helpful AI." # Or raise an error
|
86 |
+
except Exception as e:
|
87 |
+
print(f"ERROR: Could not load or parse YAML prompt file {file_path}: {e}")
|
88 |
+
return "Fallback default system prompt: You are a helpful AI."
|
89 |
+
|
90 |
def check_token_access():
|
91 |
token = os.environ.get("HF_TOKEN", "")
|
92 |
if not token:
|
|
|
238 |
python_tool = PythonInterpreterTool()
|
239 |
html_parse_tool = VisitWebpageTool()
|
240 |
# (3) Create the system prompt
|
241 |
+
# Assuming 'my_default_system_prompt.yaml' is in the same directory as app.py
|
242 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
243 |
+
prompt_yaml_file = os.path.join(current_dir, "default_system_prompt.yaml")
|
244 |
+
default_system_prompt = load_prompt_from_yaml(prompt_yaml_file)
|
245 |
my_added_prompts = """
|
246 |
When you receive output from a tool (like wiki_tool or html_parse_tool), do not include the entire raw output in your next thought if it is very long.
|
247 |
Instead, first analyze it (possibly using another tool or by writing code to extract key parts) and only include essential snippets, summaries, or the extracted data relevant to your current plan in your thoughts and observations.
|
|
|
250 |
Your thoughts and reasoning should precede the code block.
|
251 |
For web pages, always use web_structure_analyzer_tool to understand the page's layout before attempting to write detailed parsing code with bs4.
|
252 |
"""
|
253 |
+
combined_system_prompt = default_system_prompt + "\n\n" + added_prompts
|
254 |
# (4) Create the CodeAgent, passing the LLM wrapper and tools
|
255 |
agent = CodeAgent(model=llm_model,
|
256 |
tools=[web_structure_analyzer_tool, wiki_tool, search_tool, python_tool, html_parse_tool],
|