Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,180 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import yaml
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
|
|
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
return "
|
| 20 |
|
|
|
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""
|
|
|
|
| 24 |
Args:
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"Error
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 46 |
-
custom_role_conversions=None
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# Import tool from Hub
|
| 51 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
-
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
|
|
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
# AI Agent Framework Imports
|
| 4 |
+
from smolagents import CodeAgent, HfApiModel, tool, load_tool
|
| 5 |
+
|
| 6 |
+
# Web Scraping and Parsing
|
| 7 |
import requests
|
| 8 |
+
from bs4 import BeautifulSoup
|
| 9 |
+
|
| 10 |
+
# Selenium & webdriver-manager for Browser Automation
|
| 11 |
+
from selenium import webdriver
|
| 12 |
+
from selenium.webdriver.chrome.options import Options
|
| 13 |
+
from selenium.webdriver.chrome.service import Service
|
| 14 |
+
from selenium.webdriver.common.by import By
|
| 15 |
+
from webdriver_manager.chrome import ChromeDriverManager
|
| 16 |
+
import time
|
| 17 |
+
import os
|
| 18 |
+
|
| 19 |
+
# NLP and Text Processing (Optional for Future Enhancements)
|
| 20 |
+
import nltk
|
| 21 |
+
from nltk.tokenize import sent_tokenize
|
| 22 |
+
import spacy
|
| 23 |
+
|
| 24 |
+
# Code Parsing and Explanation
|
| 25 |
+
import ast
|
| 26 |
+
from pygments import highlight
|
| 27 |
+
from pygments.lexers import PythonLexer
|
| 28 |
+
from pygments.formatters import HtmlFormatter
|
| 29 |
+
|
| 30 |
+
# UI Overlay & Interaction
|
| 31 |
+
import gradio as gr
|
| 32 |
+
import cv2 # Optional: for visual highlights
|
| 33 |
import yaml
|
|
|
|
| 34 |
|
| 35 |
+
# Final Answer and UI Handling
|
| 36 |
+
from tools.final_answer import FinalAnswerTool
|
| 37 |
from Gradio_UI import GradioUI
|
| 38 |
|
| 39 |
+
# Helper function to automatically obtain and configure the Selenium driver
|
| 40 |
+
def get_driver():
|
| 41 |
+
options = Options()
|
| 42 |
+
options.add_argument("--headless") # Run browser in headless mode
|
| 43 |
+
options.add_argument("--disable-gpu")
|
| 44 |
+
options.add_argument("--no-sandbox")
|
| 45 |
+
options.add_argument("--disable-dev-shm-usage")
|
| 46 |
+
# Automatically install and use the appropriate ChromeDriver
|
| 47 |
+
service = Service(ChromeDriverManager().install())
|
| 48 |
+
driver = webdriver.Chrome(service=service, options=options)
|
| 49 |
+
return driver
|
| 50 |
+
|
| 51 |
+
# Tool to analyze a webpage or document and simplify its content
|
| 52 |
+
@tool
|
| 53 |
+
def analyze_and_simplify(url: str) -> str:
|
| 54 |
+
"""Fetches webpage content, extracts text, and provides a simplified summary.
|
| 55 |
+
|
| 56 |
+
Args:
|
| 57 |
+
url: The webpage URL to analyze.
|
| 58 |
+
"""
|
| 59 |
+
try:
|
| 60 |
+
driver = get_driver()
|
| 61 |
+
driver.get(url)
|
| 62 |
+
time.sleep(5) # Wait for content to load
|
| 63 |
+
soup = BeautifulSoup(driver.page_source, "html.parser")
|
| 64 |
+
driver.quit()
|
| 65 |
+
text = ' '.join([p.get_text() for p in soup.find_all("p")])
|
| 66 |
+
if not text:
|
| 67 |
+
return "No readable text found on the page."
|
| 68 |
+
summary = f"Simplified Summary: {text[:500]}... (summary continues)"
|
| 69 |
+
return summary
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return f"Error analyzing webpage: {str(e)}"
|
| 72 |
+
|
| 73 |
+
# Tool to detect ambiguous directions and suggest clarification
|
| 74 |
+
@tool
|
| 75 |
+
def detect_ambiguity(content: str) -> str:
|
| 76 |
+
"""Checks for vague instructions and suggests clarifications.
|
| 77 |
+
|
| 78 |
+
Args:
|
| 79 |
+
content: Text to analyze.
|
| 80 |
+
"""
|
| 81 |
+
# Placeholder logic for ambiguity detection
|
| 82 |
+
return "Ambiguity detected. Click 'Is this ambiguous?' for help."
|
| 83 |
+
|
| 84 |
+
# Tool to explain technical terms or assumed knowledge in simple language
|
| 85 |
@tool
|
| 86 |
+
def explain_assumed_knowledge(term: str) -> str:
|
| 87 |
+
"""Defines difficult or technical terms in a simple way.
|
| 88 |
+
|
| 89 |
Args:
|
| 90 |
+
term: The term to explain.
|
|
|
|
| 91 |
"""
|
| 92 |
+
return f"Definition of '{term}': [Detailed beginner-friendly explanation here]"
|
| 93 |
|
| 94 |
+
# Tool to highlight a UI element and optionally automate its interaction
|
| 95 |
@tool
|
| 96 |
+
def highlight_elements(step: str, element: str, auto_execute: bool = False) -> str:
|
| 97 |
+
"""Highlights a UI element and optionally performs an action.
|
| 98 |
+
|
| 99 |
Args:
|
| 100 |
+
step: The current step in the guide.
|
| 101 |
+
element: The UI element to highlight (expected as an XPath).
|
| 102 |
+
auto_execute: If True, the agent will click the element automatically.
|
| 103 |
"""
|
| 104 |
try:
|
| 105 |
+
driver = get_driver()
|
| 106 |
+
# Navigate to a placeholder page; replace "about:blank" with the actual URL if needed
|
| 107 |
+
driver.get("about:blank")
|
| 108 |
+
if auto_execute:
|
| 109 |
+
elem = driver.find_element(By.XPATH, element)
|
| 110 |
+
elem.click()
|
| 111 |
+
result = f"Step {step}: Auto-clicked '{element}'."
|
| 112 |
+
else:
|
| 113 |
+
result = f"Step {step}: Please click on '{element}'."
|
| 114 |
+
driver.quit()
|
| 115 |
+
return result
|
| 116 |
except Exception as e:
|
| 117 |
+
return f"Error in highlight_elements: {str(e)}"
|
| 118 |
|
| 119 |
+
# Tool to explain a single line of code in simple terms
|
| 120 |
+
@tool
|
| 121 |
+
def explain_code_line(line: str) -> str:
|
| 122 |
+
"""Explains what a line of code does in simple terms.
|
| 123 |
+
|
| 124 |
+
Args:
|
| 125 |
+
line: The code to explain.
|
| 126 |
+
"""
|
| 127 |
+
return f"Explanation for: {line} [Insert explanation here]"
|
| 128 |
|
| 129 |
+
# Tool to provide an interactive teacher box for user queries
|
| 130 |
+
@tool
|
| 131 |
+
def teacher_box_query(question: str) -> str:
|
| 132 |
+
"""Allows users to ask the AI questions while browsing.
|
| 133 |
+
|
| 134 |
+
Args:
|
| 135 |
+
question: User's query.
|
| 136 |
+
"""
|
| 137 |
+
return f"AI Answer: [Response for '{question}']"
|
| 138 |
|
| 139 |
+
# Tool to toggle between manual and automatic navigation modes
|
| 140 |
+
@tool
|
| 141 |
+
def toggle_auto_execution(enable: bool) -> str:
|
| 142 |
+
"""Lets the user turn automatic navigation on or off.
|
| 143 |
+
|
| 144 |
+
Args:
|
| 145 |
+
enable: True for auto-mode, False for manual steps.
|
| 146 |
+
"""
|
| 147 |
+
return "Auto-execution enabled." if enable else "Manual step-by-step mode enabled."
|
| 148 |
|
| 149 |
+
# Configure the AI Model
|
| 150 |
model = HfApiModel(
|
| 151 |
+
max_tokens=2096, # Maximum response length
|
| 152 |
+
temperature=0.5, # Controls response randomness
|
| 153 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Selected model
|
| 154 |
+
custom_role_conversions=None
|
| 155 |
)
|
| 156 |
|
| 157 |
+
# Load prompt templates from a YAML configuration file
|
|
|
|
|
|
|
|
|
|
| 158 |
with open("prompts.yaml", 'r') as stream:
|
| 159 |
prompt_templates = yaml.safe_load(stream)
|
| 160 |
+
|
| 161 |
+
# Create the AI agent with the defined tools
|
| 162 |
agent = CodeAgent(
|
| 163 |
model=model,
|
| 164 |
+
tools=[
|
| 165 |
+
FinalAnswerTool(),
|
| 166 |
+
analyze_and_simplify,
|
| 167 |
+
detect_ambiguity,
|
| 168 |
+
explain_assumed_knowledge,
|
| 169 |
+
highlight_elements,
|
| 170 |
+
explain_code_line,
|
| 171 |
+
teacher_box_query,
|
| 172 |
+
toggle_auto_execution
|
| 173 |
+
],
|
| 174 |
+
max_steps=6, # Maximum number of reasoning steps
|
| 175 |
+
verbosity_level=1, # Level of detail in agent responses
|
| 176 |
prompt_templates=prompt_templates
|
| 177 |
)
|
| 178 |
|
| 179 |
+
# Launch the interactive UI using Gradio
|
| 180 |
+
GradioUI(agent).launch()
|