Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -104,93 +104,146 @@ def encode_image(image_path):
|
|
| 104 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 105 |
|
| 106 |
|
| 107 |
-
def random_response(message
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
df_data
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
time_now = pd.Timestamp.now()
|
| 138 |
-
print(f'Datetime now:{time_now}')
|
| 139 |
-
goals = lida.goals(summary, n=1, textgen_config=text_gen_config,persona=f'An data analyst of the company who want to know {question}')
|
| 140 |
-
|
| 141 |
-
# ValueError: Unsupported library. Choose from 'matplotlib', 'seaborn', 'plotly', 'bokeh', 'ggplot', 'altair'.
|
| 142 |
-
print(f'goals: {goals[0]}')
|
| 143 |
-
|
| 144 |
-
try:
|
| 145 |
-
temp_chart = lida.visualize(summary=summary, goal=goals[0].question+'set different color to the graph', textgen_config=text_gen_config,library='plotly')
|
| 146 |
-
# code = temp_chart[0].code
|
| 147 |
-
# instructions = ["change the color of the graph to #4169E1 if there is only one variable","change the background color to white but keep the grid lines grey","set the average line for the graph to be red"]
|
| 148 |
-
# edited_chart = lida.edit(code=code,summary=summary,instructions=instructions,library='plotly',textgen_config = text_gen_config)
|
| 149 |
-
except Exception as e:
|
| 150 |
-
print(f"Error while: {e}")
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
]
|
| 177 |
)
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
return final_result_str,img
|
| 183 |
|
| 184 |
with gr.Blocks() as demo:
|
| 185 |
with gr.Row():
|
| 186 |
with gr.Column():
|
| 187 |
temp_img = gr.Image(
|
| 188 |
-
height=
|
| 189 |
)
|
| 190 |
with gr.Column():
|
| 191 |
chat_input = gr.Textbox(placeholder="Type your message here...", label="Chat")
|
| 192 |
examples = gr.Examples(
|
| 193 |
-
examples=['Top 10 prod_cate sales', 'Top product in category Seafood','
|
|
|
|
|
|
|
| 194 |
inputs=chat_input
|
| 195 |
)
|
| 196 |
chat_output = gr.Textbox(label="Response", interactive=False)
|
|
|
|
| 104 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 105 |
|
| 106 |
|
| 107 |
+
def random_response(message):
|
| 108 |
+
max_attempts = 3 # Set the maximum number of attempts
|
| 109 |
+
attempts = 0
|
| 110 |
+
while attempts < max_attempts:
|
| 111 |
+
try:
|
| 112 |
+
df_data = choose_table(message)
|
| 113 |
+
|
| 114 |
+
question = message
|
| 115 |
+
# fill na with empty string
|
| 116 |
+
df_data.fillna('', inplace=True)
|
| 117 |
+
# loop columns, if column is object type, convert to string
|
| 118 |
+
for col in df_data.columns:
|
| 119 |
+
if df_data[col].dtype == 'object':
|
| 120 |
+
df_data[col] = df_data[col].astype(str)
|
| 121 |
+
|
| 122 |
+
text_gen = OpenAITextGenerator(
|
| 123 |
+
provider='openai',
|
| 124 |
+
api_type='azure',
|
| 125 |
+
azure_endpoint= os.getenv('AZURE_OPENAI_ENDPOINT'),
|
| 126 |
+
api_key= os.getenv('OPENAI_API_KEY'),
|
| 127 |
+
api_version = '2023-05-15',
|
| 128 |
+
|
| 129 |
+
)
|
| 130 |
+
lida = Manager(text_gen=text_gen)
|
| 131 |
+
|
| 132 |
+
text_gen_config = TextGenerationConfig(
|
| 133 |
+
n = 1,
|
| 134 |
+
model = 'CapSuiteGPT35T16K',
|
| 135 |
+
temperature=0
|
| 136 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
summary = lida.summarize(df_data)
|
| 139 |
+
print(f'*'*50)
|
| 140 |
+
pprint(f"{summary}")
|
| 141 |
+
|
| 142 |
+
str_summary = str(summary)
|
| 143 |
+
|
| 144 |
+
print(f'*'*50)
|
| 145 |
+
time_now = pd.Timestamp.now()
|
| 146 |
+
print(f'Datetime now:{time_now}')
|
| 147 |
+
goals = lida.goals(summary, n=1, textgen_config=text_gen_config,persona=f'An data analyst of the company who want to know {question}')
|
| 148 |
+
|
| 149 |
+
print(f'goals: {goals[0]}')
|
| 150 |
+
|
| 151 |
+
output_parser = CommaSeparatedListOutputParser()
|
| 152 |
+
# "Bussiness insights focus on different aspects of the data, such as sales amount,sales qty, product category, time, etc."
|
| 153 |
+
model = AzureChatOpenAI(
|
| 154 |
+
deployment_name="CapSuiteGPT4omini",
|
| 155 |
+
openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
|
| 156 |
+
temperature=0
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
str_summary = str(summary)
|
| 160 |
+
prompt = ChatPromptTemplate.from_template("Based on the data below:{str_summary},"
|
| 161 |
+
"please give me the most related and useful possible question to get simple but useful insights for {question}."
|
| 162 |
+
"Your insight will be used to guide the graph generation by python using ploty, so make it simple and easier to process data."
|
| 163 |
+
"For example: 'Goal(question='What are the sales trends by product category?visualization='bar chart of prod_category against sum(trxn_item_qty) grouped by trxn_date'."
|
| 164 |
+
"If the data columns is empty, please ignore the column."
|
| 165 |
+
"Only output 1 question."
|
| 166 |
+
"")
|
| 167 |
+
|
| 168 |
+
chain = (
|
| 169 |
+
{"str_summary": RunnablePassthrough(),"question": RunnablePassthrough()}
|
| 170 |
+
| prompt
|
| 171 |
+
| model
|
| 172 |
+
| output_parser
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
insights = chain.invoke({"str_summary": str_summary, "question": question})
|
| 176 |
+
print(f'*'*50)
|
| 177 |
+
print(f'insights: {insights[0]}')
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
# ValueError: Unsupported library. Choose from 'matplotlib', 'seaborn', 'plotly', 'bokeh', 'ggplot', 'altair'.
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
try:
|
| 184 |
+
temp_chart = lida.visualize(summary=summary, goal=insights[0]+'Graph heigh 800,width 1000.If there is statement in previous question using monthly data,other time related using daily.set different color to the graph,x label rotate 45 degree,do not use the guide line', textgen_config=text_gen_config,library='matplotlib')
|
| 185 |
+
print(f'*'*50)
|
| 186 |
+
code = temp_chart[0].code
|
| 187 |
+
print(f"{code}")
|
| 188 |
+
# instructions = ["change the color of the graph to #4169E1 if there is only one variable","change the background color to white but keep the grid lines grey","set the average line for the graph to be red"]
|
| 189 |
+
# edited_chart = lida.edit(code=code,summary=summary,instructions=instructions,library='plotly',textgen_config = text_gen_config)
|
| 190 |
+
except Exception as e:
|
| 191 |
+
print(f"Error while: {e}")
|
| 192 |
+
|
| 193 |
+
temp_chart[0].savefig(f'chart_1.png')
|
| 194 |
+
print(f'*'*50)
|
| 195 |
+
print(f"Chart saved")
|
| 196 |
+
|
| 197 |
+
# Path to your image
|
| 198 |
+
image_path = "chart_1.png"
|
| 199 |
+
|
| 200 |
+
# Open the image file
|
| 201 |
+
# img = Image.open(image_path)
|
| 202 |
+
img = mpimg.imread('chart_1.png')
|
| 203 |
+
|
| 204 |
+
base64_image = encode_image(image_path)
|
| 205 |
+
|
| 206 |
+
llm = model
|
| 207 |
+
|
| 208 |
+
response = llm.invoke(
|
| 209 |
+
[
|
| 210 |
+
HumanMessage(
|
| 211 |
+
content=[
|
| 212 |
+
{"type": "text", "text": "Give me some business insights base on the graph, contain exact number conclusion."},
|
| 213 |
+
{
|
| 214 |
+
"type": "image_url",
|
| 215 |
+
"image_url": {
|
| 216 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 217 |
+
},
|
| 218 |
+
},
|
| 219 |
+
]
|
| 220 |
+
)
|
| 221 |
]
|
| 222 |
)
|
| 223 |
+
final_result_str = response.content
|
| 224 |
+
|
| 225 |
+
return final_result_str,img
|
| 226 |
+
except Exception as e:
|
| 227 |
+
attempts += 1
|
| 228 |
+
print(f"Attempt {attempts} failed with error: {e}")
|
| 229 |
+
if attempts >= max_attempts:
|
| 230 |
+
return "An error occurred after multiple attempts.", None # Return an error message
|
| 231 |
+
|
| 232 |
+
|
| 233 |
|
|
|
|
| 234 |
|
| 235 |
with gr.Blocks() as demo:
|
| 236 |
with gr.Row():
|
| 237 |
with gr.Column():
|
| 238 |
temp_img = gr.Image(
|
| 239 |
+
height=800
|
| 240 |
)
|
| 241 |
with gr.Column():
|
| 242 |
chat_input = gr.Textbox(placeholder="Type your message here...", label="Chat")
|
| 243 |
examples = gr.Examples(
|
| 244 |
+
examples=['Top 10 prod_cate sales', 'Top product in category Seafood','Total sales amount by product category each day','What are the hot selling at product level??',
|
| 245 |
+
'Sales amount distribution by age','Sales amount distribution by gender','Sales qty trend by time using line chart'
|
| 246 |
+
],
|
| 247 |
inputs=chat_input
|
| 248 |
)
|
| 249 |
chat_output = gr.Textbox(label="Response", interactive=False)
|