import gradio as gr from huggingface_hub import InferenceClient import json from bs4 import BeautifulSoup import requests from transformers import LlavaProcessor, LlavaForConditionalGeneration, TextIteratorStreamer from threading import Thread import torch model_id = "llava-hf/llava-interleave-qwen-0.5b-hf" processor = LlavaProcessor.from_pretrained(model_id) model = LlavaForConditionalGeneration.from_pretrained(model_id) model.to("cpu") def extract_text_from_webpage(html_content): soup = BeautifulSoup(html_content, 'html.parser') for tag in soup(["script", "style", "header", "footer"]): tag.extract() return soup.get_text(strip=True) def search(query): term = query all_results = [] max_chars_per_page = 8000 with requests.Session() as session: resp = session.get( url="https://www.google.com/search", headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"}, params={"q": term, "num": 3, "udm": 14}, timeout=5, ) resp.raise_for_status() soup = BeautifulSoup(resp.text, "html.parser") result_block = soup.find_all("div", attrs={"class": "g"}) for result in result_block: link = result.find("a", href=True)["href"] try: webpage = session.get(link, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"}, timeout=5) webpage.raise_for_status() visible_text = extract_text_from_webpage(webpage.text) if len(visible_text) > max_chars_per_page: visible_text = visible_text[:max_chars_per_page] all_results.append({"link": link, "text": visible_text}) except requests.exceptions.RequestException: all_results.append({"link": link, "text": None}) return all_results # Initialize inference clients for different models client_gemma = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3") client_mixtral = InferenceClient("NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO") client_llama = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct") # Define the main chat function def respond(question, history): func_caller = [] user_prompt = question functions_metadata = [ {"type": "function", "function": {"name": "web_search", "description": "Search query on google", "parameters": {"type": "object", "properties": {"query": {"type": "string", "description": "web search query"}}, "required": ["query"]}}}, {"type": "function", "function": {"name": "general_query", "description": "Reply general query of USER", "parameters": {"type": "object", "properties": {"prompt": {"type": "string", "description": "A detailed prompt"}}, "required": ["prompt"]}}}, ] for msg in history: func_caller.append({"role": "user", "content": f"{str(msg[0])}"}) func_caller.append({"role": "assistant", "content": f"{str(msg[1])}"}) func_caller.append({"role": "user", "content": f'[SYSTEM]You are a helpful assistant. You have access to the following functions: \n {str(functions_metadata)}\n\nTo use these functions respond with:\n {{ "name": "function_name", "arguments": {{ "arg_1": "value_1", "arg_1": "value_1", ... }} }} [USER] {question}'}) response = client_gemma.chat_completion(func_caller, max_tokens=200) response = str(response) try: response = response[int(response.find("{")):int(response.rindex("