import streamlit as st from streamlit_chat import message import os import io from dotenv import load_dotenv import requests import glob import json import shutil from RBotReloaded import SmartAgent, MODELS_DIR import time from PIL import Image from langchain.schema import AIMessage, HumanMessage load_dotenv() default_model = "" default_context = 8192 default_load_type = "Auto" default_iterations = 3 default_temperature = 0.2 default_topp = 0.95 @st.cache_resource def agent(model, temperature, top_p, context_length, load_8bit, load_4bit, max_iterations): ag = SmartAgent(f"{MODELS_DIR}/{model}" if os.path.exists(f"{MODELS_DIR}/{model}") else model, temp=temperature, top_p=top_p, load_in_4bit=load_4bit, load_in_8bit=load_8bit, ctx_len=context_length, max_iterations=max_iterations) if model else None st.session_state["temperature_executive"] = temperature st.session_state["max_iterations_executive"] = max_iterations st.session_state["model_executive"] = model st.session_state["context_length_executive"] = context_length st.session_state["load_options_executive"] = "Load 4-bit" if load_8bit else "Load 4-bit" if load_4bit else "Auto" st.session_state["top_p_executive"] = top_p return ag def get_models(): supported_extensions = ["bin","pth","gguf"] models_directory = f"{MODELS_DIR}" # Replace with the actual path # Use os.listdir to get a list of filenames in the directory os.makedirs(models_directory, exist_ok=True) models = os.listdir(models_directory) # Filter out any subdirectories, if any models = [model for model in models if (model.lower().split(".")[-1] in supported_extensions) and os.path.isfile(os.path.join(models_directory, model))] models.append("garage-bAInd/Platypus2-70B-instruct") if len(models) == 0: st.write("Downloading models") from huggingface_hub import hf_hub_download st.write("Downloading mistral-7b-instruct-v0.1.Q4_K_M.gguf") hf_hub_download(repo_id="TheBloke/Mistral-7B-Instruct-v0.1-GGUF", filename="mistral-7b-instruct-v0.1.Q4_K_M.gguf", local_dir=models_directory) st.write("Downloading dreamshaper_8.safetensors") hf_hub_download(repo_id="digiplay/DreamShaper_8", filename="dreamshaper_8.safetensors", local_dir=models_directory) st.experimental_rerun() #models.append("http://localhost:5000") #to use it with text gen webui return models def current_agent(): model = st.session_state.get("model", default_model) temperature = st.session_state.get("temperature", default_temperature) max_iterations = st.session_state.get("max_iterations", default_iterations) context_length = st.session_state.get("context_length", default_context) load_options = st.session_state.get("load_options", default_load_type) top_p = st.session_state.get("top_p", default_topp) model = st.session_state.get("model_executive", model) temperature = st.session_state.get("temperature_executive", temperature) max_iterations = st.session_state.get("max_iterations_executive", max_iterations) context_length = st.session_state.get("context_length_executive", context_length) load_options = st.session_state.get("load_options_executive", load_options) top_p = st.session_state.get("top_p_executive", top_p) return agent(model, temperature, top_p, context_length, load_options=="Load 8-bit", load_options=="Load 4-bit", max_iterations) def history(): return [] if current_agent() is None else current_agent().chat_history #@st.cache_data def generate_text(input): start_time = time.time() output = "Error: Model not Loaded!" if current_agent() is None else current_agent().agent_generate_response(input) end_time = time.time() elapsed_time = end_time - start_time print(f"\n----------------------") print(f"Agent Reply: {output} - Input: {input}") print(f"Elapsed Time: {elapsed_time} seconds") print(f"Agent Reply: {output}") print(f"\n----------------------") return output + f" ({round(elapsed_time,2)}s)" def get_generated_files(): # Specify the directory path where the generated images are stored directory = "./generated_images" # Get the list of files in the directory files = glob.glob(f"{directory}/*.jpg") # Modify the file extension as per your generated image format # Return the list of file paths return files # Function to list files in the "./knowledge_base/" folder def list_files_in_knowledge_base_folder(): knowledge_base_folder = "./knowledge_base/" files = os.listdir(knowledge_base_folder) return [file for file in files if os.path.isfile(os.path.join(knowledge_base_folder, file))] # Function to add a file to the "./knowledge_base/" folder def add_file_to_knowledge_base(file): knowledge_base_folder = "./knowledge_base/" final_path = os.path.join(knowledge_base_folder, file.name) with open(final_path, "wb") as f: f.write(file.read()) if current_agent() is None: st.error("Model Not Loaded!") else: current_agent().memory_chain.addDocumentToMemory(os.path.join(knowledge_base_folder, file.name)) # Function to add a file to the "./knowledge_base/" folder def set_image_gen_guide(file): bytes_data = io.BytesIO(file.read()) image = Image.open(bytes_data) image = image.convert("RGB") image.save("./image_gen_guide.jpg") def unset_image_gen_guide(): if os.path.exists("./image_gen_guide.jpg"): os.remove("./image_gen_guide.jpg") def get_index_size(): index_file_path = "./knowledge_base/index.faiss" # Replace with the actual path to your index file if os.path.exists(index_file_path): index_size = os.path.getsize(index_file_path) return index_size / 1024 else: print(f"{index_file_path} does not exist or is not accessible.") return 0 # @cl.langchain_factory(use_async=True) # def factory(): # return current_agent().smartAgent def render_simple_chat(): st.markdown("