File size: 3,041 Bytes
60f68c4 3d660e2 dd82d0a 19b2962 3d660e2 19b2962 60f68c4 3d660e2 60f68c4 3d660e2 60f68c4 3d660e2 60f68c4 3d660e2 60f68c4 3d660e2 60f68c4 3d660e2 60f68c4 3d660e2 dd82d0a 3d660e2 dd82d0a 3d660e2 |
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 |
from typing import List
from typing import Dict
import plotly.io as pio
import pandas as pd
from utils import TEMP_DIR
import os
import ast
from dotenv import load_dotenv
load_dotenv()
root_url = os.getenv("ROOT_URL")
def chart_generation_func(data: List[dict], session_hash: str, layout: Dict[str,str]={}):
print("CHART GENERATION")
print(data)
print(layout)
try:
dir_path = TEMP_DIR / str(session_hash)
chart_path = f'{dir_path}/chart.html'
#Processing data to account for variation from LLM
data_list = []
layout_dict = {}
if isinstance(data, list):
data_list = data
else:
data_list.append(data)
if isinstance(data[0], str):
data_list[0] = ast.literal_eval(data_list[0])
if isinstance(layout, list):
layout_obj = layout[0]
else:
layout_obj = layout
if isinstance(layout_obj, str):
layout_dict = ast.literal_eval(layout_obj)
else:
layout_dict = layout_obj
fig = dict({"data": data_list,
"layout": layout_dict})
pio.write_html(fig, chart_path, full_html=False)
chart_url = f'{root_url}/gradio_api/file/temp/{session_hash}/chart.html'
iframe = '<div style=overflow:auto;><iframe\n scrolling="yes"\n width="1000px"\n height="500px"\n src="' + chart_url + '"\n frameborder="0"\n allowfullscreen\n></iframe>\n</div>'
return {"reply": iframe}
except Exception as e:
print("CHART ERROR")
reply = f"""There was an error generating the Plotly Chart from {data} and {layout}
The error is {e},
You should probably try again.
"""
return {"reply": reply}
def table_generation_func(data: List[dict], session_hash):
print("TABLE GENERATION")
print(data)
try:
dir_path = TEMP_DIR / str(session_hash)
csv_path = f'{dir_path}/data.csv'
#Processing data to account for variation from LLM
if isinstance(data, list):
data_obj = data[0]
else:
data_obj = data
if isinstance(data_obj, str):
data_dict = ast.literal_eval(data_obj)
else:
data_dict = data_obj
df = pd.DataFrame.from_dict(data_dict)
print(df)
df.to_csv(csv_path)
download_path = f'{root_url}/gradio_api/file/temp/{session_hash}/data.csv'
html_table = df.to_html() + f'<p>Download as a <a href="{download_path}">CSV file</a></p>'
print(html_table)
return {"reply": html_table}
except Exception as e:
print("TABLE ERROR")
reply = f"""There was an error generating the Pandas DataFrame table from {data}
The error is {e},
You should probably try again.
"""
return {"reply": reply}
|