RustX commited on
Commit
e0abc06
ยท
1 Parent(s): f30361a

Create chatbot_csv.py

Browse files
Files changed (1) hide show
  1. chatbot_csv.py +109 -0
chatbot_csv.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from dotenv import load_dotenv
4
+ from io import BytesIO
5
+ from io import StringIO
6
+ import sys
7
+ import re
8
+ from langchain.agents import create_csv_agent
9
+ from langchain.chat_models import ChatOpenAI
10
+ from modules.history import ChatHistory
11
+ from modules.layout import Layout
12
+ from modules.utils import Utilities
13
+ from modules.sidebar import Sidebar
14
+
15
+ #To be able to update the changes made to modules in localhost,
16
+ #you can press the "r" key on the localhost page to refresh and reflect the changes made to the module files.
17
+ def reload_module(module_name):
18
+ import importlib
19
+ import sys
20
+ if module_name in sys.modules:
21
+ importlib.reload(sys.modules[module_name])
22
+ return sys.modules[module_name]
23
+
24
+ history_module = reload_module('modules.history')
25
+ layout_module = reload_module('modules.layout')
26
+ utils_module = reload_module('modules.utils')
27
+ sidebar_module = reload_module('modules.sidebar')
28
+
29
+ ChatHistory = history_module.ChatHistory
30
+ Layout = layout_module.Layout
31
+ Utilities = utils_module.Utilities
32
+ Sidebar = sidebar_module.Sidebar
33
+
34
+ def init():
35
+ load_dotenv()
36
+ st.set_page_config(layout="wide", page_icon="๐Ÿ’ฌ", page_title="ChatBot-CSV")
37
+
38
+
39
+ def main():
40
+
41
+ init()
42
+ layout, sidebar, utils = Layout(), Sidebar(), Utilities()
43
+ layout.show_header()
44
+ user_api_key = utils.load_api_key()
45
+
46
+ if not user_api_key:
47
+ layout.show_api_key_missing()
48
+ else:
49
+ os.environ["OPENAI_API_KEY"] = user_api_key
50
+ uploaded_file = utils.handle_upload()
51
+
52
+ if uploaded_file:
53
+ history = ChatHistory()
54
+ sidebar.show_options()
55
+
56
+ uploaded_file_content = BytesIO(uploaded_file.getvalue())
57
+
58
+ try:
59
+ chatbot = utils.setup_chatbot(
60
+ uploaded_file, st.session_state["model"], st.session_state["temperature"]
61
+ )
62
+ st.session_state["chatbot"] = chatbot
63
+
64
+ if st.session_state["ready"]:
65
+ response_container, prompt_container = st.container(), st.container()
66
+
67
+ with prompt_container:
68
+ is_ready, user_input = layout.prompt_form()
69
+
70
+ history.initialize(uploaded_file)
71
+ if st.session_state["reset_chat"]:
72
+ history.reset(uploaded_file)
73
+
74
+ if is_ready:
75
+ history.append("user", user_input)
76
+ output = st.session_state["chatbot"].conversational_chat(user_input)
77
+ history.append("assistant", output)
78
+
79
+ history.generate_messages(response_container)
80
+
81
+ if st.session_state["show_csv_agent"]:
82
+ query = st.text_input(label="Use CSV agent for precise information about the structure of your csv file / csv ํŒŒ์ผ ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์ •ํ™•ํ•œ ์ •๋ณด๋ฅผ ์–ป์œผ๋ ค๋ฉด CSV ์—์ด์ „ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค", placeholder="ex : how many rows in my file ? / ์˜ˆ: ๋‚ด ํŒŒ์ผ์— ๋ช‡ ๊ฐœ์˜ ํ–‰์ด ์žˆ์Šต๋‹ˆ๊นŒ?")
83
+ if query != "":
84
+
85
+ old_stdout = sys.stdout
86
+ sys.stdout = captured_output = StringIO()
87
+ agent = create_csv_agent(ChatOpenAI(temperature=0), uploaded_file_content, verbose=True, max_iterations=4)
88
+
89
+ result = agent.run(query)
90
+
91
+ sys.stdout = old_stdout
92
+ thoughts = captured_output.getvalue()
93
+
94
+ cleaned_thoughts = re.sub(r'\x1b\[[0-9;]*[a-zA-Z]', '', thoughts)
95
+ cleaned_thoughts = re.sub(r'\[1m>', '', cleaned_thoughts)
96
+
97
+ with st.expander("Show agent's thoughts"):
98
+ st.write(cleaned_thoughts)
99
+
100
+ st.write(result)
101
+
102
+ except Exception as e:
103
+ st.error(f"Error: {str(e)}")
104
+
105
+ sidebar.about()
106
+
107
+
108
+ if __name__ == "__main__":
109
+ main()