Delete app.py
Browse filesversion deprecated
app.py
DELETED
@@ -1,409 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
import json
|
4 |
-
import pandas as pd
|
5 |
-
import random
|
6 |
-
from os.path import join
|
7 |
-
from datetime import datetime
|
8 |
-
from src import (
|
9 |
-
preprocess_and_load_df,
|
10 |
-
load_agent,
|
11 |
-
ask_agent,
|
12 |
-
decorate_with_code,
|
13 |
-
show_response,
|
14 |
-
get_from_user,
|
15 |
-
load_smart_df,
|
16 |
-
ask_question,
|
17 |
-
)
|
18 |
-
from dotenv import load_dotenv
|
19 |
-
from langchain_groq.chat_models import ChatGroq
|
20 |
-
from langchain_google_genai import GoogleGenerativeAI
|
21 |
-
from streamlit_feedback import streamlit_feedback
|
22 |
-
from huggingface_hub import HfApi
|
23 |
-
from datasets import load_dataset, get_dataset_config_info, Dataset
|
24 |
-
from PIL import Image
|
25 |
-
|
26 |
-
st.set_page_config(layout="wide")
|
27 |
-
|
28 |
-
# Load environment variables : Groq and Hugging Face API keys
|
29 |
-
load_dotenv()
|
30 |
-
Groq_Token = os.environ["GROQ_API_KEY"]
|
31 |
-
hf_token = os.environ["HF_TOKEN"]
|
32 |
-
gemini_token = os.environ["GEMINI_TOKEN"]
|
33 |
-
models = {
|
34 |
-
"llama3": "llama3-70b-8192",
|
35 |
-
"mixtral": "mixtral-8x7b-32768",
|
36 |
-
"llama2": "llama2-70b-4096",
|
37 |
-
"gemma": "gemma-7b-it",
|
38 |
-
"gemini-pro": "gemini-pro",
|
39 |
-
}
|
40 |
-
|
41 |
-
self_path = os.path.dirname(os.path.abspath(__file__))
|
42 |
-
|
43 |
-
|
44 |
-
# Using HTML and CSS to center the title
|
45 |
-
st.write(
|
46 |
-
"""
|
47 |
-
<style>
|
48 |
-
.title {
|
49 |
-
text-align: center;
|
50 |
-
color: #17becf;
|
51 |
-
}
|
52 |
-
</style>
|
53 |
-
""",
|
54 |
-
unsafe_allow_html=True,
|
55 |
-
)
|
56 |
-
|
57 |
-
# Displaying the centered title
|
58 |
-
st.markdown(
|
59 |
-
"<div style='text-align:center; padding: 20px;'>VayuBuddy makes pollution monitoring easier by bridging the gap between users and datasets.<br>No coding required—just meaningful insights at your fingertips!</div>",
|
60 |
-
unsafe_allow_html=True,
|
61 |
-
)
|
62 |
-
|
63 |
-
# Center-aligned instruction text with bold formatting
|
64 |
-
st.markdown(
|
65 |
-
"<div style='text-align:center;'>Choose a query from <b>Select a prompt</b> or type a query in the <b>chat box</b>, select a <b>LLM</b> (Large Language Model), and press enter to generate a response.</div>",
|
66 |
-
unsafe_allow_html=True,
|
67 |
-
)
|
68 |
-
# os.environ["PANDASAI_API_KEY"] = "$2a$10$gbmqKotzJOnqa7iYOun8eO50TxMD/6Zw1pLI2JEoqncwsNx4XeBS2"
|
69 |
-
|
70 |
-
# with open(join(self_path, "context1.txt")) as f:
|
71 |
-
# context = f.read().strip()
|
72 |
-
|
73 |
-
# agent = load_agent(join(self_path, "app_trial_1.csv"), context)
|
74 |
-
# df = preprocess_and_load_df(join(self_path, "Data.csv"))
|
75 |
-
# inference_server = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
76 |
-
# inference_server = "https://api-inference.huggingface.co/models/codellama/CodeLlama-13b-hf"
|
77 |
-
# inference_server = "https://api-inference.huggingface.co/models/pandasai/bamboo-llm"
|
78 |
-
|
79 |
-
image_path = "IITGN_Logo.png"
|
80 |
-
|
81 |
-
# Display images and text in three columns with specified ratios
|
82 |
-
col1, col2, col3 = st.sidebar.columns((1.0, 2, 1.0))
|
83 |
-
with col2:
|
84 |
-
st.image(image_path, use_column_width=True)
|
85 |
-
st.markdown("<h1 class='title'>VayuBuddy</h1>", unsafe_allow_html=True)
|
86 |
-
|
87 |
-
|
88 |
-
model_name = st.sidebar.selectbox("Select LLM:", ["llama3", "mixtral", "gemma", "gemini-pro"])
|
89 |
-
|
90 |
-
questions = ["Custom Prompt"]
|
91 |
-
with open(join(self_path, "questions.txt")) as f:
|
92 |
-
questions += f.read().split("\n")
|
93 |
-
|
94 |
-
waiting_lines = (
|
95 |
-
"Thinking...",
|
96 |
-
"Just a moment...",
|
97 |
-
"Let me think...",
|
98 |
-
"Working on it...",
|
99 |
-
"Processing...",
|
100 |
-
"Hold on...",
|
101 |
-
"One moment...",
|
102 |
-
"On it...",
|
103 |
-
)
|
104 |
-
|
105 |
-
# agent = load_agent(df, context="", inference_server=inference_server, name=model_name)
|
106 |
-
|
107 |
-
# Initialize chat history
|
108 |
-
if "responses" not in st.session_state:
|
109 |
-
st.session_state.responses = []
|
110 |
-
|
111 |
-
### Old code for feedback
|
112 |
-
# def push_to_dataset(feedback, comments,output,code,error):
|
113 |
-
# # Load existing dataset or create a new one if it doesn't exist
|
114 |
-
# try:
|
115 |
-
# ds = load_dataset("YashB1/Feedbacks_eoc", split="evaluation")
|
116 |
-
# except FileNotFoundError:
|
117 |
-
# # If dataset doesn't exist, create a new one
|
118 |
-
# ds = Dataset.from_dict({"feedback": [], "comments": [], "error": [], "output": [], "code": []})
|
119 |
-
|
120 |
-
# # Add new feedback to the dataset
|
121 |
-
# new_data = {"feedback": [feedback], "comments": [comments], "error": [error], "output": [output], "code": [code]} # Convert feedback and comments to lists
|
122 |
-
# new_data = Dataset.from_dict(new_data)
|
123 |
-
|
124 |
-
# ds = concatenate_datasets([ds, new_data])
|
125 |
-
|
126 |
-
# # Push the updated dataset to Hugging Face Hub
|
127 |
-
# ds.push_to_hub("YashB1/Feedbacks_eoc", split="evaluation")
|
128 |
-
|
129 |
-
|
130 |
-
def upload_feedback():
|
131 |
-
print("Uploading feedback")
|
132 |
-
data = {
|
133 |
-
"feedback": feedback["score"],
|
134 |
-
"comment": feedback["text"],
|
135 |
-
"error": error,
|
136 |
-
"output": output,
|
137 |
-
"prompt": last_prompt,
|
138 |
-
"code": code,
|
139 |
-
}
|
140 |
-
|
141 |
-
# generate a random file name based on current time-stamp: YYYY-MM-DD_HH-MM-SS
|
142 |
-
random_folder_name = str(datetime.now()).replace(" ", "_").replace(":", "-").replace(".", "-")
|
143 |
-
print("Random folder:", random_folder_name)
|
144 |
-
save_path = f"/tmp/vayubuddy_feedback.md"
|
145 |
-
path_in_repo = f"data/{random_folder_name}/feedback.md"
|
146 |
-
with open(save_path, "w") as f:
|
147 |
-
template = f"""Prompt: {last_prompt}
|
148 |
-
|
149 |
-
Output: {output}
|
150 |
-
|
151 |
-
Code:
|
152 |
-
|
153 |
-
```py
|
154 |
-
{code}
|
155 |
-
```
|
156 |
-
|
157 |
-
Error: {error}
|
158 |
-
|
159 |
-
Feedback: {feedback['score']}
|
160 |
-
|
161 |
-
Comments: {feedback['text']}
|
162 |
-
"""
|
163 |
-
|
164 |
-
print(template, file=f)
|
165 |
-
|
166 |
-
api = HfApi(token=hf_token)
|
167 |
-
api.upload_file(
|
168 |
-
path_or_fileobj=save_path,
|
169 |
-
path_in_repo=path_in_repo,
|
170 |
-
repo_id="SustainabilityLabIITGN/VayuBuddy_Feedback",
|
171 |
-
repo_type="dataset",
|
172 |
-
)
|
173 |
-
if status["is_image"]:
|
174 |
-
api.upload_file(
|
175 |
-
path_or_fileobj=output,
|
176 |
-
path_in_repo=f"data/{random_folder_name}/plot.png",
|
177 |
-
repo_id="SustainabilityLabIITGN/VayuBuddy_Feedback",
|
178 |
-
repo_type="dataset",
|
179 |
-
)
|
180 |
-
|
181 |
-
print("Feedback uploaded successfully!")
|
182 |
-
|
183 |
-
|
184 |
-
# Display chat responses from history on app rerun
|
185 |
-
print("#" * 10)
|
186 |
-
for response_id, response in enumerate(st.session_state.responses):
|
187 |
-
status = show_response(st, response)
|
188 |
-
if response["role"] == "assistant":
|
189 |
-
feedback_key = f"feedback_{int(response_id/2)}"
|
190 |
-
print("response_id", response_id, "feedback_key", feedback_key)
|
191 |
-
|
192 |
-
error = response["error"]
|
193 |
-
output = response["content"]
|
194 |
-
last_prompt = response["last_prompt"]
|
195 |
-
code = response["gen_code"]
|
196 |
-
|
197 |
-
if "feedback" in st.session_state.responses[response_id]:
|
198 |
-
st.write("Feedback:", st.session_state.responses[response_id]["feedback"])
|
199 |
-
else:
|
200 |
-
## !!! This does on work on Safari !!!
|
201 |
-
# feedback = streamlit_feedback(feedback_type="thumbs",
|
202 |
-
# optional_text_label="[Optional] Please provide extra information", on_submit=upload_feedback, key=feedback_key)
|
203 |
-
|
204 |
-
# Display thumbs up/down buttons for feedback
|
205 |
-
thumbs = st.radio("We would appreciate your feedback!", ("👍", "👎"), index=None, key=feedback_key)
|
206 |
-
|
207 |
-
if thumbs:
|
208 |
-
# Text input for comments
|
209 |
-
comments = st.text_area("[Optional] Please provide extra information", key=feedback_key + "_comments")
|
210 |
-
feedback = {"score": thumbs, "text": comments}
|
211 |
-
if st.button("Submit", on_click=upload_feedback, key=feedback_key + "_submit"):
|
212 |
-
st.session_state.responses[response_id]["feedback"] = feedback
|
213 |
-
st.success("Feedback uploaded successfully!")
|
214 |
-
|
215 |
-
|
216 |
-
print("#" * 10)
|
217 |
-
|
218 |
-
show = True
|
219 |
-
prompt = st.sidebar.selectbox("Select a Prompt:", questions, key="prompt_key")
|
220 |
-
if prompt == "Custom Prompt":
|
221 |
-
show = False
|
222 |
-
# React to user input
|
223 |
-
prompt = st.chat_input("Ask me anything about air quality!", key=1000)
|
224 |
-
if prompt:
|
225 |
-
show = True
|
226 |
-
else:
|
227 |
-
# placeholder for chat input
|
228 |
-
st.chat_input(
|
229 |
-
"Select 'Select a Prompt' -> 'Custom Prompt' in the sidebar to ask your own questions.", key=1000, disabled=True
|
230 |
-
)
|
231 |
-
|
232 |
-
if "last_prompt" in st.session_state:
|
233 |
-
last_prompt = st.session_state["last_prompt"]
|
234 |
-
last_model_name = st.session_state["last_model_name"]
|
235 |
-
if (prompt == last_prompt) and (model_name == last_model_name):
|
236 |
-
show = False
|
237 |
-
|
238 |
-
if prompt:
|
239 |
-
st.sidebar.info("Select 'Custom Prompt' to ask your own questions.")
|
240 |
-
|
241 |
-
if show:
|
242 |
-
# Add user input to chat history
|
243 |
-
user_response = get_from_user(prompt)
|
244 |
-
st.session_state.responses.append(user_response)
|
245 |
-
|
246 |
-
# select random waiting line
|
247 |
-
with st.spinner(random.choice(waiting_lines)):
|
248 |
-
ran = False
|
249 |
-
for i in range(1):
|
250 |
-
print(f"Attempt {i+1}")
|
251 |
-
if model_name == "gemini-pro":
|
252 |
-
llm = GoogleGenerativeAI(
|
253 |
-
model=models[model_name], google_api_key=os.getenv("GEMINI_TOKEN"), temperature=0
|
254 |
-
)
|
255 |
-
else:
|
256 |
-
llm = ChatGroq(model=models[model_name], api_key=os.getenv("GROQ_API"), temperature=0)
|
257 |
-
|
258 |
-
df_check = pd.read_csv("Data.csv")
|
259 |
-
df_check["Timestamp"] = pd.to_datetime(df_check["Timestamp"])
|
260 |
-
df_check = df_check.head(5)
|
261 |
-
|
262 |
-
new_line = "\n"
|
263 |
-
|
264 |
-
parameters = {"font.size": 12, "figure.dpi": 600}
|
265 |
-
|
266 |
-
template = f"""```python
|
267 |
-
import pandas as pd
|
268 |
-
import matplotlib.pyplot as plt
|
269 |
-
|
270 |
-
plt.rcParams.update({parameters})
|
271 |
-
|
272 |
-
df = pd.read_csv("Data.csv")
|
273 |
-
df["Timestamp"] = pd.to_datetime(df["Timestamp"])
|
274 |
-
|
275 |
-
import geopandas as gpd
|
276 |
-
india = gpd.read_file("https://gist.githubusercontent.com/jbrobst/56c13bbbf9d97d187fea01ca62ea5112/raw/e388c4cae20aa53cb5090210a42ebb9b765c0a36/india_states.geojson")
|
277 |
-
india.loc[india['ST_NM'].isin(['Ladakh', 'Jammu & Kashmir']), 'ST_NM'] = 'Jammu and Kashmir'
|
278 |
-
import uuid
|
279 |
-
# df.dtypes
|
280 |
-
{new_line.join(map(lambda x: '# '+x, str(df_check.dtypes).split(new_line)))}
|
281 |
-
|
282 |
-
{new_line.join(['# '+line for line in prompt.strip().split(new_line)])}
|
283 |
-
"""
|
284 |
-
with open("system_prompt.txt") as f:
|
285 |
-
system_prompt = f.read().strip()
|
286 |
-
query = f"""{system_prompt}
|
287 |
-
|
288 |
-
Complete the following code.
|
289 |
-
|
290 |
-
{template}
|
291 |
-
|
292 |
-
"""
|
293 |
-
|
294 |
-
answer = None
|
295 |
-
code = None
|
296 |
-
error = None
|
297 |
-
try:
|
298 |
-
if model_name == "gemini-pro":
|
299 |
-
answer = llm.invoke(query)
|
300 |
-
else:
|
301 |
-
answer = llm.invoke(query).content
|
302 |
-
code = f"""
|
303 |
-
{template.split("```python")[1].split("```")[0]}
|
304 |
-
{answer.split("```python")[1].split("```")[0]}
|
305 |
-
"""
|
306 |
-
# update variable `answer` when code is executed
|
307 |
-
exec(code)
|
308 |
-
ran = True
|
309 |
-
except Exception as e:
|
310 |
-
error = e
|
311 |
-
if code is not None:
|
312 |
-
answer = f"Error executing the code...\n\n{e}"
|
313 |
-
|
314 |
-
if type(answer) != str:
|
315 |
-
answer = f"!!!Faced an error while working on your query. Please try again!!!"
|
316 |
-
|
317 |
-
response = {
|
318 |
-
"role": "assistant",
|
319 |
-
"content": answer,
|
320 |
-
"gen_code": code,
|
321 |
-
"ex_code": code,
|
322 |
-
"last_prompt": prompt,
|
323 |
-
"error": error,
|
324 |
-
}
|
325 |
-
|
326 |
-
try:
|
327 |
-
print("Trying to open image", answer)
|
328 |
-
img = Image.open(answer)
|
329 |
-
print("Image opened")
|
330 |
-
image = answer
|
331 |
-
answer = None
|
332 |
-
except:
|
333 |
-
image = None
|
334 |
-
|
335 |
-
item = {
|
336 |
-
"prompt": prompt,
|
337 |
-
"code": code,
|
338 |
-
"answer": answer,
|
339 |
-
"error": error,
|
340 |
-
"model": model_name,
|
341 |
-
"image": image,
|
342 |
-
}
|
343 |
-
|
344 |
-
# Update to HuggingFace dataset
|
345 |
-
dataset_config = get_dataset_config_info("SustainabilityLabIITGN/VayuBuddy_logs", token=hf_token)
|
346 |
-
splits = dataset_config.splits
|
347 |
-
last_split = list(splits.keys())[-1]
|
348 |
-
last_split_size = splits[last_split].num_examples
|
349 |
-
|
350 |
-
ds = load_dataset("SustainabilityLabIITGN/VayuBuddy_logs", token=hf_token, split=last_split)
|
351 |
-
if last_split_size >= 100:
|
352 |
-
current_split = str(int(last_split) + 1)
|
353 |
-
ds = Dataset.from_list([item], features=ds.features)
|
354 |
-
else:
|
355 |
-
current_split = last_split
|
356 |
-
ds = ds.add_item(item)
|
357 |
-
|
358 |
-
ds.push_to_hub("SustainabilityLabIITGN/VayuBuddy_logs", split=current_split, token=hf_token)
|
359 |
-
|
360 |
-
# Get response from agent
|
361 |
-
# response = ask_question(model_name=model_name, question=prompt)
|
362 |
-
# response = ask_agent(agent, prompt)
|
363 |
-
|
364 |
-
if ran:
|
365 |
-
break
|
366 |
-
|
367 |
-
# Append agent response to chat history
|
368 |
-
st.session_state.responses.append(response)
|
369 |
-
|
370 |
-
st.session_state["last_prompt"] = prompt
|
371 |
-
st.session_state["last_model_name"] = model_name
|
372 |
-
st.rerun()
|
373 |
-
|
374 |
-
|
375 |
-
# contact details
|
376 |
-
contact_details = """
|
377 |
-
**Feel free to reach out to us:**
|
378 |
-
- [Zeel B Patel](https://patel-zeel.github.io/)
|
379 |
-
(PhD Student, IIT Gandhinagar)
|
380 |
-
- Vinayak Rana
|
381 |
-
(Developer, IIT Gandhinagar)
|
382 |
-
- Nitish Sharma
|
383 |
-
(Developer, Independent Contributor)
|
384 |
-
- Yash J Bachwana
|
385 |
-
(Developer, IIT Gandhinagar)
|
386 |
-
- [Nipun Batra](https://nipunbatra.github.io/)
|
387 |
-
(Faculty, IIT Gandhinagar)
|
388 |
-
"""
|
389 |
-
|
390 |
-
|
391 |
-
# Display contact details with message
|
392 |
-
st.sidebar.markdown("<hr>", unsafe_allow_html=True)
|
393 |
-
st.sidebar.markdown(contact_details, unsafe_allow_html=True)
|
394 |
-
|
395 |
-
|
396 |
-
st.markdown(
|
397 |
-
"""
|
398 |
-
<style>
|
399 |
-
.sidebar .sidebar-content {
|
400 |
-
position: sticky;
|
401 |
-
top: 0;
|
402 |
-
height: 100vh;
|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