simbake / web_search

web search extension for text-generation-webui
GNU General Public License v3.0
92 stars 19 forks source link

Releases v1.0 update #8

Closed tijo95 closed 11 months ago

tijo95 commented 11 months ago

some modifications to version 1.0

Improved Handling of Online Search Results In this version, we improved the handling of online search results by using search_data[0] to access relevant data. This simplifies the manipulation of search results, ensuring that we can extract the needed information more directly and efficiently.

import gradio as gr
import modules.shared as shared
from googlesearch import search
from bs4 import BeautifulSoup
import re
import requests

# Global variable to enable/disable Google search
search_access = False

# Function to create user interface
def ui():
    global search_access
    checkbox = gr.Checkbox(value=search_access, label="Activate Google Search.")
    checkbox.change(fn=update_search_access, inputs=checkbox)
    return checkbox, search_access

# Function to update access to Google search.
def update_search_access(checkbox_value):
    global search_access
    search_access = checkbox_value
    return search_access, checkbox_value

# User input modification function
def input_modifier(user_input, state):
    global search_access
    if search_access:
        if user_input.lower().startswith("search"):
            shared.processing_message = "*Search online...*"
            try:
                query = user_input.replace("search", "").strip()
                search_results = search(query, num_results=1)
                search_data = [(get_text(result), result) for result in search_results]
                state["context"] = "Summarize the results online and answer the question correctly, providing all the information in as much detail as possible, with links if necessary."
                user_prompt = f"{user_input}\n results online: {search_data[0]}"
                return user_prompt
            except Exception as e:
                print(type(e), e)
                state["context_instruct"] = "Notify user of connection problems"
                return ""
    shared.processing_message = "*Typing...*"
    return user_input

# Output modification function
def output_modifier(output):
    return output

# Bot prefix modification function
def bot_prefix_modifier(prefix):
    return prefix

# Function for displaying data (not used in this code)
def print_data(data):
    return data

# Function to extract text from a URL
def get_text(url):
    response = requests.get(url)
    soup = "\n".join([p.text for p in BeautifulSoup(response.text, "html.parser").find_all(["section", "p"])])
    soup = soup[:2044]
    text = re.sub(r"\s+", " ", soup.strip())
    output = "\n".join([text, f" -Source: {response.url}"])
    return output
simbake commented 11 months ago

Please specify what you changed.

simbake commented 11 months ago

I will make a v1.0 branch and see if your changes will be viable there as somethings have changed. Thanks.

simbake commented 11 months ago

Hi, sorry for the long wait. I made the branch and put your changes there. The branch is called v1.0. Please check it out and you can do pull requests for changes to v1.0, thanks.

simbake commented 11 months ago

Closing this now, you can reopen if you have anything else.

tijo95 commented 11 months ago

thanks for your feedback . me too I'm pretty rare on github lol .

simbake commented 11 months ago

Welcome! Have you tried the new version?