import os import requests import json from bs4 import BeautifulSoup import streamlit as…
- User
import os import requests import json from bs4 import BeautifulSoup import streamlit as st from huggingface_hub import InferenceClient from datasets import load_dataset from duckduckgo_search import DDGS import yaml # Importing YAML module
Load the dataset
ds = load_dataset("nvidia/HelpSteer2")
Initialize Hugging Face client
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
Load premade prompts from YAML with error handling
def load_premade_prompts(file_path): try: with open(file_path, 'r', encoding='utf-8') as file: return yaml.safe_load(file) # Load YAML instead of JSON except yaml.YAMLError as e: st.error(f"Error decoding YAML: {e}") return {} except Exception as e: st.error(f"An error occurred: {e}") return {}
Load prompts
prompts = load_premade_prompts("J:/project_relapse/agent_jobs/templates/premade_prompts.yaml").get("categories", {})
Specify the output file for scraped content
scraped_content_file = "templates/scraped_content.txt"
Function to search DuckDuckGo
def search_duckduckgo(query): results = DDGS().text(query, max_results=5) return results
Function to scrape text and URLs from a page
def scrape_all_text_and_urls(url, is_email_content=False): try: response = requests.get(url) response.raise_for_status() # Raise an HTTPError for 4xx and 5xx status codes soup = BeautifulSoup(response.text, 'html.parser')
codeif is_email_content: email_content = soup.find_all(text=True) clean_text = ' '.join(email_content).replace('\n', ' ').strip() return clean_text all_text = soup.get_text(separator='\n', strip=True) return all_text except Exception as e: st.error(f"Error scraping the website '{url}': {str(e)}") return NoneFunction to chunk content
def chunk_content(content, max_chunk_size): chunks = [] words = content.split() for i in range(0, len(words), max_chunk_size): chunks.append(' '.join(words[i:i + max_chunk_size])) return chunks
Function to summarize content with Hugging Face model
def summarize_content_with_huggingface(content, prompt, max_tokens=4096): chunks = chunk_content(content, 1500) # Adjust chunk size based on model limits summary = "" summary_output = st.empty() # Create a placeholder for the summary output
code - Assistant
The provided code primarily saves the scraped content to a file named
scraped_content_file, but it does not appear to explicitly save the summarized response generated by the Hugging Face model to a file. Here's how your code handles saving:-
Scraped Content Saving:
- The scraped content is saved to
scraped_content_file, which is a file path defined as"templates/scraped_content.txt".
pythonif scraped_content: with open(scraped_content_file, 'w', encoding='utf-8') as file: file.write(scraped_content) st.success("Content scraped successfully and saved to output file.") - The scraped content is saved to
-
- User
J:\project_relapse\agent_jobs\scraped_data
can you update it to save a unique filename in this folder please of the generated response
- Assistant
To save the generated summary content to a unique filename in the specified directory (
J:\project_relapse\agent_jobs\scraped_data), you can incorporate a timestamp or a unique identifier in the filename. This ensures that each file saved is distinct and does not overwrite previous files. Here's how you can modify your code to achieve this: - User
- Assistant