Spaces:
Build error
Build error
File size: 5,378 Bytes
fd0b99a ba5532a 99ece80 61747c2 0186eac 577064b 0186eac 577064b 0186eac 577064b 0186eac bba9878 ba5532a 8940edd 44f30ad 577064b ba5532a 8940edd ba5532a 99ece80 ba5532a 1b72b3a ba5532a 972a9f4 1cee1f9 972a9f4 ba5532a 1b72b3a 4d6aabb 972a9f4 4d6aabb ba5532a 577064b 61747c2 577064b 61747c2 e5b1596 577064b 61747c2 6ddae11 61747c2 dee08f7 fd0b99a 1ba6387 ba5532a 577064b 8940edd 577064b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import os
import streamlit as st
from dotenv import load_dotenv
from io import BytesIO
from io import StringIO
import sys
import re
from langchain.agents import create_csv_agent
from langchain.chat_models import ChatOpenAI
from src.modules.history import ChatHistory
from src.modules.layout import Layout
from src.modules.utils import Utilities
from src.modules.sidebar import Sidebar
# To be able to update the changes made to modules in localhost,
# you can press the "r" key on the localhost page to refresh and reflect the changes made to the module files.
def reload_module(module_name):
import importlib
import sys
if module_name in sys.modules:
importlib.reload(sys.modules[module_name])
return sys.modules[module_name]
history_module = reload_module('src.modules.history')
layout_module = reload_module('src.modules.layout')
utils_module = reload_module('src.modules.utils')
sidebar_module = reload_module('src.modules.sidebar')
ChatHistory = history_module.ChatHistory
Layout = layout_module.Layout
Utilities = utils_module.Utilities
Sidebar = sidebar_module.Sidebar
def init():
load_dotenv()
st.set_page_config(layout="wide", page_icon="💬", page_title="ChatBot-CSV")
def main():
init()
layout, sidebar, utils = Layout(), Sidebar(), Utilities()
layout.show_header()
user_api_key = utils.load_api_key()
if not user_api_key:
layout.show_api_key_missing()
else:
os.environ["OPENAI_API_KEY"] = user_api_key
uploaded_file = utils.handle_upload()
if uploaded_file:
history = ChatHistory()
sidebar.show_options()
uploaded_file_content = BytesIO(uploaded_file.getvalue())
try:
chatbot = utils.setup_chatbot(
uploaded_file, st.session_state["model"], st.session_state["temperature"]
)
st.session_state["chatbot"] = chatbot
agent = create_csv_agent(ChatOpenAI(temperature=0),
uploaded_file_content,
verbose=True,
max_iterations=15)
st.session_state['agent'] = agent
if st.session_state["ready"]:
response_container, prompt_container = st.container(), st.container()
with prompt_container:
is_ready, user_input = layout.prompt_form()
history.initialize(uploaded_file)
if st.session_state["reset_chat"]:
history.reset(uploaded_file)
if is_ready:
history.append("user", user_input)
output = st.session_state["chatbot"].conversational_chat(user_input)
# history.append("assistant", output)
old_stdout = sys.stdout
sys.stdout = captured_output = StringIO()
agent_answer = agent.run(user_input)
sys.stdout = old_stdout
thoughts = captured_output.getvalue()
cleaned_thoughts = re.sub(r'\x1b\[[0-9;]*[a-zA-Z]', '', thoughts)
cleaned_thoughts = re.sub(r'\[1m>', '', cleaned_thoughts)
resp = cleaned_thoughts.split('Thought:')[-1].split('Final Answer')
thought = resp[0]
final_answer = resp[1].split('\n')[0].split(': ')[-1]
agent_answer_clean = '\n'.join([thought, final_answer])
full_answer = '\n'.join([output, agent_answer_clean])
history.append("assistant", full_answer)
history.generate_messages(response_container)
if st.session_state["show_csv_agent"]:
query = st.text_input(
label="Use CSV agent for precise information about the structure of your csv file",
placeholder="ex : how many rows in my file ?")
if query != "":
old_stdout = sys.stdout
sys.stdout = captured_output = StringIO()
agent = create_csv_agent(ChatOpenAI(temperature=0),
uploaded_file_content,
verbose=True,
max_iterations=4)
result = agent.run(query)
sys.stdout = old_stdout
thoughts = captured_output.getvalue()
cleaned_thoughts = re.sub(r'\x1b\[[0-9;]*[a-zA-Z]', '', thoughts)
cleaned_thoughts = re.sub(r'\[1m>', '', cleaned_thoughts)
with st.expander("Afficher les pensées de l'agent"):
st.write(cleaned_thoughts)
st.write(result)
except Exception as e:
st.error(f"Error: {str(e)}")
sidebar.about()
if __name__ == "__main__":
main()
|