Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,43 @@
|
|
| 1 |
-
|
| 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 |
-
#
|
|
|
|
| 40 |
def get_driver():
|
| 41 |
options = Options()
|
| 42 |
-
options.add_argument("--headless")
|
| 43 |
options.add_argument("--disable-gpu")
|
| 44 |
options.add_argument("--no-sandbox")
|
| 45 |
options.add_argument("--disable-dev-shm-usage")
|
| 46 |
-
#
|
| 47 |
service = Service(ChromeDriverManager().install())
|
| 48 |
driver = webdriver.Chrome(service=service, options=options)
|
| 49 |
return driver
|
| 50 |
|
| 51 |
-
#
|
|
|
|
| 52 |
@tool
|
| 53 |
def analyze_and_simplify(url: str) -> str:
|
| 54 |
-
"""Fetches webpage content,
|
| 55 |
|
| 56 |
Args:
|
| 57 |
url: The webpage URL to analyze.
|
|
@@ -60,17 +46,23 @@ def analyze_and_simplify(url: str) -> str:
|
|
| 60 |
driver = get_driver()
|
| 61 |
driver.get(url)
|
| 62 |
time.sleep(5) # Wait for content to load
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
driver.quit()
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
if not text:
|
| 67 |
return "No readable text found on the page."
|
| 68 |
-
|
|
|
|
| 69 |
return summary
|
| 70 |
except Exception as e:
|
| 71 |
return f"Error analyzing webpage: {str(e)}"
|
| 72 |
|
| 73 |
-
#
|
|
|
|
| 74 |
@tool
|
| 75 |
def detect_ambiguity(content: str) -> str:
|
| 76 |
"""Checks for vague instructions and suggests clarifications.
|
|
@@ -78,32 +70,34 @@ def detect_ambiguity(content: str) -> str:
|
|
| 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 |
-
#
|
|
|
|
| 85 |
@tool
|
| 86 |
def explain_assumed_knowledge(term: str) -> str:
|
| 87 |
-
"""Defines
|
| 88 |
|
| 89 |
Args:
|
| 90 |
term: The term to explain.
|
| 91 |
"""
|
| 92 |
return f"Definition of '{term}': [Detailed beginner-friendly explanation here]"
|
| 93 |
|
| 94 |
-
#
|
|
|
|
| 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 (
|
| 102 |
-
auto_execute: If True, the agent will click the element
|
| 103 |
"""
|
| 104 |
try:
|
| 105 |
driver = get_driver()
|
| 106 |
-
#
|
| 107 |
driver.get("about:blank")
|
| 108 |
if auto_execute:
|
| 109 |
elem = driver.find_element(By.XPATH, element)
|
|
@@ -116,17 +110,19 @@ def highlight_elements(step: str, element: str, auto_execute: bool = False) -> s
|
|
| 116 |
except Exception as e:
|
| 117 |
return f"Error in highlight_elements: {str(e)}"
|
| 118 |
|
| 119 |
-
#
|
|
|
|
| 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 |
-
#
|
|
|
|
| 130 |
@tool
|
| 131 |
def teacher_box_query(question: str) -> str:
|
| 132 |
"""Allows users to ask the AI questions while browsing.
|
|
@@ -136,7 +132,8 @@ def teacher_box_query(question: str) -> str:
|
|
| 136 |
"""
|
| 137 |
return f"AI Answer: [Response for '{question}']"
|
| 138 |
|
| 139 |
-
#
|
|
|
|
| 140 |
@tool
|
| 141 |
def toggle_auto_execution(enable: bool) -> str:
|
| 142 |
"""Lets the user turn automatic navigation on or off.
|
|
@@ -146,19 +143,20 @@ def toggle_auto_execution(enable: bool) -> str:
|
|
| 146 |
"""
|
| 147 |
return "Auto-execution enabled." if enable else "Manual step-by-step mode enabled."
|
| 148 |
|
| 149 |
-
#
|
|
|
|
| 150 |
model = HfApiModel(
|
| 151 |
-
max_tokens=2096,
|
| 152 |
-
temperature=0.5,
|
| 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=[
|
|
@@ -171,10 +169,11 @@ agent = CodeAgent(
|
|
| 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()
|
|
|
|
|
|
| 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
|
| 18 |
|
| 19 |
# Final Answer and UI Handling
|
| 20 |
from tools.final_answer import FinalAnswerTool
|
| 21 |
from Gradio_UI import GradioUI
|
| 22 |
|
| 23 |
+
# --------------------------------------------
|
| 24 |
+
# Helper function to obtain and configure the Selenium driver automatically.
|
| 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.
|
|
|
|
| 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 |
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 to explain technical terms in simple language.
|
| 78 |
@tool
|
| 79 |
def explain_assumed_knowledge(term: str) -> str:
|
| 80 |
+
"""Defines technical terms in a simple way.
|
| 81 |
|
| 82 |
Args:
|
| 83 |
term: The term to explain.
|
| 84 |
"""
|
| 85 |
return f"Definition of '{term}': [Detailed beginner-friendly explanation here]"
|
| 86 |
|
| 87 |
+
# --------------------------------------------
|
| 88 |
+
# Tool to highlight a UI element and optionally perform a click.
|
| 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 XPath).
|
| 96 |
+
auto_execute: If True, the agent will automatically click the element.
|
| 97 |
"""
|
| 98 |
try:
|
| 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)
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
return f"Error in highlight_elements: {str(e)}"
|
| 112 |
|
| 113 |
+
# --------------------------------------------
|
| 114 |
+
# Tool to explain a single line of code.
|
| 115 |
@tool
|
| 116 |
def explain_code_line(line: str) -> str:
|
| 117 |
"""Explains what a line of code does in simple terms.
|
| 118 |
|
| 119 |
Args:
|
| 120 |
+
line: The code line to explain.
|
| 121 |
"""
|
| 122 |
return f"Explanation for: {line} [Insert explanation here]"
|
| 123 |
|
| 124 |
+
# --------------------------------------------
|
| 125 |
+
# Tool to provide an interactive teacher box for user queries.
|
| 126 |
@tool
|
| 127 |
def teacher_box_query(question: str) -> str:
|
| 128 |
"""Allows users to ask the AI questions while browsing.
|
|
|
|
| 132 |
"""
|
| 133 |
return f"AI Answer: [Response for '{question}']"
|
| 134 |
|
| 135 |
+
# --------------------------------------------
|
| 136 |
+
# Tool to toggle between manual and automatic navigation modes.
|
| 137 |
@tool
|
| 138 |
def toggle_auto_execution(enable: bool) -> str:
|
| 139 |
"""Lets the user turn automatic navigation on or off.
|
|
|
|
| 143 |
"""
|
| 144 |
return "Auto-execution enabled." if enable else "Manual step-by-step mode enabled."
|
| 145 |
|
| 146 |
+
# --------------------------------------------
|
| 147 |
+
# Configure the AI Model.
|
| 148 |
model = HfApiModel(
|
| 149 |
+
max_tokens=2096, # Maximum response length.
|
| 150 |
+
temperature=0.5, # Controls response randomness.
|
| 151 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Selected model.
|
| 152 |
custom_role_conversions=None
|
| 153 |
)
|
| 154 |
|
| 155 |
+
# Load prompt templates from a YAML configuration file.
|
| 156 |
with open("prompts.yaml", 'r') as stream:
|
| 157 |
prompt_templates = yaml.safe_load(stream)
|
| 158 |
|
| 159 |
+
# Create the AI agent with the defined tools.
|
| 160 |
agent = CodeAgent(
|
| 161 |
model=model,
|
| 162 |
tools=[
|
|
|
|
| 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 |
+
|