DJOMGA TOUKO Peter Charles commited on
Commit
6748588
1 Parent(s): c358276

initial commit

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.db filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ processed/embeddings.csv
2
+ processed/scraped.csv
3
+ .DS_Store
4
+ __pycache__
.streamlit/config.toml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ [server]
2
+ runOnSave = true
3
+ headless = true
4
+ maxUploadSize = 2000
README.md CHANGED
@@ -10,3 +10,38 @@ pinned: false
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
13
+
14
+
15
+
16
+ # AI Chatbot Prototype on Business Insights
17
+
18
+ Having a sample database with a structure
19
+ ```
20
+ - application
21
+ - app_number
22
+ - amount
23
+ - amount_paid
24
+ - state. (APPROVED, REJECTED, PENDING_PAYMENT, PAID)
25
+ - office_code [FK]
26
+ - service_code [FK]
27
+ - date_created
28
+ - date_paid
29
+ - date_processed
30
+ - office
31
+ - office_name
32
+ - office_location_code [FK]
33
+ - location
34
+ - location_name
35
+ - location_code
36
+ - service
37
+ - service_code
38
+ - service_name
39
+ ```
40
+
41
+ The chatbot will provide answers from that database
42
+
43
+ ```
44
+ a- The number of applications rejected in a location during the current month
45
+ b- The trend of applications in particular states, for a location
46
+ c- Any question you think relevant from this DB
47
+ ```
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from openai import OpenAI
3
+ from app_config import *
4
+ from app_access_db import *
5
+
6
+
7
+ model = "gpt-3.5-turbo"
8
+
9
+ # ------------------------------------------------------------------------------------------------
10
+ # SIDEBAR
11
+ # ------------------------------------------------------------------------------------------------
12
+ st.sidebar.title('OpenAI Business Chat')
13
+ st.sidebar.write('This chat bot is build with Tools and Function feature of OpenAI to be able to answer question regarding applications and performance of officers')
14
+ st.sidebar.markdown("""
15
+ ### Having a sample database with a structure
16
+ - application
17
+ - app_number
18
+ - amount
19
+ - amount_paid
20
+ - state. (APPROVED, REJECTED, PENDING_PAYMENT, PAID)
21
+ - office_code [FK]
22
+ - service_code [FK]
23
+ - date_created
24
+ - date_paid
25
+ - date_processed
26
+ - office
27
+ - office_name
28
+ - office_location_code [FK]
29
+ - location
30
+ - location_name
31
+ - location_code
32
+ - service
33
+ - service_code
34
+ - service_name
35
+
36
+ ### The chatbot will provide answers from that database
37
+ - The number of applications rejected is a location during the current month
38
+ - The trend of applications in particular states, for a location
39
+ - Any question you think relevant from this DB
40
+ """)
41
+
42
+ def onchange_openai_key():
43
+ print(st.session_state.openai_key)
44
+
45
+ openai_key = st.sidebar.text_input('OpenAI key', on_change=onchange_openai_key, key='openai_key')
46
+
47
+ def submit_openai_key(model=model):
48
+ if(openai_key == None or openai_key==''):
49
+ st.sidebar.write('Please provide the key before')
50
+ return
51
+ else:
52
+ client = OpenAI(api_key=openai_key)
53
+ model = model
54
+ completion = client.chat.completions.create(
55
+ model=model,
56
+ messages=[
57
+ {"role": "system", "content": "You are an assistant giving simple and short answer for question of child"},
58
+ {"role": "user", "content": "count from 0 to 10"}
59
+ ]
60
+ )
61
+ st.sidebar.write(f'Simple count : {completion.choices[0].message.content}')
62
+
63
+ submit_key = st.sidebar.button(label='Submit', on_click=submit_openai_key)
64
+
65
+
66
+
67
+ # ------------------------------------------------------------------------------------------------
68
+ # CHAT
69
+ # ------------------------------------------------------------------------------------------------
70
+
71
+ st.title('OpenAI Business Chat')
72
+ st.write(f'Ask any question that can be answer by the LLM {model}.')
73
+
74
+
75
+ def askQuestion(model=model, question=''):
76
+ if(openai_key == None or openai_key==''):
77
+ print('Please provide the key before')
78
+ return 'LLM API is not defined. Please provide the key before'
79
+ else:
80
+ client = OpenAI(api_key=openai_key)
81
+ model = model
82
+ completion = client.chat.completions.create(
83
+ model=model,
84
+ messages=[
85
+ {"role": "system", "content": f'{query_context}'},
86
+ {"role": "user", "content": f'{question}'}
87
+ ]
88
+ )
89
+ return completion.choices[0].message.content
90
+
91
+ class AssistantMessage:
92
+ def __init__(self):
93
+ self.sql : str
94
+ self.response_data : DataFrame
95
+
96
+
97
+
98
+ def displayAssistantMessage( assistantMessage: AssistantMessage):
99
+ with st.chat_message("assistant"):
100
+ st.code(assistantMessage.sql, language='sql')
101
+ st.code(assistantMessage.response_data, language='markdown')
102
+ if assistantMessage.response_data.columns.size == 2:
103
+ st.bar_chart(assistantMessage.response_data, x=assistantMessage.response_data.columns[0], y=assistantMessage.response_data.columns[1])
104
+ if assistantMessage.response_data.columns.size == 1:
105
+ st.metric(label=assistantMessage.response_data.columns[0], value=f'{assistantMessage.response_data.values[0]}')
106
+
107
+
108
+
109
+ # Initialize chat history
110
+ if "messages" not in st.session_state:
111
+ st.session_state.messages = []
112
+
113
+ # Display chat messages from history on app rerun
114
+ for message in st.session_state.messages:
115
+ if message["role"] == "user":
116
+ with st.chat_message(message["role"]):
117
+ st.markdown(message["content"])
118
+ elif message["role"] == "assistant":
119
+ displayAssistantMessage(message["content"])
120
+
121
+ # React to user input
122
+ if prompt := st.chat_input("What is up?"):
123
+ with st.status('Running', expanded=True) as status:
124
+ # Display user message in chat message container
125
+ st.chat_message("user").markdown(prompt)
126
+ # Add user message to chat history
127
+ st.session_state.messages.append({"role": "user", "content": prompt})
128
+
129
+ response = askQuestion(question=prompt)
130
+ st.code(response, language='sql')
131
+ response_data = run_query(response)
132
+ # Display assistant response in chat message container
133
+ assistanMsg = AssistantMessage()
134
+ assistanMsg.sql = response
135
+ assistanMsg.response_data = response_data
136
+ displayAssistantMessage(assistanMsg)
137
+ # with st.chat_message("assistant"):
138
+ # st.code(response, language='sql')
139
+ # st.caption(response_data)
140
+ # st.bar_chart(response_data, x=response_data.columns[0], y=response_data.columns[1])
141
+
142
+ # Add assistant response to chat history
143
+ st.session_state.messages.append({"role": "assistant", "content": assistanMsg})
144
+ status.update(label='Response of last question', state="complete", expanded=True)
145
+
146
+
147
+
app_access_db.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ from pandas import DataFrame
3
+
4
+ DB_FILENAME = 'irembo_application_4.db'
5
+
6
+
7
+ def run_query(query=''):
8
+ print(query)
9
+ conn = sqlite3.connect(DB_FILENAME)
10
+ cursor = conn.cursor()
11
+ cursor.execute(query)
12
+ #data = cursor.fetchall()
13
+ #print(data)
14
+ #conn.close()
15
+
16
+ df = DataFrame(cursor.fetchall())
17
+ df.columns = [i[0] for i in cursor.description]
18
+
19
+ # print(f'Field Names : {field_names}')
20
+ print(cursor.description)
21
+ print(df.head())
22
+ conn.close()
23
+ return df
app_config.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ query_context = """
2
+ Given the following SQL tables, your job is to write queries given a user’s request.
3
+
4
+ CREATE TABLE application (
5
+ application_id int,
6
+ application_number varchar(10),
7
+ amount int,
8
+ amount_paid int,
9
+ state varchar(10),
10
+ office_code varchar(10),
11
+ service_code varchar(10),
12
+ date_created datetime,
13
+ date_paid datetime,
14
+ date_processed datetime,
15
+ PRIMARY KEY (application_id),
16
+ FOREIGN KEY(office_code) REFERENCES Office(office_code),
17
+ FOREIGN KEY(service_code) REFERENCES Service(service_code)
18
+
19
+ );
20
+
21
+ CREATE TABLE Office (
22
+ office_code varchar(10),
23
+ office_name varchar(20),
24
+ location_code varchar(10),
25
+ PRIMARY KEY (office_code),
26
+ FOREIGN KEY(location_code) REFERENCES location(location_code)
27
+ );
28
+
29
+ CREATE TABLE location (
30
+ location_code varchar(10),
31
+ location_name varchar(20),
32
+ PRIMARY KEY (location_code)
33
+ );
34
+
35
+ CREATE TABLE service (
36
+ service_code varchar(10),
37
+ service_name varchar(20),
38
+ PRIMARY KEY (service_code)
39
+ );
40
+
41
+
42
+ Important, The query should be in SQLite format
43
+ Important, Your response should be only the SQL script in SQLite format with no comment and no explanation.
44
+
45
+ """
irembo_application_4.db ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a7b01b7ad04e5a60032efc417d0ef6165d20d9b367d0cb72bcb056e6714e0019
3
+ size 5910528
openai-business-chat-06-utilitaire.ipynb ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ openai
3
+ watchdog