-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph_description.py
305 lines (203 loc) · 16.2 KB
/
Graph_description.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import requests
from PIL import Image
import torch
from transformers import BitsAndBytesConfig, pipeline
import cv2
import os
# if torch.cuda.is_available():
# torch.cuda.init()
# os.environ["CUDA_VISIBLE_DEVICES"] = "2"
class GraphDescriptionPipeline:
def __init__(self,model_id="llava-hf/llava-1.5-7b-hf", bnb_4bit_compute_dtype="float16", use_4bit=True):
# os.environ["CUDA_VISIBLE_DEVICES"] = "2"
# os.environ["CUDA_VISIBLE_DEVICES"] = ""
# Quantization configuration
#self.Query=Query
self.bnb_4bit_compute_dtype = bnb_4bit_compute_dtype
self.use_4bit = use_4bit
self.quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
use_nested_quant=False
)
self.compute_dtype = getattr(torch, self.bnb_4bit_compute_dtype)
# Initialize CUDA if available
if torch.cuda.is_available():
torch.cuda.init()
if self.compute_dtype == torch.float16 and self.use_4bit:
major, _ = torch.cuda.get_device_capability()
if major >= 8:
print("=" * 80)
print("Your GPU supports bfloat16: accelerate training with bf16=True")
print("=" * 80)
self.device = "cuda" if torch.cuda.is_available() else "cpu"
# Image-to-text pipeline
self.model_id = model_id
# Check if GPU is available and set the device accordingly
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.img_to_text_pipe = pipeline("image-to-text", model=self.model_id, model_kwargs={"quantization_config": self.quantization_config})
def process(self, filename, prompt):
max_new_tokens=2000
# Add your image processing logic here
image = Image.open(filename)
# Additional image processing steps if needed
# ...
# Run the image-to-text pipeline
result = self.img_to_text_pipe(image,prompt=prompt, generate_kwargs={"max_new_tokens": 2000})
return result[0]["generated_text"]
# def get_result(self, image_path,prompt):
# return self.process(image_path,prompt)
# def process_image(self, filename,prompt):
# return self.process(filename,prompt)
# print(result)
# Display the result
#return result_image
def display_image(self,image_path):
# Open an image file
img = Image.open(image_path)
# Display the image
img.show()
###### time Series Graphs Function #######
def Distributions(self,image_path,Query):
self.image_path=image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt = f"USER: <image>\n The image displays the distribution of columns based on. Consider this query: {Query} and answer it. Please analyze the ` to identify any discernible patterns, cycles, or trends in the distribution of data over time. Explain, analyze and gain insights from each graph within the image. Provide a detailed description of the shape supported by visual and statistical analysis. Additionally, characterize the shape of the distribution (symmetric, skewed, uniform) fot each Column.\nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def LSTM_inference_plot(self,image_path,Query):
self.image_path=image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_LSTM_inference_plot_['Mold'].png"
self.prompt = f"USER: <image>\n Review the LSTM inference and evaluate the {Query}. Analyze the predictions generated by the model in comparison to the actual values within the image. Explain the variables depicted in the model visualization, such as time, actual values, and predicted values, and provide insights for accurately and preformance and how to improve the image.\nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def Training_loss_graph(self,image_path,Query):
self.image_path=image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt = f"USER: <image>\n Analyze the image and address the {Query}. Compare the training loss and validation loss across epochs in the training loss graph. Explore whether there are signs of overfitting or underfitting by examining the convergence or divergence of the two loss curves.\nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def Heatmap_explainer(self,image_path,Question):
self.image_path=image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt = f"USER: <image>\n Explore the {Question}. Examine and deep Analysis of the visual representation, a correlation heatmap, explain the understand the relationships between variables.these varaibles are likely related which data in image and Analyse colors and patterns in the heatmap to gain insights into the strength and direction of correlations of varaibles. \nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def Confusion_matrix(self,image_path,Question):
self.image_path=image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt = f"USER: <image>\n Explore the {Question}. Examine and deep Analysis of the visual representation, a Confusion_matrix, explain the understand the relationships between variables.these varaibles are likely related which data in image and Analyse colors and patterns in the Confusion_matrix to gain insights into the strength and direction of correlations of varaibles. \nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def Neural_Regressor(self,image_path,Question):
self.image_path=image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt = f"USER: <image>\n Explore the {Question}. Examine and deep Analysis of the visual representation, a Neural_Regressor, explain the understand the relationships between variables.these varaibles are likely related which data in image and Analyse colors and patterns in the Neural_Regressor to gain insights into the strength and direction of correlations of varaibles. \nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def Missing_Number(self,image_path,Question):
self.image_path=image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt = f"USER: <image>\n Elaborate on the insights conveyed by the image. Evaluate the {Question}. What causes numbers to be missing, and how does it impact data analysis or modeling? Explore how exploratory data analysis (EDA) techniques can aid in identifying missing values.\nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def Most_Correlated_Features(self, image_path, Question):
self.image_path = image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt = f"USER: <image>\n Interpret the {Question}. Analyze the {Question}, with a particular emphasis on the image's representation of the most correlated features. Explore the correlation, highlighting significant relationships between variables. Discuss the implications of these strong correlations, shedding light on the key features that exhibit the most pronounced interdependence in the dataset \nASSISTANT:"
graph_result=self.process(image_path, self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def Trend_Graph(self,image_path,Question):
self.image_path=image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt = f"USER: <image>\n Investigate the {Question}. Clarify the concept of a trend graph and its function in visualizing data trends over time. Identify and discuss the trends or insights conveyed in the image, emphasizing the role of the trend graph in capturing temporal patterns and providing valuable information about the dataset's dynamics.\nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def PairWise_Graph(self,image_path,Question):
self.image_path=image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt = f"USER: <image>\n Evaluate the {Question}. Define and explain each pairwise graph utilized for visualizing the data in the image. Uncover insights presented in the image, emphasizing the significance of pairwise graphs in revealing patterns and changes. Discuss how these visualizations enhance our understanding of relationships between variables, providing a comprehensive view of the data's intricate interconnections.\nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
###### Categoricals Graphs Function #######
def BarCharts(self, image_path, Question, var1):
self.image_path=image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt= f"USER: <image>\n Investigate the {Question} by employing a bar chart and evaluate the importance of {var1} within its respective category. Scrutinize the differences between {var1} and other components depicted on the bar chart. Present a thorough visualization and analysis, with a specific emphasis on the bars that represent {var1} \nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def Histogram(self, image_path, Question, var1, var2):
self.image_path = image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt = f"USER: <image>\n What insights can be drawn from the histogram comparing {var1} and {var2}.how is differ from {var1} and {var2} from other categories.describe visualizing and Elaborate the {Question}.What does the shape of the histogram reveal about the data in image.\nASSISTANT:"
graph_result=self.process(image_path, self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def StackedBarChart(self,image_path,Question,var1,var2):
self.image_path = image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt= f"""USER: <image> Analyze the {Question} through a bar chart and expound on the significance of {var1} within the {var2} category. Investigate the differences between {var1} and {var2} in contrast to other categories on the bar chart. Present a comprehensive visualization and analysis, with a specific focus on the bars representing {var1} and {var2}.Additionally, discuss the insights that the shape of bar chart reveals about the data in the image.
ASSISTANT:
"""
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
def CrossTabulation(self, image_path, Query, var1, var2):
self.image_path = image_path
#image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
self.prompt= f"USER: <image>\n The given image is the Cross tabulation between {var1} and {var2} to indicate relationship among them. Consider this user query: {Question} and analyze the given image to get insigths about relationship between {var1} and {var2}. Explain how {var1} is differ from {var2}. Explain what these values and numbers represents about the relationship between these two variables. \nASSISTANT:"
graph_result=self.process(image_path,self.prompt)
start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
graph_result = graph_result[start_index:].strip()
return graph_result
# def denormalized_income_cat_income_num_stacked_bar_chart(self,image_path,Query,var1,var2):
# self.image_path = image_path
# #image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
# self.prompt= f"USER: <image>\n consider the {Query} explain the graph {var1} with each categories of {var2} and indicate the {var1} effected with each categories \nASSISTANT:"
# graph_result=self.process(image_path,self.prompt)
# start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
# graph_result = graph_result[start_index:].strip()
# return graph_result
###### Numerical Graph function #######
# def denormalized_sales_data_sample_bar_chart(self,image_path,Query,var1):
# self.image_path=image_path
# #image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
# self.prompt= f"USER: <image>\n consider the {Query} which the highest bar line in {var1} and visualize and anlysis of varaibles in {var1} \nASSISTANT:"
# graph_result=self.process(image_path,self.prompt)
# start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
# graph_result = graph_result[start_index:].strip()
# return graph_result
# def denormalized_sales_data_sample_histogram(self,image_path,Query,var1,var2):
# self.image_path = image_path
# #image_path ="Time_Series_graphs/denormalized_denormalized_dataset_new_distributions.png"
# self.prompt= f"USER: <image>\n consider the {Query}.explain the graph {var1} with each categories of {var2},How strong is the relationship between the {var1} and {var2} \nASSISTANT:"
# graph_result=self.process(image_path,self.prompt)
# start_index = graph_result.find("ASSISTANT:") + len("ASSISTANT:")
# graph_result = graph_result[start_index:].strip()
# return graph_result
# print(result)
# Display the result
#return result_image