import os import subprocess import sys import requests from bs4 import BeautifulSoup…
- User
import os import subprocess import sys import requests from bs4 import BeautifulSoup import sqlite3 import schedule import time import logging from transformers import pipeline, BertForQuestionAnswering, BertTokenizer, RobertaForQuestionAnswering, RobertaTokenizer from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager from concurrent.futures import ThreadPoolExecutor import hashlib import psutil import torch
def install(package): subprocess.check_call([sys.executable, "-m", "pip", "install", package])
required_packages = ['requests', 'beautifulsoup4', 'selenium', 'webdriver-manager', 'transformers', 'psutil', 'torch'] for package in required_packages: try: import(package) except ImportError: install(package)
logging.basicConfig(level=logging.INFO)
Wybór urządzenia: CUDA (NVIDIA) lub ROCm (AMD) lub CPU
if torch.cuda.is_available(): device = torch.device('cuda') logging.info("Użycie GPU NVIDIA") elif torch.backends.mps.is_available(): # Obsługuje ROCm device = torch.device('mps') logging.info("Użycie GPU AMD") else: device = torch.device('cpu') logging.info("Użycie CPU")
Inicjalizacja modeli
model_qa_1 = BertForQuestionAnswering.from_pretrained("dkleczek/bert-base-polish-uncased-v1").to(device) tokenizer_qa_1 = BertTokenizer.from_pretrained("dkleczek/bert-base-polish-uncased-v1")
model_qa_2 = RobertaForQuestionAnswering.from_pretrained("deepset/roberta-base-squad2").to(device) tokenizer_qa_2 = RobertaTokenizer.from_pretrained("deepset/roberta-base-squad2")
qa_pipeline_1 = pipeline("question-answering", model=model_qa_1, tokenizer=tokenizer_qa_1, device=device.index) qa_pipeline_2 = pipeline("question-answering", model=model_qa_2, tokenizer=tokenizer_qa_2, device=device.index)
chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=chrome_options)
def generate_hash(url): return hashlib.md5(url.encode()).hexdigest()
def scrape_dynamic_data(url): try: driver.get(url) time.sleep(3) # Czekaj na załadowanie strony soup = BeautifulSoup(driver.page_source, 'html.parser') return soup.get_text() except Exception as e: logging.error(f"Błąd podczas pobierania danych z {url}: {e}") return None
def scrape_data(url): cached_data = check_cache(url) if cached_data: logging.info(f"Pobieranie danych z cache dla: {url}") return cached_data if "dynamic" in url: return scrape_dynamic_data(url) else: try: response = requests.get(url) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') store_cache(url, soup.get_text()) return soup.get_text() except requests.RequestException as e: logging.error(f"Błąd podczas pobierania danych z {url}: {e}") return None
- Assistant
This script is a complex system combining web scraping, caching, data storage, and a question-answering AI. Here's a breakdown of its functionality:
- User
repair this code
- Assistant