petrified commited on
Commit
a1559c3
·
verified ·
1 Parent(s): 312154b

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +196 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sqlalchemy import create_engine
3
+ import pandas as pd
4
+ import openai
5
+ import os
6
+ from lida import Manager, TextGenerationConfig, llm
7
+ from llmx.generators.text.openai_textgen import OpenAITextGenerator
8
+ from langchain_openai import AzureChatOpenAI
9
+ from langchain_core.runnables import RunnablePassthrough
10
+ from langchain_core.prompts import ChatPromptTemplate
11
+ from langchain_core.output_parsers import StrOutputParser
12
+ import pandas as pd
13
+ import base64
14
+ import numpy as np
15
+ from PIL import Image
16
+ from langchain_core.messages import HumanMessage
17
+ from langchain_openai import ChatOpenAI
18
+ import base64
19
+
20
+ os.environ["AZURE_OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
21
+ os.environ["AZURE_OPENAI_API_VERSION"] = "2023-06-01-preview"
22
+ os.environ["AZURE_OPENAI_ENDPOINT"] = os.getenv("AZURE_OPENAI_ENDPOINT")
23
+ db_host = os.getenv('DB_HOST')
24
+ db_name = os.getenv('DB_NAME')
25
+ db_user = os.getenv('DB_USER')
26
+ db_password = os.getenv('DB_PASSWORD')
27
+
28
+ model = AzureChatOpenAI(
29
+ deployment_name="CapSuiteGPT4omini",
30
+ openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
31
+ )
32
+
33
+ def choose_table(question):
34
+ try:
35
+ connection_string = f'postgresql+psycopg2://{db_user}:{db_password}@{db_host}/{db_name}'
36
+ engine = create_engine(connection_string)
37
+ capsuite_ref = 'foodBeverageSample1'
38
+
39
+
40
+ model = AzureChatOpenAI(
41
+ deployment_name="CapSuiteGPT4omini",
42
+ openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
43
+ )
44
+
45
+ table_format = """
46
+ 1.table name:cdp_sale_order,
47
+ its columns:trxn_id,member_id,staff_id,subsidiary_name,staff_name,team_name,trxn_ref,trxn_channel,trxn_date,trxn_year,trxn_month,trxn_day,trxn_week,remark.
48
+
49
+ 2.table name:cdp_sale_order_line,
50
+ its columns:trxn_item_id,trxn_id,trxn_item_target_curr_unit_price,
51
+ trxn_item_qty,trxn_item_discount_amt,trxn_original_net_currency,trxn_date,trxn_channel,staff_name,staff_id,member_id,display_name,pord_sku,prod_category,prod_type,prod_name,
52
+ capsuite_ref.
53
+
54
+ 3.table name:cdp_stock_quant,
55
+ its columns:stock_quant_id,prod_id,location_id,stock_quantity,stock_quantity_reserved,stock_quant_create_date,capsuite_ref.
56
+ """
57
+
58
+ prompt = ChatPromptTemplate.from_template("Base on the question:{question},"
59
+ "And the following table format:{table_format},"
60
+ "Dont write a complex query. Only select statement like 'select * from table_name'."
61
+ "Dont add any condition or filter to the query. The query should be generic and should return all the data from the table."
62
+ "Select all the columns from the table. "
63
+ "Only output one SQL Query without any other information even the '''sql''' prefix. ")
64
+
65
+ chain = (
66
+ {"question": RunnablePassthrough(), "table_format": RunnablePassthrough()}
67
+ # {"table_format": RunnablePassthrough()}
68
+ | prompt
69
+ | model
70
+ | StrOutputParser()
71
+ )
72
+ # query = 'select * from cdp_membership_summary;'
73
+ query = chain.invoke({"question": question, "table_format": table_format})
74
+ query = query.replace(f"`", '')
75
+ query = query.replace(f"sql", '')
76
+ query = query.split(';')[0] + f' where capsuite_ref = \'{capsuite_ref}\';'
77
+ df_data = pd.read_sql(query, engine)
78
+ print(f'*'*50)
79
+ print(f"Query: {query}")
80
+
81
+ if 'cdp_sale_order_line' in query:
82
+ df_data['sales_amount'] = df_data['trxn_item_target_curr_unit_price'].astype(float) * df_data['trxn_item_qty'].astype(float)
83
+ df_data.rename(columns={'trxn_item_target_curr_unit_price':'unit_price'}, inplace=True)
84
+ df_data.rename(columns={'display_name':'customer_name'}, inplace=True)
85
+ df_data = df_data[['trxn_item_id','trxn_id','sales_amount','unit_price','trxn_item_qty','trxn_item_discount_amt','trxn_date','trxn_channel','staff_name','customer_name','prod_category','prod_type','prod_name','capsuite_ref']]
86
+
87
+ except Exception as e:
88
+ print(f"Error while: {e}")
89
+
90
+ finally:
91
+ engine.dispose()
92
+ return df_data
93
+
94
+
95
+ # Function to encode the image
96
+ def encode_image(image_path):
97
+ with open(image_path, "rb") as image_file:
98
+ return base64.b64encode(image_file.read()).decode('utf-8')
99
+
100
+
101
+ def random_response(message, history):
102
+ df_data = choose_table(message)
103
+
104
+ question = message
105
+ # fill na with empty string
106
+ df_data.fillna('', inplace=True)
107
+ # loop columns, if column is object type, convert to string
108
+ for col in df_data.columns:
109
+ if df_data[col].dtype == 'object':
110
+ df_data[col] = df_data[col].astype(str)
111
+
112
+ text_gen = OpenAITextGenerator(
113
+ provider='openai',
114
+ api_type='azure',
115
+ azure_endpoint= os.getenv('AZURE_OPENAI_ENDPOINT'),
116
+ api_key= os.getenv('OPENAI_API_KEY'),
117
+ api_version = '2023-05-15',
118
+
119
+ )
120
+ lida = Manager(text_gen=text_gen)
121
+
122
+ text_gen_config = TextGenerationConfig(
123
+ n = 1,
124
+ model = 'CapSuiteGPT35T16K',
125
+ temperature=0.1
126
+ )
127
+
128
+ summary = lida.summarize(df_data)
129
+
130
+ print(f'*'*50)
131
+ goals = lida.goals(summary, n=1, textgen_config=text_gen_config,persona=f'Do not use white color for the line or bar.An data analyst of the company who want to know {question}')
132
+
133
+ # ValueError: Unsupported library. Choose from 'matplotlib', 'seaborn', 'plotly', 'bokeh', 'ggplot', 'altair'.
134
+
135
+ chart_result = []
136
+
137
+ final_explanation = []
138
+
139
+ for i in range(1):
140
+ try:
141
+ print(f"Goal{i}: {goals[i]}")
142
+ temp_chart = lida.visualize(summary=summary, goal=goals[i], textgen_config=text_gen_config,library='plotly')
143
+ temp_explanation = lida.explain(code=temp_chart[0].code)
144
+ final_explanation.append(temp_explanation)
145
+ chart_result.append(temp_chart)
146
+ except Exception as e:
147
+ print(f"Error while: {e}")
148
+ for i in range(len(chart_result)):
149
+ chart_result[i][0].savefig(f'chart_{i}.png')
150
+ print(f'*'*50)
151
+ print(f"Chart {i} saved")
152
+
153
+ # Path to your image
154
+ image_path = "chart_0.png"
155
+
156
+ # Open the image file
157
+ img = Image.open(image_path)
158
+ base64_image = encode_image(image_path)
159
+
160
+ llm = model
161
+
162
+ response = llm.invoke(
163
+ [
164
+ HumanMessage(
165
+ content=[
166
+ {"type": "text", "text": "Give me some business insights base on the graph, contain exact number conclusion."},
167
+ {
168
+ "type": "image_url",
169
+ "image_url": {
170
+ "url": f"data:image/jpeg;base64,{base64_image}"
171
+ },
172
+ },
173
+ ]
174
+ )
175
+ ]
176
+ )
177
+ final_result_str = response.content
178
+
179
+ return final_result_str,img
180
+
181
+ with gr.Blocks() as demo:
182
+ with gr.Row():
183
+ with gr.Column():
184
+ temp_img = gr.Image(
185
+ height=500
186
+ )
187
+ with gr.Column():
188
+ gr.ChatInterface(
189
+ random_response,
190
+ examples=['Top 10 prod_cate sales','Top product in category Seafood'],
191
+ type="messages",
192
+ autofocus=False,
193
+ additional_outputs=[temp_img]
194
+ )
195
+
196
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ huggingface_hub==0.25.2
2
+ sqlalchemy
3
+ pandas
4
+ lida
5
+ llmx
6
+ langchain_openai
7
+ langchain_core
8
+ gradio==5.11.0