Delete src.py
Browse filesVersions deprecated
src.py
DELETED
@@ -1,143 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import pandas as pd
|
3 |
-
from pandasai import Agent, SmartDataframe
|
4 |
-
from typing import Tuple
|
5 |
-
from PIL import Image
|
6 |
-
from pandasai.llm import HuggingFaceTextGen
|
7 |
-
from dotenv import load_dotenv
|
8 |
-
from langchain_groq.chat_models import ChatGroq
|
9 |
-
from langchain_google_genai import GoogleGenerativeAI
|
10 |
-
|
11 |
-
load_dotenv()
|
12 |
-
Groq_Token = os.environ["GROQ_API_KEY"]
|
13 |
-
models = {"mixtral": "mixtral-8x7b-32768", "llama": "llama2-70b-4096", "gemma": "gemma-7b-it", "gemini-pro": "gemini-pro"}
|
14 |
-
|
15 |
-
hf_token = os.getenv("HF_READ")
|
16 |
-
gemini_token = os.getenv("GEMINI_TOKEN")
|
17 |
-
|
18 |
-
def preprocess_and_load_df(path: str) -> pd.DataFrame:
|
19 |
-
df = pd.read_csv(path)
|
20 |
-
df["Timestamp"] = pd.to_datetime(df["Timestamp"])
|
21 |
-
return df
|
22 |
-
|
23 |
-
def load_agent(df: pd.DataFrame, context: str, inference_server: str, name="mixtral") -> Agent:
|
24 |
-
# llm = HuggingFaceTextGen(
|
25 |
-
# inference_server_url=inference_server,
|
26 |
-
# max_new_tokens=250,
|
27 |
-
# temperature=0.1,
|
28 |
-
# repetition_penalty=1.2,
|
29 |
-
# top_k=5,
|
30 |
-
# )
|
31 |
-
# llm.client.headers = {"Authorization": f"Bearer {hf_token}"}
|
32 |
-
if name == "gemini-pro":
|
33 |
-
llm = GoogleGenerativeAI(model=model, google_api_key=gemini_token, temperature=0.1)
|
34 |
-
else:
|
35 |
-
llm = ChatGroq(model=models[name], api_key=os.getenv("GROQ_API"), temperature=0.1)
|
36 |
-
|
37 |
-
agent = Agent(df, config={"llm": llm, "enable_cache": False, "options": {"wait_for_model": True}})
|
38 |
-
agent.add_message(context)
|
39 |
-
return agent
|
40 |
-
|
41 |
-
def load_smart_df(df: pd.DataFrame, inference_server: str, name="mixtral") -> SmartDataframe:
|
42 |
-
# llm = HuggingFaceTextGen(
|
43 |
-
# inference_server_url=inference_server,
|
44 |
-
# )
|
45 |
-
# llm.client.headers = {"Authorization": f"Bearer {hf_token}"}
|
46 |
-
llm = ChatGroq(model=models[name], api_key=os.getenv("GROQ_API"), temperature=0.1)
|
47 |
-
df = SmartDataframe(df, config={"llm": llm, "max_retries": 5, "enable_cache": False})
|
48 |
-
return df
|
49 |
-
|
50 |
-
def get_from_user(prompt):
|
51 |
-
return {"role": "user", "content": prompt}
|
52 |
-
|
53 |
-
def ask_agent(agent: Agent, prompt: str) -> Tuple[str, str, str]:
|
54 |
-
response = agent.chat(prompt)
|
55 |
-
gen_code = agent.last_code_generated
|
56 |
-
ex_code = agent.last_code_executed
|
57 |
-
last_prompt = agent.last_prompt
|
58 |
-
return {"role": "assistant", "content": response, "gen_code": gen_code, "ex_code": ex_code, "last_prompt": last_prompt}
|
59 |
-
|
60 |
-
def decorate_with_code(response: dict) -> str:
|
61 |
-
return f"""<details>
|
62 |
-
<summary>Generated Code</summary>
|
63 |
-
|
64 |
-
```python
|
65 |
-
{response["gen_code"]}
|
66 |
-
```
|
67 |
-
</details>
|
68 |
-
|
69 |
-
<details>
|
70 |
-
<summary>Prompt</summary>
|
71 |
-
|
72 |
-
{response["last_prompt"]}
|
73 |
-
"""
|
74 |
-
|
75 |
-
def show_response(st, response):
|
76 |
-
with st.chat_message(response["role"]):
|
77 |
-
try:
|
78 |
-
image = Image.open(response["content"])
|
79 |
-
if "gen_code" in response:
|
80 |
-
st.markdown(decorate_with_code(response), unsafe_allow_html=True)
|
81 |
-
st.image(image)
|
82 |
-
return {"is_image": True}
|
83 |
-
except Exception as e:
|
84 |
-
if "gen_code" in response:
|
85 |
-
display_content = decorate_with_code(response) + f"""</details>
|
86 |
-
|
87 |
-
{response["content"]}"""
|
88 |
-
else:
|
89 |
-
display_content = response["content"]
|
90 |
-
st.markdown(display_content, unsafe_allow_html=True)
|
91 |
-
return {"is_image": False}
|
92 |
-
|
93 |
-
def ask_question(model_name, question):
|
94 |
-
if model_name == "gemini-pro":
|
95 |
-
llm = GoogleGenerativeAI(model=model, google_api_key=os.environ.get("GOOGLE_API_KEY"), temperature=0)
|
96 |
-
else:
|
97 |
-
llm = ChatGroq(model=models[model_name], api_key=os.getenv("GROQ_API"), temperature=0.1)
|
98 |
-
|
99 |
-
df_check = pd.read_csv("Data.csv")
|
100 |
-
df_check["Timestamp"] = pd.to_datetime(df_check["Timestamp"])
|
101 |
-
df_check = df_check.head(5)
|
102 |
-
|
103 |
-
new_line = "\n"
|
104 |
-
|
105 |
-
template = f"""```python
|
106 |
-
import pandas as pd
|
107 |
-
import matplotlib.pyplot as plt
|
108 |
-
|
109 |
-
df = pd.read_csv("Data.csv")
|
110 |
-
df["Timestamp"] = pd.to_datetime(df["Timestamp"])
|
111 |
-
|
112 |
-
# df.dtypes
|
113 |
-
{new_line.join(map(lambda x: '# '+x, str(df_check.dtypes).split(new_line)))}
|
114 |
-
|
115 |
-
# {question.strip()}
|
116 |
-
# <your code here>
|
117 |
-
```
|
118 |
-
"""
|
119 |
-
|
120 |
-
query = f"""I have a pandas dataframe data of PM2.5 and PM10.
|
121 |
-
* Frequency of data is daily.
|
122 |
-
* `pollution` generally means `PM2.5`.
|
123 |
-
* Save result in a variable `answer` and make it global.
|
124 |
-
* If result is a plot, save it and save path in `answer`. Example: `answer='plot.png'`
|
125 |
-
* If result is not a plot, save it as a string in `answer`. Example: `answer='The city is Mumbai'`
|
126 |
-
|
127 |
-
Complete the following code.
|
128 |
-
|
129 |
-
{template}
|
130 |
-
|
131 |
-
"""
|
132 |
-
if model_name == "gemini-pro":
|
133 |
-
answer = llm.invoke(query)
|
134 |
-
else:
|
135 |
-
answer = llm.invoke(query).content
|
136 |
-
code = f"""
|
137 |
-
{template.split("```python")[1].split("```")[0]}
|
138 |
-
{answer.split("```python")[1].split("```")[0]}
|
139 |
-
"""
|
140 |
-
# update variable `answer` when code is executed
|
141 |
-
exec(code)
|
142 |
-
|
143 |
-
return {"role": "assistant", "content": answer.content, "gen_code": code, "ex_code": code, "last_prompt": question}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|