Spaces:
Sleeping
Sleeping
Upload query.py
Browse files
query.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from lida import Manager, TextGenerationConfig , llm
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import os
|
4 |
+
import openai
|
5 |
+
import base64
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
12 |
+
|
13 |
+
def base64_to_image(base64_string):
|
14 |
+
# Decode the base64 string
|
15 |
+
byte_data = base64.b64decode(base64_string)
|
16 |
+
|
17 |
+
# Use BytesIO to convert the byte data to image
|
18 |
+
return Image.open(BytesIO(byte_data))
|
19 |
+
|
20 |
+
def save_image(base64_str, save_path):
|
21 |
+
img = base64_to_image(base64_str)
|
22 |
+
img.save(save_path)
|
23 |
+
print(f"Image saved at {save_path}")
|
24 |
+
|
25 |
+
lida = Manager(text_gen = llm("openai"))
|
26 |
+
textgen_config = TextGenerationConfig(n=1, temperature=0.2, use_cache=True)
|
27 |
+
summary = lida.summarize("2019.csv", summary_method="default", textgen_config=textgen_config)
|
28 |
+
|
29 |
+
user_query = "Which country has the most GDP per capita?"
|
30 |
+
charts = lida.visualize(summary=summary, goal=user_query, textgen_config=textgen_config)
|
31 |
+
charts[0]
|
32 |
+
|
33 |
+
image_base64 = charts[0].raster
|
34 |
+
|
35 |
+
save_image(image_base64, "filename1.png")
|
36 |
+
|