Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,10 @@
|
|
| 1 |
|
| 2 |
|
|
|
|
|
|
|
| 3 |
# AI Agent Framework Imports
|
| 4 |
from smolagents import CodeAgent, HfApiModel, tool, load_tool
|
| 5 |
|
| 6 |
-
# Selenium & webdriver-manager for Browser Automation
|
| 7 |
-
from selenium import webdriver
|
| 8 |
-
from selenium.webdriver.chrome.options import Options
|
| 9 |
-
from selenium.webdriver.chrome.service import Service
|
| 10 |
-
from selenium.webdriver.common.by import By
|
| 11 |
-
from webdriver_manager.chrome import ChromeDriverManager
|
| 12 |
-
import re
|
| 13 |
-
import time
|
| 14 |
-
|
| 15 |
# Standard Library Imports (allowed)
|
| 16 |
import time
|
| 17 |
import yaml
|
|
@@ -21,48 +14,7 @@ from tools.final_answer import FinalAnswerTool
|
|
| 21 |
from Gradio_UI import GradioUI
|
| 22 |
|
| 23 |
# --------------------------------------------
|
| 24 |
-
#
|
| 25 |
-
def get_driver():
|
| 26 |
-
options = Options()
|
| 27 |
-
options.add_argument("--headless") # Run in headless mode
|
| 28 |
-
options.add_argument("--disable-gpu")
|
| 29 |
-
options.add_argument("--no-sandbox")
|
| 30 |
-
options.add_argument("--disable-dev-shm-usage")
|
| 31 |
-
# Use webdriver-manager to install the proper ChromeDriver
|
| 32 |
-
service = Service(ChromeDriverManager().install())
|
| 33 |
-
driver = webdriver.Chrome(service=service, options=options)
|
| 34 |
-
return driver
|
| 35 |
-
|
| 36 |
-
# --------------------------------------------
|
| 37 |
-
# Tool to analyze a webpage or document and provide a simplified summary.
|
| 38 |
-
@tool
|
| 39 |
-
def analyze_and_simplify(url: str) -> str:
|
| 40 |
-
"""Fetches webpage content using Selenium, strips HTML tags, and provides a simplified summary.
|
| 41 |
-
|
| 42 |
-
Args:
|
| 43 |
-
url: The webpage URL to analyze.
|
| 44 |
-
"""
|
| 45 |
-
try:
|
| 46 |
-
driver = get_driver()
|
| 47 |
-
driver.get(url)
|
| 48 |
-
time.sleep(5) # Wait for content to load
|
| 49 |
-
|
| 50 |
-
# Fetch page source and use regex to strip HTML tags.
|
| 51 |
-
page_source = driver.page_source
|
| 52 |
-
driver.quit()
|
| 53 |
-
# Remove HTML tags (very simple approach)
|
| 54 |
-
text = re.sub(r'<[^>]+>', '', page_source)
|
| 55 |
-
text = text.strip()
|
| 56 |
-
if not text:
|
| 57 |
-
return "No readable text found on the page."
|
| 58 |
-
# Return a truncated summary of the text.
|
| 59 |
-
summary = f"Simplified Summary: {text[:500]}..."
|
| 60 |
-
return summary
|
| 61 |
-
except Exception as e:
|
| 62 |
-
return f"Error analyzing webpage: {str(e)}"
|
| 63 |
-
|
| 64 |
-
# --------------------------------------------
|
| 65 |
-
# Tool to detect ambiguous instructions.
|
| 66 |
@tool
|
| 67 |
def detect_ambiguity(content: str) -> str:
|
| 68 |
"""Checks for vague instructions and suggests clarifications.
|
|
@@ -70,11 +22,10 @@ def detect_ambiguity(content: str) -> str:
|
|
| 70 |
Args:
|
| 71 |
content: Text to analyze.
|
| 72 |
"""
|
| 73 |
-
# Placeholder logic for ambiguity detection.
|
| 74 |
return "Ambiguity detected. Click 'Is this ambiguous?' for help."
|
| 75 |
|
| 76 |
# --------------------------------------------
|
| 77 |
-
# Tool
|
| 78 |
@tool
|
| 79 |
def explain_assumed_knowledge(term: str) -> str:
|
| 80 |
"""Defines technical terms in a simple way.
|
|
@@ -85,33 +36,20 @@ def explain_assumed_knowledge(term: str) -> str:
|
|
| 85 |
return f"Definition of '{term}': [Detailed beginner-friendly explanation here]"
|
| 86 |
|
| 87 |
# --------------------------------------------
|
| 88 |
-
# Tool
|
| 89 |
@tool
|
| 90 |
def highlight_elements(step: str, element: str, auto_execute: bool = False) -> str:
|
| 91 |
-
"""Highlights a UI element and optionally performs an action.
|
| 92 |
|
| 93 |
Args:
|
| 94 |
step: The current step in the guide.
|
| 95 |
-
element: The UI element to highlight (as an
|
| 96 |
-
auto_execute: If True, the agent
|
| 97 |
"""
|
| 98 |
-
|
| 99 |
-
driver = get_driver()
|
| 100 |
-
# Here we simply navigate to a blank page (or a given URL if needed).
|
| 101 |
-
driver.get("about:blank")
|
| 102 |
-
if auto_execute:
|
| 103 |
-
elem = driver.find_element(By.XPATH, element)
|
| 104 |
-
elem.click()
|
| 105 |
-
result = f"Step {step}: Auto-clicked '{element}'."
|
| 106 |
-
else:
|
| 107 |
-
result = f"Step {step}: Please click on '{element}'."
|
| 108 |
-
driver.quit()
|
| 109 |
-
return result
|
| 110 |
-
except Exception as e:
|
| 111 |
-
return f"Error in highlight_elements: {str(e)}"
|
| 112 |
|
| 113 |
# --------------------------------------------
|
| 114 |
-
# Tool
|
| 115 |
@tool
|
| 116 |
def explain_code_line(line: str) -> str:
|
| 117 |
"""Explains what a line of code does in simple terms.
|
|
@@ -122,7 +60,7 @@ def explain_code_line(line: str) -> str:
|
|
| 122 |
return f"Explanation for: {line} [Insert explanation here]"
|
| 123 |
|
| 124 |
# --------------------------------------------
|
| 125 |
-
# Tool
|
| 126 |
@tool
|
| 127 |
def teacher_box_query(question: str) -> str:
|
| 128 |
"""Allows users to ask the AI questions while browsing.
|
|
@@ -133,7 +71,7 @@ def teacher_box_query(question: str) -> str:
|
|
| 133 |
return f"AI Answer: [Response for '{question}']"
|
| 134 |
|
| 135 |
# --------------------------------------------
|
| 136 |
-
# Tool
|
| 137 |
@tool
|
| 138 |
def toggle_auto_execution(enable: bool) -> str:
|
| 139 |
"""Lets the user turn automatic navigation on or off.
|
|
@@ -156,24 +94,5 @@ model = HfApiModel(
|
|
| 156 |
with open("prompts.yaml", 'r') as stream:
|
| 157 |
prompt_templates = yaml.safe_load(stream)
|
| 158 |
|
| 159 |
-
#
|
| 160 |
-
agent = CodeAgent(
|
| 161 |
-
model=model,
|
| 162 |
-
tools=[
|
| 163 |
-
FinalAnswerTool(),
|
| 164 |
-
analyze_and_simplify,
|
| 165 |
-
detect_ambiguity,
|
| 166 |
-
explain_assumed_knowledge,
|
| 167 |
-
highlight_elements,
|
| 168 |
-
explain_code_line,
|
| 169 |
-
teacher_box_query,
|
| 170 |
-
toggle_auto_execution
|
| 171 |
-
],
|
| 172 |
-
max_steps=6, # Maximum number of reasoning steps.
|
| 173 |
-
verbosity_level=1, # Level of detail in agent responses.
|
| 174 |
-
prompt_templates=prompt_templates
|
| 175 |
-
)
|
| 176 |
-
|
| 177 |
-
# Launch the interactive UI using Gradio.
|
| 178 |
-
GradioUI(agent).launch()
|
| 179 |
|
|
|
|
| 1 |
|
| 2 |
|
| 3 |
+
# app.py
|
| 4 |
+
|
| 5 |
# AI Agent Framework Imports
|
| 6 |
from smolagents import CodeAgent, HfApiModel, tool, load_tool
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Standard Library Imports (allowed)
|
| 9 |
import time
|
| 10 |
import yaml
|
|
|
|
| 14 |
from Gradio_UI import GradioUI
|
| 15 |
|
| 16 |
# --------------------------------------------
|
| 17 |
+
# Tool: detect_ambiguity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@tool
|
| 19 |
def detect_ambiguity(content: str) -> str:
|
| 20 |
"""Checks for vague instructions and suggests clarifications.
|
|
|
|
| 22 |
Args:
|
| 23 |
content: Text to analyze.
|
| 24 |
"""
|
|
|
|
| 25 |
return "Ambiguity detected. Click 'Is this ambiguous?' for help."
|
| 26 |
|
| 27 |
# --------------------------------------------
|
| 28 |
+
# Tool: explain_assumed_knowledge
|
| 29 |
@tool
|
| 30 |
def explain_assumed_knowledge(term: str) -> str:
|
| 31 |
"""Defines technical terms in a simple way.
|
|
|
|
| 36 |
return f"Definition of '{term}': [Detailed beginner-friendly explanation here]"
|
| 37 |
|
| 38 |
# --------------------------------------------
|
| 39 |
+
# Tool: highlight_elements (modified to a placeholder)
|
| 40 |
@tool
|
| 41 |
def highlight_elements(step: str, element: str, auto_execute: bool = False) -> str:
|
| 42 |
+
"""(Placeholder) Highlights a UI element and optionally performs an action.
|
| 43 |
|
| 44 |
Args:
|
| 45 |
step: The current step in the guide.
|
| 46 |
+
element: The UI element to highlight (as an identifier).
|
| 47 |
+
auto_execute: If True, the agent would auto-click the element.
|
| 48 |
"""
|
| 49 |
+
return "Highlight functionality is currently not available."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# --------------------------------------------
|
| 52 |
+
# Tool: explain_code_line
|
| 53 |
@tool
|
| 54 |
def explain_code_line(line: str) -> str:
|
| 55 |
"""Explains what a line of code does in simple terms.
|
|
|
|
| 60 |
return f"Explanation for: {line} [Insert explanation here]"
|
| 61 |
|
| 62 |
# --------------------------------------------
|
| 63 |
+
# Tool: teacher_box_query
|
| 64 |
@tool
|
| 65 |
def teacher_box_query(question: str) -> str:
|
| 66 |
"""Allows users to ask the AI questions while browsing.
|
|
|
|
| 71 |
return f"AI Answer: [Response for '{question}']"
|
| 72 |
|
| 73 |
# --------------------------------------------
|
| 74 |
+
# Tool: toggle_auto_execution
|
| 75 |
@tool
|
| 76 |
def toggle_auto_execution(enable: bool) -> str:
|
| 77 |
"""Lets the user turn automatic navigation on or off.
|
|
|
|
| 94 |
with open("prompts.yaml", 'r') as stream:
|
| 95 |
prompt_templates = yaml.safe_load(stream)
|
| 96 |
|
| 97 |
+
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|