Spaces:
Sleeping
Sleeping
DJOMGA TOUKO Peter Charles
commited on
Commit
·
865ba19
1
Parent(s):
2eac26d
fix bug on managin cursor description
Browse files- app.py +13 -8
- app_access_db.py +8 -4
- app_config.py +1 -0
- openai-business-chat-06-utilitaire.ipynb +403 -0
app.py
CHANGED
|
@@ -6,6 +6,9 @@ from app_access_db import *
|
|
| 6 |
|
| 7 |
# model = "gpt-3.5-turbo"
|
| 8 |
model = "gpt-4-turbo"
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# ------------------------------------------------------------------------------------------------
|
| 11 |
# SIDEBAR
|
|
@@ -50,14 +53,15 @@ def submit_openai_key(model=model):
|
|
| 50 |
st.sidebar.write('Please provide the key before')
|
| 51 |
return
|
| 52 |
else:
|
| 53 |
-
client = OpenAI(api_key=openai_key)
|
| 54 |
model = model
|
| 55 |
completion = client.chat.completions.create(
|
| 56 |
model=model,
|
| 57 |
messages=[
|
| 58 |
-
{"role": "system", "content": "You are an assistant giving simple and short answer for question of child"},
|
| 59 |
{"role": "user", "content": "count from 0 to 10"}
|
| 60 |
-
]
|
|
|
|
| 61 |
)
|
| 62 |
st.sidebar.write(f'Simple count : {completion.choices[0].message.content}')
|
| 63 |
|
|
@@ -78,14 +82,15 @@ def askQuestion(model=model, question=''):
|
|
| 78 |
print('Please provide the key before')
|
| 79 |
return 'LLM API is not defined. Please provide the key before'
|
| 80 |
else:
|
| 81 |
-
client = OpenAI(api_key=openai_key)
|
| 82 |
model = model
|
| 83 |
completion = client.chat.completions.create(
|
| 84 |
model=model,
|
| 85 |
messages=[
|
| 86 |
{"role": "system", "content": f'{query_context}'},
|
| 87 |
{"role": "user", "content": f'{question}'}
|
| 88 |
-
]
|
|
|
|
| 89 |
)
|
| 90 |
return completion.choices[0].message.content
|
| 91 |
|
|
@@ -103,7 +108,7 @@ def displayAssistantMessage( assistantMessage: AssistantMessage):
|
|
| 103 |
if assistantMessage.response_data.columns.size == 2:
|
| 104 |
st.bar_chart(assistantMessage.response_data, x=assistantMessage.response_data.columns[0], y=assistantMessage.response_data.columns[1])
|
| 105 |
if assistantMessage.response_data.columns.size == 1:
|
| 106 |
-
st.metric(label=assistantMessage.response_data.columns[0], value=f'{assistantMessage.response_data.values[0]}')
|
| 107 |
|
| 108 |
|
| 109 |
|
|
@@ -129,10 +134,10 @@ if prompt := st.chat_input("What is up?"):
|
|
| 129 |
|
| 130 |
response = askQuestion(question=prompt)
|
| 131 |
# st.code(response, language='sql')
|
| 132 |
-
response_data = run_query(response)
|
| 133 |
# Display assistant response in chat message container
|
| 134 |
assistanMsg = AssistantMessage()
|
| 135 |
-
assistanMsg.sql = response
|
| 136 |
assistanMsg.response_data = response_data
|
| 137 |
displayAssistantMessage(assistanMsg)
|
| 138 |
# with st.chat_message("assistant"):
|
|
|
|
| 6 |
|
| 7 |
# model = "gpt-3.5-turbo"
|
| 8 |
model = "gpt-4-turbo"
|
| 9 |
+
gpt_base_url = None
|
| 10 |
+
# model = "lmstudio-community/Meta-Llama-3-8B-Instruct-GGUF"
|
| 11 |
+
# gpt_base_url = "http://localhost:1234/v1/"
|
| 12 |
|
| 13 |
# ------------------------------------------------------------------------------------------------
|
| 14 |
# SIDEBAR
|
|
|
|
| 53 |
st.sidebar.write('Please provide the key before')
|
| 54 |
return
|
| 55 |
else:
|
| 56 |
+
client = OpenAI(api_key=openai_key, base_url=gpt_base_url)
|
| 57 |
model = model
|
| 58 |
completion = client.chat.completions.create(
|
| 59 |
model=model,
|
| 60 |
messages=[
|
| 61 |
+
{"role": "system", "content": "You are an assistant giving simple and short answer for question of child. No questions, and no explanations"},
|
| 62 |
{"role": "user", "content": "count from 0 to 10"}
|
| 63 |
+
],
|
| 64 |
+
temperature=0
|
| 65 |
)
|
| 66 |
st.sidebar.write(f'Simple count : {completion.choices[0].message.content}')
|
| 67 |
|
|
|
|
| 82 |
print('Please provide the key before')
|
| 83 |
return 'LLM API is not defined. Please provide the key before'
|
| 84 |
else:
|
| 85 |
+
client = OpenAI(api_key=openai_key, base_url=gpt_base_url)
|
| 86 |
model = model
|
| 87 |
completion = client.chat.completions.create(
|
| 88 |
model=model,
|
| 89 |
messages=[
|
| 90 |
{"role": "system", "content": f'{query_context}'},
|
| 91 |
{"role": "user", "content": f'{question}'}
|
| 92 |
+
],
|
| 93 |
+
temperature=0
|
| 94 |
)
|
| 95 |
return completion.choices[0].message.content
|
| 96 |
|
|
|
|
| 108 |
if assistantMessage.response_data.columns.size == 2:
|
| 109 |
st.bar_chart(assistantMessage.response_data, x=assistantMessage.response_data.columns[0], y=assistantMessage.response_data.columns[1])
|
| 110 |
if assistantMessage.response_data.columns.size == 1:
|
| 111 |
+
st.metric(label=assistantMessage.response_data.columns[0], value=f'{assistantMessage.response_data.values[0][0]}')
|
| 112 |
|
| 113 |
|
| 114 |
|
|
|
|
| 134 |
|
| 135 |
response = askQuestion(question=prompt)
|
| 136 |
# st.code(response, language='sql')
|
| 137 |
+
response_data = run_query(response.replace('```',''))
|
| 138 |
# Display assistant response in chat message container
|
| 139 |
assistanMsg = AssistantMessage()
|
| 140 |
+
assistanMsg.sql = response.replace('```','')
|
| 141 |
assistanMsg.response_data = response_data
|
| 142 |
displayAssistantMessage(assistanMsg)
|
| 143 |
# with st.chat_message("assistant"):
|
app_access_db.py
CHANGED
|
@@ -5,19 +5,23 @@ DB_FILENAME = 'irembo_application_4.db'
|
|
| 5 |
|
| 6 |
|
| 7 |
def run_query(query=''):
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
conn = sqlite3.connect(DB_FILENAME)
|
| 10 |
cursor = conn.cursor()
|
| 11 |
-
cursor.execute(
|
| 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 |
-
|
| 21 |
print(df.head())
|
| 22 |
conn.close()
|
| 23 |
return df
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
def run_query(query=''):
|
| 8 |
+
|
| 9 |
+
clean_query = query.replace('```','')
|
| 10 |
+
print(f"clean_query \n {clean_query}")
|
| 11 |
conn = sqlite3.connect(DB_FILENAME)
|
| 12 |
cursor = conn.cursor()
|
| 13 |
+
cursor.execute(clean_query)
|
| 14 |
#data = cursor.fetchall()
|
| 15 |
#print(data)
|
| 16 |
#conn.close()
|
| 17 |
+
print(f"Cursor Description \n {cursor.description}")
|
| 18 |
+
|
| 19 |
df = DataFrame(cursor.fetchall())
|
| 20 |
+
print(df.head())
|
| 21 |
df.columns = [i[0] for i in cursor.description]
|
| 22 |
|
| 23 |
# print(f'Field Names : {field_names}')
|
| 24 |
+
|
| 25 |
print(df.head())
|
| 26 |
conn.close()
|
| 27 |
return df
|
app_config.py
CHANGED
|
@@ -41,5 +41,6 @@ CREATE TABLE service (
|
|
| 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 |
"""
|
|
|
|
| 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 |
+
Important, the output should be in text that can be executed directly wihtout any transformation. Don't return Markdown format
|
| 45 |
|
| 46 |
"""
|
openai-business-chat-06-utilitaire.ipynb
CHANGED
|
@@ -0,0 +1,403 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 85,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"table_application = \"\"\"\n",
|
| 10 |
+
" CREATE TABLE application (\n",
|
| 11 |
+
" application_id int,\n",
|
| 12 |
+
" application_number varchar(10),\n",
|
| 13 |
+
" amount int,\n",
|
| 14 |
+
" amount_paid int,\n",
|
| 15 |
+
" state varchar(10),\n",
|
| 16 |
+
" office_code varchar(10), \n",
|
| 17 |
+
" service_code varchar(10), \n",
|
| 18 |
+
" date_created datetime,\n",
|
| 19 |
+
" date_paid datetime,\n",
|
| 20 |
+
" date_processed datetime,\n",
|
| 21 |
+
" PRIMARY KEY (application_id)\n",
|
| 22 |
+
" );\n",
|
| 23 |
+
"\"\"\"\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"table_office =\"\"\"\n",
|
| 26 |
+
" CREATE TABLE Office (\n",
|
| 27 |
+
" office_code varchar(10),\n",
|
| 28 |
+
" office_name varchar(20),\n",
|
| 29 |
+
" location_code varchar(10),\n",
|
| 30 |
+
" PRIMARY KEY (office_code)\n",
|
| 31 |
+
" );\n",
|
| 32 |
+
"\"\"\"\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"table_location =\"\"\"\n",
|
| 35 |
+
" CREATE TABLE location (\n",
|
| 36 |
+
" location_code varchar(10),\n",
|
| 37 |
+
" location_name varchar(20),\n",
|
| 38 |
+
" PRIMARY KEY (location_code)\n",
|
| 39 |
+
" );\n",
|
| 40 |
+
"\"\"\"\n",
|
| 41 |
+
"\n",
|
| 42 |
+
"table_service =\"\"\"\n",
|
| 43 |
+
" CREATE TABLE service (\n",
|
| 44 |
+
" service_code varchar(10),\n",
|
| 45 |
+
" service_name varchar(20),\n",
|
| 46 |
+
" PRIMARY KEY (service_code)\n",
|
| 47 |
+
" );\n",
|
| 48 |
+
"\"\"\""
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"cell_type": "code",
|
| 53 |
+
"execution_count": 86,
|
| 54 |
+
"metadata": {},
|
| 55 |
+
"outputs": [
|
| 56 |
+
{
|
| 57 |
+
"data": {
|
| 58 |
+
"text/plain": [
|
| 59 |
+
"<sqlite3.Cursor at 0x143b448c0>"
|
| 60 |
+
]
|
| 61 |
+
},
|
| 62 |
+
"execution_count": 86,
|
| 63 |
+
"metadata": {},
|
| 64 |
+
"output_type": "execute_result"
|
| 65 |
+
}
|
| 66 |
+
],
|
| 67 |
+
"source": [
|
| 68 |
+
"# Create the data base\n",
|
| 69 |
+
"import sqlite3\n",
|
| 70 |
+
"\n",
|
| 71 |
+
"DB_FILENAME = 'irembo_application_4.db'\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"conn = sqlite3.connect(DB_FILENAME)\n",
|
| 74 |
+
"cursor = conn.cursor()\n",
|
| 75 |
+
"cursor.execute(table_application)\n",
|
| 76 |
+
"cursor.execute(table_office)\n",
|
| 77 |
+
"cursor.execute(table_location)\n",
|
| 78 |
+
"cursor.execute(table_service)\n"
|
| 79 |
+
]
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"cell_type": "code",
|
| 83 |
+
"execution_count": 78,
|
| 84 |
+
"metadata": {},
|
| 85 |
+
"outputs": [],
|
| 86 |
+
"source": [
|
| 87 |
+
"conn.close()"
|
| 88 |
+
]
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
"cell_type": "code",
|
| 92 |
+
"execution_count": 87,
|
| 93 |
+
"metadata": {},
|
| 94 |
+
"outputs": [],
|
| 95 |
+
"source": [
|
| 96 |
+
"# --\n",
|
| 97 |
+
"# Define Office, Location, Application and Service information\n",
|
| 98 |
+
"# --\n",
|
| 99 |
+
"\n",
|
| 100 |
+
"office_data = [\n",
|
| 101 |
+
" ('O1', 'Office 1', 'L1'),\n",
|
| 102 |
+
" ('O2', 'Office 2', 'L2'),\n",
|
| 103 |
+
" ('O3', 'Office 3', 'L3'),\n",
|
| 104 |
+
" ('O4', 'Office 4', 'L4'),\n",
|
| 105 |
+
" ('O5', 'Office 5', 'L5'),\n",
|
| 106 |
+
" ('O6', 'Office 6', 'L6'),\n",
|
| 107 |
+
" ('O7', 'Office 7', 'L7'),\n",
|
| 108 |
+
" ('O8', 'Office 8', 'L8'),\n",
|
| 109 |
+
" ('O9', 'Office 9', 'L9'),\n",
|
| 110 |
+
" ('O10', 'Office 10', 'L10'),\n",
|
| 111 |
+
" ('O11', 'Office 11', 'L11'),\n",
|
| 112 |
+
" ('O12', 'Office 12', 'L12'),\n",
|
| 113 |
+
" ('O13', 'Office 13', 'L13'),\n",
|
| 114 |
+
" ('O14', 'Office 14', 'L14'),\n",
|
| 115 |
+
" ('O15', 'Office 15', 'L15'),\n",
|
| 116 |
+
" ('O16', 'Office 16', 'L16'),\n",
|
| 117 |
+
" ('O17', 'Office 17', 'L17'),\n",
|
| 118 |
+
"]\n",
|
| 119 |
+
"\n",
|
| 120 |
+
"location_data = [\n",
|
| 121 |
+
" ('L1', 'Location 1'),\n",
|
| 122 |
+
" ('L2', 'Location 2'),\n",
|
| 123 |
+
" ('L3', 'Location 3'),\n",
|
| 124 |
+
" ('L4', 'Location 4'),\n",
|
| 125 |
+
" ('L5', 'Location 5'),\n",
|
| 126 |
+
" ('L6', 'Location 6'),\n",
|
| 127 |
+
" ('L7', 'Location 7'),\n",
|
| 128 |
+
" ('L8', 'Location 8'),\n",
|
| 129 |
+
"]\n",
|
| 130 |
+
"\n",
|
| 131 |
+
"service_data = [\n",
|
| 132 |
+
" ('S1', 'Service 1'),\n",
|
| 133 |
+
" ('S2', 'Service 2'),\n",
|
| 134 |
+
" ('S3', 'Service 3'),\n",
|
| 135 |
+
" ('S4', 'Service 4'),\n",
|
| 136 |
+
" ('S5', 'Service 5'),\n",
|
| 137 |
+
" ('S6', 'Service 6'),\n",
|
| 138 |
+
" ('S7', 'Service 7'),\n",
|
| 139 |
+
" ('S8', 'Service 8'),\n",
|
| 140 |
+
"]\n",
|
| 141 |
+
"\n",
|
| 142 |
+
"conn = sqlite3.connect(DB_FILENAME)\n",
|
| 143 |
+
"cursor = conn.cursor()\n",
|
| 144 |
+
"cursor.executemany('INSERT INTO Office VALUES (?,?,?)', office_data)\n",
|
| 145 |
+
"cursor.executemany('INSERT INTO Location VALUES (?,?)', location_data)\n",
|
| 146 |
+
"cursor.executemany('INSERT INTO Service VALUES (?,?)', service_data)\n",
|
| 147 |
+
"conn.commit()\n",
|
| 148 |
+
"conn.close()\n"
|
| 149 |
+
]
|
| 150 |
+
},
|
| 151 |
+
{
|
| 152 |
+
"cell_type": "code",
|
| 153 |
+
"execution_count": 88,
|
| 154 |
+
"metadata": {},
|
| 155 |
+
"outputs": [],
|
| 156 |
+
"source": [
|
| 157 |
+
"import string\n",
|
| 158 |
+
"import random\n",
|
| 159 |
+
"from datetime import datetime, timedelta\n",
|
| 160 |
+
"\n",
|
| 161 |
+
"states = ['APPROVED','REJECTED','PENDING_PAYMENT', 'PAID']\n",
|
| 162 |
+
"prices = [1000, 10000,25000, 20000, 0]\n",
|
| 163 |
+
"\n",
|
| 164 |
+
"# or a function\n",
|
| 165 |
+
"def gen_datetime(min_year=2021, max_year=datetime.now().year):\n",
|
| 166 |
+
" # generate a datetime in format yyyy-mm-dd hh:mm:ss.000000\n",
|
| 167 |
+
"\n",
|
| 168 |
+
" today_datetime = datetime.now()\n",
|
| 169 |
+
" return today_datetime - timedelta(days=random.randint(1,1100), hours=random.randint(1,23), minutes=random.randint(1,60), seconds=random.randint(1,60))\n",
|
| 170 |
+
" # start = datetime(min_year, 1, 1, 00, 00, 00)\n",
|
| 171 |
+
" # years = max_year - min_year + 1\n",
|
| 172 |
+
" # end = start + timedelta(days=365 * years)\n",
|
| 173 |
+
" # return start + (end - start) * random.random()\n",
|
| 174 |
+
"\n",
|
| 175 |
+
"def generate_random_application(states=states):\n",
|
| 176 |
+
" N = 8\n",
|
| 177 |
+
" application_data = []\n",
|
| 178 |
+
" strformat = '%Y-%m-%d %H:%M:%S'\n",
|
| 179 |
+
" for i in range(100000,150000):\n",
|
| 180 |
+
" creationdate = gen_datetime()\n",
|
| 181 |
+
" price = prices[random.randint(0,4)]\n",
|
| 182 |
+
" application = (\n",
|
| 183 |
+
" i+1,\n",
|
| 184 |
+
" 'A0' + ''.join(random.choices(string.ascii_uppercase + string.digits, k=8)), \n",
|
| 185 |
+
" price, \n",
|
| 186 |
+
" price, \n",
|
| 187 |
+
" states[random.randint(0,3)],\n",
|
| 188 |
+
" 'O' +''.join(str(random.randint(1,8))),\n",
|
| 189 |
+
" 'S' +''.join(str(random.randint(1,8))),\n",
|
| 190 |
+
" creationdate.strftime(strformat),\n",
|
| 191 |
+
" (creationdate + timedelta(hours=9)).strftime(strformat),\n",
|
| 192 |
+
" (creationdate + timedelta(hours=24)).strftime(strformat),\n",
|
| 193 |
+
" )\n",
|
| 194 |
+
" \n",
|
| 195 |
+
" application_data.append(application)\n",
|
| 196 |
+
" return application_data \n",
|
| 197 |
+
"\n",
|
| 198 |
+
"application_data = generate_random_application()\n",
|
| 199 |
+
"\n",
|
| 200 |
+
"conn = sqlite3.connect(DB_FILENAME)\n",
|
| 201 |
+
"cursor = conn.cursor()\n",
|
| 202 |
+
"cursor.executemany('INSERT INTO Application VALUES (?,?,?,?,?,?,?,?,?,?)', application_data)\n",
|
| 203 |
+
"conn.commit()\n",
|
| 204 |
+
"conn.close()\n"
|
| 205 |
+
]
|
| 206 |
+
},
|
| 207 |
+
{
|
| 208 |
+
"cell_type": "code",
|
| 209 |
+
"execution_count": 81,
|
| 210 |
+
"metadata": {},
|
| 211 |
+
"outputs": [
|
| 212 |
+
{
|
| 213 |
+
"name": "stdout",
|
| 214 |
+
"output_type": "stream",
|
| 215 |
+
"text": [
|
| 216 |
+
"[('2024-04-20', 1), ('2024-04-21', 1), ('2024-04-25', 1)]\n"
|
| 217 |
+
]
|
| 218 |
+
}
|
| 219 |
+
],
|
| 220 |
+
"source": [
|
| 221 |
+
"def run_query(query=''):\n",
|
| 222 |
+
" conn = sqlite3.connect(DB_FILENAME)\n",
|
| 223 |
+
" cursor = conn.cursor()\n",
|
| 224 |
+
" cursor.execute(query) \n",
|
| 225 |
+
" data = cursor.fetchall()\n",
|
| 226 |
+
" print(data)\n",
|
| 227 |
+
" conn.close\n",
|
| 228 |
+
"\n",
|
| 229 |
+
"\n",
|
| 230 |
+
"\n",
|
| 231 |
+
"\n",
|
| 232 |
+
"query_trend = \"\"\"\n",
|
| 233 |
+
"SELECT \n",
|
| 234 |
+
" strftime('%Y-%m-%d', date_created) AS application_date,\n",
|
| 235 |
+
" COUNT(*) AS approved_applications\n",
|
| 236 |
+
"FROM \n",
|
| 237 |
+
" application\n",
|
| 238 |
+
"JOIN \n",
|
| 239 |
+
" Office ON application.office_code = Office.office_code\n",
|
| 240 |
+
"JOIN \n",
|
| 241 |
+
" location ON Office.location_code = location.location_code\n",
|
| 242 |
+
"WHERE \n",
|
| 243 |
+
" application.state = 'APPROVED'\n",
|
| 244 |
+
" AND location.location_name = 'Location 1'\n",
|
| 245 |
+
" AND date_created >= date('now', '-9 days') -- last 10 days including today\n",
|
| 246 |
+
" AND date_created <= date('now') -- up to today\n",
|
| 247 |
+
"GROUP BY \n",
|
| 248 |
+
" strftime('%Y-%m-%d', date_created)\n",
|
| 249 |
+
"ORDER BY \n",
|
| 250 |
+
" strftime('%Y-%m-%d', date_created) ASC;\n",
|
| 251 |
+
"\"\"\"\n",
|
| 252 |
+
"\n",
|
| 253 |
+
"run_query(query=query_trend)\n"
|
| 254 |
+
]
|
| 255 |
+
},
|
| 256 |
+
{
|
| 257 |
+
"cell_type": "code",
|
| 258 |
+
"execution_count": 82,
|
| 259 |
+
"metadata": {},
|
| 260 |
+
"outputs": [
|
| 261 |
+
{
|
| 262 |
+
"name": "stdout",
|
| 263 |
+
"output_type": "stream",
|
| 264 |
+
"text": [
|
| 265 |
+
"[('2021-01', 27), ('2021-02', 31), ('2021-03', 30), ('2021-04', 33), ('2021-05', 40), ('2021-06', 28), ('2021-07', 27), ('2021-08', 30), ('2021-09', 31), ('2021-10', 35), ('2021-11', 26), ('2021-12', 33), ('2022-01', 32), ('2022-02', 34), ('2022-03', 39), ('2022-04', 39), ('2022-05', 30), ('2022-06', 29), ('2022-07', 35), ('2022-08', 31), ('2022-09', 37), ('2022-10', 31), ('2022-11', 31), ('2022-12', 22), ('2023-01', 33), ('2023-02', 35), ('2023-03', 34), ('2023-04', 35), ('2023-05', 28), ('2023-06', 32), ('2023-07', 26), ('2023-08', 30), ('2023-09', 26), ('2023-10', 36), ('2023-11', 37), ('2023-12', 39), ('2024-01', 39), ('2024-02', 33), ('2024-03', 29), ('2024-04', 28), ('2024-05', 37), ('2024-06', 33), ('2024-07', 33), ('2024-08', 35), ('2024-09', 34), ('2024-10', 36), ('2024-11', 26), ('2024-12', 41)]\n"
|
| 266 |
+
]
|
| 267 |
+
}
|
| 268 |
+
],
|
| 269 |
+
"source": [
|
| 270 |
+
"query = \"\"\"\n",
|
| 271 |
+
"SELECT \n",
|
| 272 |
+
" strftime('%Y-%m', date_paid) AS month,\n",
|
| 273 |
+
" COUNT(*) AS approved_applications\n",
|
| 274 |
+
"FROM \n",
|
| 275 |
+
" application\n",
|
| 276 |
+
"WHERE \n",
|
| 277 |
+
" amount_paid = amount AND state='APPROVED' AND office_code IN (\n",
|
| 278 |
+
" SELECT \n",
|
| 279 |
+
" o.office_code\n",
|
| 280 |
+
" FROM \n",
|
| 281 |
+
" Office o, location l\n",
|
| 282 |
+
" WHERE \n",
|
| 283 |
+
" o.location_code=l.location_code AND l.location_name='Location 2'\n",
|
| 284 |
+
" )\n",
|
| 285 |
+
"GROUP BY \n",
|
| 286 |
+
" month\n",
|
| 287 |
+
"ORDER BY \n",
|
| 288 |
+
" month;\n",
|
| 289 |
+
"\"\"\"\n",
|
| 290 |
+
"\n",
|
| 291 |
+
"run_query(query=query)"
|
| 292 |
+
]
|
| 293 |
+
},
|
| 294 |
+
{
|
| 295 |
+
"cell_type": "code",
|
| 296 |
+
"execution_count": 83,
|
| 297 |
+
"metadata": {},
|
| 298 |
+
"outputs": [
|
| 299 |
+
{
|
| 300 |
+
"name": "stdout",
|
| 301 |
+
"output_type": "stream",
|
| 302 |
+
"text": [
|
| 303 |
+
"[('2023-04', 34), ('2023-05', 33), ('2023-06', 32), ('2023-07', 34), ('2023-08', 32), ('2023-09', 35), ('2023-10', 37), ('2023-11', 31), ('2023-12', 32), ('2024-01', 36), ('2024-02', 28), ('2024-03', 22)]\n"
|
| 304 |
+
]
|
| 305 |
+
}
|
| 306 |
+
],
|
| 307 |
+
"source": [
|
| 308 |
+
"query=\"\"\"\n",
|
| 309 |
+
"WITH monthly_trend AS (\n",
|
| 310 |
+
" SELECT strftime('%Y-%m', date_created) AS month,\n",
|
| 311 |
+
" COUNT(*) AS approved_applications\n",
|
| 312 |
+
" FROM application\n",
|
| 313 |
+
" WHERE state = 'APPROVED'\n",
|
| 314 |
+
" AND strftime('%Y-%m', date_created) >= strftime('%Y-%m', 'now', '-12 months')\n",
|
| 315 |
+
" AND office_code IN (SELECT office_code FROM Office WHERE location_code = 'L1')\n",
|
| 316 |
+
" GROUP BY month\n",
|
| 317 |
+
")\n",
|
| 318 |
+
"SELECT all_months.month, COALESCE(approved_applications, 0) AS approved_applications\n",
|
| 319 |
+
"FROM (\n",
|
| 320 |
+
" SELECT strftime('%Y-%m', 'now', '-12 months') AS month\n",
|
| 321 |
+
" UNION ALL\n",
|
| 322 |
+
" SELECT strftime('%Y-%m', 'now', '-11 months')\n",
|
| 323 |
+
" UNION ALL\n",
|
| 324 |
+
" SELECT strftime('%Y-%m', 'now', '-10 months')\n",
|
| 325 |
+
" UNION ALL\n",
|
| 326 |
+
" SELECT strftime('%Y-%m', 'now', '-9 months')\n",
|
| 327 |
+
" UNION ALL\n",
|
| 328 |
+
" SELECT strftime('%Y-%m', 'now', '-8 months')\n",
|
| 329 |
+
" UNION ALL\n",
|
| 330 |
+
" SELECT strftime('%Y-%m', 'now', '-7 months')\n",
|
| 331 |
+
" UNION ALL\n",
|
| 332 |
+
" SELECT strftime('%Y-%m', 'now', '-6 months')\n",
|
| 333 |
+
" UNION ALL\n",
|
| 334 |
+
" SELECT strftime('%Y-%m', 'now', '-5 months')\n",
|
| 335 |
+
" UNION ALL\n",
|
| 336 |
+
" SELECT strftime('%Y-%m', 'now', '-4 months')\n",
|
| 337 |
+
" UNION ALL\n",
|
| 338 |
+
" SELECT strftime('%Y-%m', 'now', '-3 months')\n",
|
| 339 |
+
" UNION ALL\n",
|
| 340 |
+
" SELECT strftime('%Y-%m', 'now', '-2 months')\n",
|
| 341 |
+
" UNION ALL\n",
|
| 342 |
+
" SELECT strftime('%Y-%m', 'now', '-1 months')\n",
|
| 343 |
+
") AS all_months\n",
|
| 344 |
+
"LEFT JOIN monthly_trend ON all_months.month = monthly_trend.month;\n",
|
| 345 |
+
"\n",
|
| 346 |
+
"\n",
|
| 347 |
+
"\"\"\"\n",
|
| 348 |
+
"\n",
|
| 349 |
+
"run_query(query=query)"
|
| 350 |
+
]
|
| 351 |
+
},
|
| 352 |
+
{
|
| 353 |
+
"cell_type": "code",
|
| 354 |
+
"execution_count": 84,
|
| 355 |
+
"metadata": {},
|
| 356 |
+
"outputs": [
|
| 357 |
+
{
|
| 358 |
+
"name": "stdout",
|
| 359 |
+
"output_type": "stream",
|
| 360 |
+
"text": [
|
| 361 |
+
"[('2023-04', 25), ('2023-05', 170), ('2023-06', 157), ('2023-07', 153), ('2023-08', 160), ('2023-09', 158), ('2023-10', 165), ('2023-11', 159), ('2023-12', 175), ('2024-01', 186), ('2024-02', 158), ('2024-03', 148), ('2024-04', 148), ('2024-05', 154), ('2024-06', 166), ('2024-07', 158), ('2024-08', 166), ('2024-09', 156), ('2024-10', 164), ('2024-11', 149), ('2024-12', 170)]\n"
|
| 362 |
+
]
|
| 363 |
+
}
|
| 364 |
+
],
|
| 365 |
+
"source": [
|
| 366 |
+
"query =\"\"\" \n",
|
| 367 |
+
"SELECT \n",
|
| 368 |
+
" strftime('%Y-%m', date_processed) AS month, \n",
|
| 369 |
+
" COUNT(*) as approved_applications\n",
|
| 370 |
+
"FROM application\n",
|
| 371 |
+
"WHERE state = 'APPROVED' AND office_code IN (\n",
|
| 372 |
+
" SELECT office_code FROM Office WHERE location_code in ('L1','L2','L3','L4','L5')\n",
|
| 373 |
+
") AND date_processed >= DATE('now', '-12 months')\n",
|
| 374 |
+
"GROUP BY strftime('%Y-%m', date_processed)\n",
|
| 375 |
+
"ORDER BY month;\n",
|
| 376 |
+
"\"\"\"\n",
|
| 377 |
+
"\n",
|
| 378 |
+
"run_query(query=query)"
|
| 379 |
+
]
|
| 380 |
+
}
|
| 381 |
+
],
|
| 382 |
+
"metadata": {
|
| 383 |
+
"kernelspec": {
|
| 384 |
+
"display_name": "sample-projects",
|
| 385 |
+
"language": "python",
|
| 386 |
+
"name": "python3"
|
| 387 |
+
},
|
| 388 |
+
"language_info": {
|
| 389 |
+
"codemirror_mode": {
|
| 390 |
+
"name": "ipython",
|
| 391 |
+
"version": 3
|
| 392 |
+
},
|
| 393 |
+
"file_extension": ".py",
|
| 394 |
+
"mimetype": "text/x-python",
|
| 395 |
+
"name": "python",
|
| 396 |
+
"nbconvert_exporter": "python",
|
| 397 |
+
"pygments_lexer": "ipython3",
|
| 398 |
+
"version": "3.12.2"
|
| 399 |
+
}
|
| 400 |
+
},
|
| 401 |
+
"nbformat": 4,
|
| 402 |
+
"nbformat_minor": 2
|
| 403 |
+
}
|