CoolFace
Apppublic

awacke1/ComputerUseSeleniumPlaywrightDifflibSKLearnImagehash

sourceHugging Facemitupdated 2y agoView on Hugging Face
1likes
app.py330 linesDownload Raw Back to root
1import streamlit as st2import requests3from bs4 import BeautifulSoup4from PIL import Image5import io6import base647from urllib.parse import urljoin, urlparse8import pandas as pd9import plotly.graph_objects as go10import numpy as np11from difflib import SequenceMatcher12from sklearn.feature_extraction.text import TfidfVectorizer13from sklearn.metrics.pairwise import cosine_similarity14import time15import asyncio16from playwright.sync_api import sync_playwright17import sys18import subprocess19 20def install_playwright_deps():21    try:22        from playwright.sync_api import sync_playwright23        # Install browsers if not already installed24        subprocess.run(['playwright', 'install'], check=True)25    except Exception as e:26        st.error(f"Error installing Playwright dependencies: {str(e)}")27        st.info("Try running 'pip install playwright' and 'playwright install' manually")28 29def initialize_session_state():30    if 'visited_urls' not in st.session_state:31        st.session_state.visited_urls = []32    if 'load_times' not in st.session_state:33        st.session_state.load_times = []34    if 'screenshots' not in st.session_state:35        st.session_state.screenshots = []36    if 'crawl_results' not in st.session_state:37        st.session_state.crawl_results = []38 39def setup_browser():40    """Initialize Playwright browser"""41    try:42        playwright = sync_playwright().start()43        browser = playwright.chromium.launch(headless=True)44        return playwright, browser45    except Exception as e:46        st.error(f"Error setting up browser: {str(e)}")47        return None, None48 49def capture_screenshot(page):50    """Capture screenshot using Playwright"""51    screenshot_bytes = page.screenshot()52    return Image.open(io.BytesIO(screenshot_bytes))53 54def calculate_similarity(text1, text2):55    # Basic similarity56    basic_ratio = SequenceMatcher(None, text1, text2).ratio()57    58    # Semantic similarity59    vectorizer = TfidfVectorizer()60    try:61        tfidf = vectorizer.fit_transform([text1, text2])62        semantic_ratio = cosine_similarity(tfidf[0:1], tfidf[1:2])[0][0]63    except:64        semantic_ratio = 065    66    return basic_ratio, semantic_ratio67 68async def crawl_website(url, max_pages=10, search_term=None):69    visited = set()70    to_visit = {url}71    results = []72    73    try:74        with sync_playwright() as p:75            browser = p.chromium.launch(headless=True)76            page = browser.new_page()77            78            while to_visit and len(visited) < max_pages:79                current_url = to_visit.pop()80                if current_url in visited:81                    continue82                    83                try:84                    page.goto(current_url, wait_until="networkidle")85                    visited.add(current_url)86                    87                    # Extract text content88                    text_content = page.content()89                    90                    # If search term provided, check for matches91                    match_found = search_term.lower() in text_content.lower() if search_term else True92                    93                    if match_found:94                        results.append({95                            'url': current_url,96                            'title': page.title(),97                            'content_preview': text_content[:200],98                            'matches_search': match_found99                        })100                    101                    # Find new links102                    links = page.eval_on_selector_all('a[href]', 'elements => elements.map(el => el.href)')103                    for href in links:104                        absolute_url = urljoin(current_url, href)105                        if urlparse(absolute_url).netloc == urlparse(url).netloc:106                            to_visit.add(absolute_url)107                            108                except Exception as e:109                    st.error(f"Error crawling {current_url}: {str(e)}")110                    111            browser.close()112            113    except Exception as e:114        st.error(f"Error in crawl process: {str(e)}")115        116    return results117 118def main():119    st.title("Web Testing and Crawling Suite")120    initialize_session_state()121    122    # Install dependencies if needed123    with st.spinner("Checking dependencies..."):124        install_playwright_deps()125    126    # Sidebar for tool selection127    tool = st.sidebar.radio(128        "Select Tool",129        ["WebTest", "Crawler", "AI Content Comparison"]130    )131    132    if tool == "WebTest":133        st.header("WebTest - Web Performance Testing")134        url = st.text_input("Enter URL to test")135        interval = st.slider("Time interval between requests (seconds)", 1, 30, 5)136        max_cycles = st.number_input("Number of test cycles", 1, 100, 1)137        138        if st.button("Start Testing"):139            playwright, browser = setup_browser()140            if playwright and browser:141                try:142                    page = browser.new_page()143                    144                    for cycle in range(max_cycles):145                        start_time = time.time()146                        147                        try:148                            page.goto(url, wait_until="networkidle")149                            load_time = time.time() - start_time150                            st.session_state.load_times.append(load_time)151                            152                            # Capture screenshot153                            screenshot = capture_screenshot(page)154                            st.session_state.screenshots.append(screenshot)155                            156                            # Show results157                            st.success(f"Cycle {cycle + 1} completed - Load time: {load_time:.2f}s")158                            st.image(screenshot, caption=f"Screenshot - Cycle {cycle + 1}")159                            160                            # Plot load times161                            fig = go.Figure(data=go.Scatter(162                                x=list(range(1, len(st.session_state.load_times) + 1)),163                                y=st.session_state.load_times,164                                mode='lines+markers'165                            ))166                            fig.update_layout(title="Page Load Times", 167                                            xaxis_title="Cycle",168                                            yaxis_title="Load Time (s)")169                            st.plotly_chart(fig)170                            171                            time.sleep(interval)172                            173                        except Exception as e:174                            st.error(f"Error in cycle {cycle + 1}: {str(e)}")175                            176                finally:177                    browser.close()178                    playwright.stop()179            180    elif tool == "Crawler":181        st.header("Web Crawler")182        base_url = st.text_input("Enter base URL to crawl")183        max_pages = st.number_input("Maximum pages to crawl", 1, 100, 10)184        search_term = st.text_input("Search term (optional)")185        186        if st.button("Start Crawling"):187            results = asyncio.run(crawl_website(base_url, max_pages, search_term))188            st.session_state.crawl_results = results189            190            # Display results191            df = pd.DataFrame(results)192            st.dataframe(df)193            194            # Export options195            if st.button("Export Results"):196                csv = df.to_csv(index=False)197                b64 = base64.b64encode(csv.encode()).decode()198                href = f'<a href="data:file/csv;base64,{b64}" download="crawl_results.csv">Download CSV</a>'199                st.markdown(href, unsafe_allow_html=True)200                201    else:  # AI Content Comparison202        st.header("AI Content Comparison")203        url1 = st.text_input("Enter first URL (AI-generated content)")204        url2 = st.text_input("Enter second URL (Comparison content)")205        206        if st.button("Compare Content"):207            playwright, browser = setup_browser()208            if playwright and browser:209                try:210                    page = browser.new_page()211                    212                    # Get content from first URL213                    page.goto(url1, wait_until="networkidle")214                    content1 = page.content()215                    216                    # Get content from second URL217                    page.goto(url2, wait_until="networkidle")218                    content2 = page.content()219                    220                    # Calculate similarities221                    basic_ratio, semantic_ratio = calculate_similarity(content1, content2)222                    223                    # Display results224                    st.subheader("Similarity Results")225                    col1, col2 = st.columns(2)226                    227                    with col1:228                        st.metric("Basic Similarity", f"{basic_ratio:.2%}")229                    230                    with col2:231                        st.metric("Semantic Similarity", f"{semantic_ratio:.2%}")232                    233                    # Show content previews234                    st.subheader("Content Previews")235                    st.text_area("Content 1 (First 500 chars)", content1[:500])236                    st.text_area("Content 2 (First 500 chars)", content2[:500])237                    238                finally:239                    browser.close()240                    playwright.stop()241 242if __name__ == "__main__":243    main()244    245Goals="""246Computer Use2471. Browser based testing app2482. similar to apps I wrote years ago which would operate a browser then run tests against my web apps including being able to compare any image or text content together to search results from one of my ai programs to determine content overlap which is then used to evaluate the results and update my ai model context data to store anything that was found that adds to the original idea.  When I looked at this problem before I found chrome driver for automatic testing, saucelabs which can kind of do it, and then some python testing libraries which could do it.  Can you enlighten me on which python libraries and potenitally dev tools which would help me with this to automate my testing and evaluation of my ai generated content which resides at many different URLs on huggingface as running apps2493. Past apps per wayback from 2004:  250    - https://web.archive.org/web/20040520102150/http://www.evolvable.com/EStore/251 252WebTest 8.0253WebTest is a stress and load testing browser.254You can use WebTest to identify defects that occur when web sites incurr a large amount of traffic.255To use WebTest, simply visit the pages that you want to test and WebTest remembers your navigation history. You can save the history to a text file that you can open from other machines or other copies of WebTest running on your computer. To perform stress testing, set the time interval you would like WebTest to visit each Url, then set the cycle option to cycle through your site list.256 257Features Include:258Screen Captures259Graph of Page Load Times260Memory of Visited Pages261Tunable Delay and Maximum Wait Time262Hotkeys for Typical Browser Resolution Sizing263XML/XSL based Usability Reporting264 265Crawler 1.0266 267Have you ever wanted to analyze a web site to see what it contains yet didn't want to spend the next few days traversing all the links on the site?268 269With Crawler, you can enter a web address and Crawler visits every page, extracting a reusable list of links from the pages it reviews.270You can save the HTML source code from each page that Crawler encounters so you can analyze the site further, or automatically surf the site and take snapshots using Evolvable WebTest.271 272Crawler is an indespensable tool because it provides results in an easy to use format and also allows you to search for terms across different sites and save the list of links that match your search criteria.273 274Let me help you explore the Python libraries and tools that would be well-suited for automated browser testing and content comparison for AI-generated content.275The most popular and robust options for this type of testing are:276 277Selenium with Python278 279pythonCopyfrom selenium import webdriver280from selenium.webdriver.common.by import By281 282driver = webdriver.Chrome()283driver.get("https://huggingface.co/your-app")284content = driver.find_element(By.CLASS_NAME, "content").text285 286Playwright (more modern alternative to Selenium)287 288pythonCopyfrom playwright.sync_api import sync_playwright289 290with sync_playwright() as p:291    browser = p.chromium.launch()292    page = browser.new_page()293    page.goto("https://huggingface.co/your-app")294    content = page.inner_text(".content")295 296For content comparison and similarity analysis:297 298pythonCopyfrom difflib import SequenceMatcher299from sklearn.feature_extraction.text import TfidfVectorizer300from sklearn.metrics.pairwise import cosine_similarity301 302# Basic text similarity303def similarity_ratio(text1, text2):304    return SequenceMatcher(None, text1, text2).ratio()305 306# More advanced semantic similarity307def semantic_similarity(text1, text2):308    vectorizer = TfidfVectorizer()309    tfidf = vectorizer.fit_transform([text1, text2])310    return cosine_similarity(tfidf[0:1], tfidf[1:2])[0][0]311For image comparison:312pythonCopyfrom PIL import Image313import imagehash314import numpy as np315 316def compare_images(image1_path, image2_path):317    hash1 = imagehash.average_hash(Image.open(image1_path))318    hash2 = imagehash.average_hash(Image.open(image2_path))319    return hash1 - hash2  # Lower number means more similar320Would you like me to create a more comprehensive example that combines these elements into a complete testing framework? I could show you how to:321 322Automate browsing multiple HuggingFace URLs323Extract both text and image content324Compare content using different similarity metrics325Store results in a structured format for model context updates326 327"""328 329st.markdown(Goals)330