Zeel commited on
Commit
18bcedb
1 Parent(s): b30aa2f

Updated with feedback option

Browse files
Files changed (5) hide show
  1. .gitignore +2 -0
  2. Groq.txt +0 -1
  3. app.py +215 -428
  4. questions.txt +20 -20
  5. src.py +3 -1
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ *.pyc
2
+ *.png
Groq.txt DELETED
@@ -1 +0,0 @@
1
- GROQ_API_KEY = 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
 
 
app.py CHANGED
@@ -1,284 +1,19 @@
1
- # import streamlit as st
2
- # import os
3
- # import pandas as pd
4
- # import random
5
- # from os.path import join
6
- # from src import preprocess_and_load_df, load_agent, ask_agent, decorate_with_code, show_response, get_from_user, load_smart_df, ask_question
7
- # from dotenv import load_dotenv
8
- # from langchain_groq.chat_models import ChatGroq
9
-
10
- # load_dotenv("Groq.txt")
11
- # Groq_Token = os.environ["GROQ_API_KEY"]
12
- # models = {"llama3":"llama3-70b-8192","mixtral": "mixtral-8x7b-32768", "llama2": "llama2-70b-4096", "gemma": "gemma-7b-it"}
13
-
14
- # self_path = os.path.dirname(os.path.abspath(__file__))
15
-
16
- # # Using HTML and CSS to center the title
17
- # st.write(
18
- # """
19
- # <style>
20
- # .title {
21
- # text-align: center;
22
- # color: #17becf;
23
- # }
24
- # """,
25
- # unsafe_allow_html=True,
26
- # )
27
-
28
- # # Displaying the centered title
29
- # st.markdown("<h2 class='title'>VayuBuddy</h2>", unsafe_allow_html=True)
30
- # st.markdown("<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>", unsafe_allow_html=True)
31
-
32
- # # Center-aligned instruction text with bold formatting
33
- # st.markdown("<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>", unsafe_allow_html=True)
34
- # # os.environ["PANDASAI_API_KEY"] = "$2a$10$gbmqKotzJOnqa7iYOun8eO50TxMD/6Zw1pLI2JEoqncwsNx4XeBS2"
35
-
36
- # # with open(join(self_path, "context1.txt")) as f:
37
- # # context = f.read().strip()
38
-
39
- # # agent = load_agent(join(self_path, "app_trial_1.csv"), context)
40
- # # df = preprocess_and_load_df(join(self_path, "Data.csv"))
41
- # # inference_server = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
42
- # # inference_server = "https://api-inference.huggingface.co/models/codellama/CodeLlama-13b-hf"
43
- # # inference_server = "https://api-inference.huggingface.co/models/pandasai/bamboo-llm"
44
-
45
- # model_name = st.sidebar.selectbox("Select LLM:", ["llama3","mixtral", "gemma"])
46
-
47
- # questions = ('Custom Prompt',
48
- # 'Plot the monthly average PM2.5 for the year 2023.',
49
- # 'Which month in which year has the highest average PM2.5 overall?',
50
- # 'Which month in which year has the highest PM2.5 overall?',
51
- # 'Which month has the highest average PM2.5 in 2023 for Mumbai?',
52
- # 'Plot and compare monthly timeseries of pollution for Mumbai and Bengaluru.',
53
- # 'Plot the yearly average PM2.5.',
54
- # 'Plot the monthly average PM2.5 of Delhi, Mumbai and Bengaluru for the year 2022.',
55
- # 'Which month has the highest pollution?',
56
- # 'Which city has the highest PM2.5 level in July 2022?',
57
- # 'Plot and compare monthly timeseries of PM2.5 for Mumbai and Bengaluru.',
58
- # 'Plot and compare the monthly average PM2.5 of Delhi, Mumbai and Bengaluru for the year 2022.',
59
- # 'Plot the monthly average PM2.5.',
60
- # 'Plot the monthly average PM10 for the year 2023.',
61
- # 'Which (month, year) has the highest PM2.5?',
62
- # 'Plot the monthly average PM2.5 of Delhi for the year 2022.',
63
- # 'Plot the monthly average PM2.5 of Bengaluru for the year 2022.',
64
- # 'Plot the monthly average PM2.5 of Mumbai for the year 2022.',
65
- # 'Which state has the highest average PM2.5?',
66
- # 'Plot monthly PM2.5 in Gujarat for 2023.',
67
- # 'What is the name of the month with the highest average PM2.5 overall?')
68
-
69
- # waiting_lines = ("Thinking...", "Just a moment...", "Let me think...", "Working on it...", "Processing...", "Hold on...", "One moment...", "On it...")
70
-
71
- # # agent = load_agent(df, context="", inference_server=inference_server, name=model_name)
72
-
73
- # # Initialize chat history
74
- # if "responses" not in st.session_state:
75
- # st.session_state.responses = []
76
-
77
- # # Display chat responses from history on app rerun
78
- # for response in st.session_state.responses:
79
- # if not response["no_response"]:
80
- # show_response(st, response)
81
-
82
- # show = True
83
-
84
- # if prompt := st.sidebar.selectbox("Select a Prompt:", questions):
85
-
86
- # # add a note "select custom prompt to ask your own question"
87
- # st.sidebar.info("Select 'Custom Prompt' to ask your own question.")
88
-
89
- # if prompt == 'Custom Prompt':
90
- # show = False
91
- # # React to user input
92
- # prompt = st.chat_input("Ask me anything about air quality!", key=10)
93
- # if prompt : show = True
94
- # if show :
95
-
96
- # # Add user input to chat history
97
- # response = get_from_user(prompt)
98
- # response["no_response"] = False
99
- # st.session_state.responses.append(response)
100
-
101
- # # Display user input
102
- # show_response(st, response)
103
-
104
- # no_response = False
105
-
106
- # # select random waiting line
107
- # with st.spinner(random.choice(waiting_lines)):
108
- # ran = False
109
- # for i in range(1):
110
- # print(f"Attempt {i+1}")
111
- # llm = ChatGroq(model=models[model_name], api_key=os.getenv("GROQ_API"), temperature=0)
112
-
113
- # df_check = pd.read_csv("Data.csv")
114
- # df_check["Timestamp"] = pd.to_datetime(df_check["Timestamp"])
115
- # df_check = df_check.head(5)
116
-
117
- # new_line = "\n"
118
-
119
- # parameters = {"font.size": 12}
120
-
121
- # template = f"""```python
122
- # import pandas as pd
123
- # import matplotlib.pyplot as plt
124
-
125
- # # plt.rcParams.update({parameters})
126
-
127
- # df = pd.read_csv("Data.csv")
128
- # df["Timestamp"] = pd.to_datetime(df["Timestamp"])
129
-
130
- # import geopandas as gpd
131
- # india = gpd.read_file("https://gist.githubusercontent.com/jbrobst/56c13bbbf9d97d187fea01ca62ea5112/raw/e388c4cae20aa53cb5090210a42ebb9b765c0a36/india_states.geojson")
132
- # india.loc[india['ST_NM'].isin(['Ladakh', 'Jammu & Kashmir']), 'ST_NM'] = 'Jammu and Kashmir'
133
-
134
- # # df.dtypes
135
- # {new_line.join(map(lambda x: '# '+x, str(df_check.dtypes).split(new_line)))}
136
-
137
- # # {prompt.strip()}
138
- # # <your code here>
139
- # ```
140
- # """
141
-
142
- # query = f"""I have a pandas dataframe data of PM2.5 and PM10.
143
- # * The columns are 'Timestamp', 'station', 'PM2.5', 'PM10', 'address', 'city', 'latitude', 'longitude',and 'state'.
144
- # * Frequency of data is daily.
145
- # * `pollution` generally means `PM2.5`.
146
- # * You already have df, so don't read the csv file
147
- # * Don't print anything, but save result in a variable `answer` and make it global.
148
- # * Unless explicitly mentioned, don't consider the result as a plot.
149
- # * PM2.5 guidelines: India: 60, WHO: 15.
150
- # * PM10 guidelines: India: 100, WHO: 50.
151
- # * If result is a plot, show the India and WHO guidelines in the plot.
152
- # * If result is a plot make it in tight layout, save it and save path in `answer`. Example: `answer='plot.png'`
153
- # * If result is a plot, rotate x-axis tick labels by 45 degrees,
154
- # * If result is not a plot, save it as a string in `answer`. Example: `answer='The city is Mumbai'`
155
- # * I have a geopandas.geodataframe india containining the coordinates required to plot Indian Map with states.
156
- # * If the query asks you to plot on India Map, use that geodataframe to plot and then add more points as per the requirements using the similar code as follows : v = ax.scatter(df['longitude'], df['latitude']). If the colorbar is required, use the following code : plt.colorbar(v)
157
- # * If the query asks you to plot on India Map plot the India Map in Beige color
158
- # * Whenever you do any sort of aggregation, report the corresponding standard deviation, standard error and the number of data points for that aggregation.
159
- # * Whenever you're reporting a floating point number, round it to 2 decimal places.
160
- # * Always report the unit of the data. Example: `The average PM2.5 is 45.67 µg/m³`
161
-
162
- # Complete the following code.
163
-
164
- # {template}
165
-
166
- # """
167
- # answer = None
168
- # code = None
169
- # try:
170
- # answer = llm.invoke(query)
171
- # code = f"""
172
- # {template.split("```python")[1].split("```")[0]}
173
- # {answer.content.split("```python")[1].split("```")[0]}
174
- # """
175
- # # update variable `answer` when code is executed
176
- # exec(code)
177
- # ran = True
178
- # no_response = False
179
- # except Exception as e:
180
- # no_response = True
181
- # exception = e
182
- # if code is not None:
183
- # answer = f"!!!Faced an error while working on your query. Please try again!!!"
184
-
185
- # if type(answer) != str:
186
- # answer = f"!!!Faced an error while working on your query. Please try again!!!"
187
-
188
- # response = {"role": "assistant", "content": answer, "gen_code": code, "ex_code": code, "last_prompt": prompt, "no_response": no_response}
189
-
190
- # # Get response from agent
191
- # # response = ask_question(model_name=model_name, question=prompt)
192
- # # response = ask_agent(agent, prompt)
193
-
194
- # if ran:
195
- # break
196
-
197
- # # Display agent response
198
- # if code is not None:
199
- # # Add agent response to chat history
200
- # print("Adding response")
201
-
202
- # st.session_state.responses.append(response)
203
- # show_response(st, response)
204
-
205
- # if no_response:
206
- # print("No response")
207
- # st.error(f"Failed to generate right output due to the following error:\n\n{exception}")
208
-
209
-
210
-
211
- # prompt = 'Custom Prompt'
212
-
213
-
214
-
215
-
216
-
217
- ####################################################Added User Feedback###################################################
218
-
219
  import streamlit as st
220
  import os
 
221
  import pandas as pd
222
  import random
223
  from os.path import join
 
224
  from src import preprocess_and_load_df, load_agent, ask_agent, decorate_with_code, show_response, get_from_user, load_smart_df, ask_question
225
  from dotenv import load_dotenv
226
  from langchain_groq.chat_models import ChatGroq
227
-
228
-
229
- from datasets import Dataset, load_dataset, concatenate_datasets
230
- import streamlit as st
231
  from streamlit_feedback import streamlit_feedback
232
- import uuid
233
-
234
- from huggingface_hub import login, HfFolder
235
- import os
236
 
237
- # Set the token
238
- token = os.getenv("HF_TOKEN") # Replace "YOUR_AUTHENTICATION_TOKEN" with your actual token
239
- shape_file = os.getenv("SHAPE_FILE")
240
-
241
- # Login using the token
242
-
243
- login(token=token)
244
-
245
- model_name = st.sidebar.selectbox("Select LLM:", ["llama3","mixtral", "gemma"])
246
-
247
- contact_details = """
248
- **Feel free to reach out to us:**
249
- - [Nipun Batra](mailto:nipun.batra@iitgn.ac.in)
250
- - [Zeel B Patel](mailto:patel_zeel@iitgn.ac.in)
251
- - [Yash J Bachwana](mailto:yash.bachwana@iitgn.ac.in)
252
- """
253
- for _ in range(9):
254
- st.sidebar.markdown(" ")
255
-
256
- # Display contact details with message
257
- st.sidebar.markdown("<hr>", unsafe_allow_html=True)
258
- st.sidebar.markdown(contact_details, unsafe_allow_html=True)
259
-
260
- # Function to push feedback data to Hugging Face Hub dataset
261
- def push_to_dataset(feedback, comments,output,code,error):
262
- # Load existing dataset or create a new one if it doesn't exist
263
- try:
264
- ds = load_dataset("YashB1/Feedbacks_eoc", split="evaluation")
265
- except FileNotFoundError:
266
- # If dataset doesn't exist, create a new one
267
- ds = Dataset.from_dict({"feedback": [], "comments": [], "error": [], "output": [], "code": []})
268
-
269
- # Add new feedback to the dataset
270
- new_data = {"feedback": [feedback], "comments": [comments], "error": [error], "output": [output], "code": [code]} # Convert feedback and comments to lists
271
- new_data = Dataset.from_dict(new_data)
272
-
273
- ds = concatenate_datasets([ds, new_data])
274
-
275
- # Push the updated dataset to Hugging Face Hub
276
- ds.push_to_hub("YashB1/Feedbacks_eoc", split="evaluation")
277
-
278
-
279
-
280
- load_dotenv("Groq.txt")
281
  Groq_Token = os.environ["GROQ_API_KEY"]
 
282
  models = {"llama3":"llama3-70b-8192","mixtral": "mixtral-8x7b-32768", "llama2": "llama2-70b-4096", "gemma": "gemma-7b-it"}
283
 
284
  self_path = os.path.dirname(os.path.abspath(__file__))
@@ -312,83 +47,159 @@ st.markdown("<div style='text-align:center;'>Choose a query from <b>Select a pro
312
  # inference_server = "https://api-inference.huggingface.co/models/codellama/CodeLlama-13b-hf"
313
  # inference_server = "https://api-inference.huggingface.co/models/pandasai/bamboo-llm"
314
 
315
- # model_name = st.sidebar.selectbox("Select LLM:", ["llama3","mixtral", "gemma"])
316
-
317
-
318
- if 'question_state' not in st.session_state:
319
- st.session_state.question_state = False
320
-
321
- if 'fbk' not in st.session_state:
322
- st.session_state.fbk = str(uuid.uuid4())
323
-
324
- if 'feedback' not in st.session_state:
325
- st.session_state.feedback = None
326
-
327
- if "chat_history" not in st.session_state:
328
- st.session_state.chat_history = []
329
-
330
 
331
- def display_answer():
332
- for entry in st.session_state.chat_history:
333
- with st.chat_message("human"):
334
- st.write(entry["question"])
335
-
336
- # st.write(entry["answer"])
337
- # print(entry["answer"])
338
- show_response(st, entry["answer"])
339
 
 
340
 
 
341
 
342
- def fbcb(response):
343
- """Update the history with feedback.
 
344
 
345
- The question and answer are already saved in history.
346
- Now we will add the feedback in that history entry.
347
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
 
349
- display_answer() # display hist
350
-
351
- # Create a new feedback by changing the key of feedback component.
352
- st.session_state.fbk = str(uuid.uuid4())
 
 
 
353
 
 
354
 
 
355
 
356
- question = st.chat_input(placeholder="Ask your question here .... !!!!")
357
- if question:
358
- # We need this because of feedback. That question above
359
- # is a stopper. If user hits the feedback button, streamlit
360
- # reruns the code from top and we cannot enter back because
361
- # of that chat_input.
362
- st.session_state.prompt = question
363
- st.session_state.question_state = True
364
-
365
 
 
366
 
 
367
 
368
- # We are now free because st.session_state.question_state is True.
369
- # But there are consequences. We will have to handle
370
- # the double runs of create_answer() and display_answer()
371
- # just to get the user feedback.
372
- if st.session_state.question_state:
373
 
374
- waiting_lines = ("Thinking...", "Just a moment...", "Let me think...", "Working on it...", "Processing...", "Hold on...", "One moment...", "On it...")
375
- with st.spinner(random.choice(waiting_lines)):
376
- ran = False
377
- for i in range(5):
378
- print(f"Attempt {i+1}")
379
- llm = ChatGroq(model=models[model_name], api_key=os.getenv("GROQ_API"), temperature=0)
380
-
381
- df_check = pd.read_csv("Data.csv")
382
- df_check["Timestamp"] = pd.to_datetime(df_check["Timestamp"])
383
- df_check = df_check.head(5)
384
-
385
- new_line = "\n"
386
-
387
- parameters = {"font.size": 12}
388
- # If the query asks you to make a gif/animation, don't use savefig to save it. Instead use ani.save(answer, writer='pillow').
389
- # If the query asks you to make a gif/animation, don't use colormaps .
390
-
391
- template = f"""```python
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
392
  import pandas as pd
393
  import matplotlib.pyplot as plt
394
 
@@ -398,113 +209,89 @@ df = pd.read_csv("Data.csv")
398
  df["Timestamp"] = pd.to_datetime(df["Timestamp"])
399
 
400
  import geopandas as gpd
401
- file_path = "india_states.geojson"
402
- india = gpd.read_file(f"{shape_file}")
403
  india.loc[india['ST_NM'].isin(['Ladakh', 'Jammu & Kashmir']), 'ST_NM'] = 'Jammu and Kashmir'
404
 
405
-
406
  # df.dtypes
407
  {new_line.join(map(lambda x: '# '+x, str(df_check.dtypes).split(new_line)))}
408
 
409
- # {st.session_state.prompt.strip()}
410
  # <your code here>
411
  ```
412
  """
413
 
414
- query = f"""I have a pandas dataframe data of PM2.5 and PM10.
415
- * The columns are 'Timestamp', 'station', 'PM2.5', 'PM10', 'address', 'city', 'latitude', 'longitude',and 'state'.
416
- * Frequency of data is daily.
417
- * `pollution` generally means `PM2.5`.
418
- * You already have df, so don't read the csv file
419
- * Don't print anything, but save result in a variable `answer` and make it global.
420
- * Unless explicitly mentioned, don't consider the result as a plot.
421
- * PM2.5 guidelines: India: 60, WHO: 15.
422
- * PM10 guidelines: India: 100, WHO: 50.
423
- * If query asks to plot calendarmap, use library calmap.
424
- * If result is a plot, show the India and WHO guidelines in the plot.
425
- * If result is a plot make it in tight layout, save it and save path in `answer`. Example: `answer='plot.png'`
426
- * If result is a plot, rotate x-axis tick labels by 45 degrees,
427
- * If result is not a plot, save it as a string in `answer`. Example: `answer='The city is Mumbai'`
428
- * I have a geopandas.geodataframe india containining the coordinates required to plot Indian Map with states.
429
- * If the query asks you to plot on India Map, use that geodataframe to plot and then add more points as per the requirements using the similar code as follows : v = ax.scatter(df['longitude'], df['latitude']). If the colorbar is required, use the following code : plt.colorbar(v)
430
- * If the query asks you to plot on India Map plot the India Map in Beige color
431
- * Whenever you do any sort of aggregation, report the corresponding standard deviation, standard error and the number of data points for that aggregation.
432
- * Whenever you're reporting a floating point number, round it to 2 decimal places.
433
- * Always report the unit of the data. Example: `The average PM2.5 is 45.67 µg/m³`
434
-
435
- Complete the following code.
436
-
437
- {template}
438
-
439
- """
440
- answer = None
441
- code = None
442
- exception = None
443
- try:
444
- answer = llm.invoke(query)
445
- code = f"""
446
- {template.split("```python")[1].split("```")[0]}
447
- {answer.content.split("```python")[1].split("```")[0]}
448
  """
449
- # update variable `answer` when code is executed
450
- exec(code)
451
- ran = True
452
- no_response = False
453
- except Exception as e:
454
- no_response = True
455
- exception = e
456
- if code is not None:
 
 
 
 
 
 
 
 
 
 
457
  answer = f"!!!Faced an error while working on your query. Please try again!!!"
458
-
459
- if type(answer) != str:
460
- answer = f"!!!Faced an error while working on your query. Please try again!!!"
461
-
462
- response = {"role": "assistant", "content": answer, "gen_code": code, "ex_code": code, "last_prompt": st.session_state.prompt, "no_response": no_response,"exception": exception}
463
- # print(response)
464
 
465
- if ran:
466
- break
467
-
468
- # Display agent response
469
- if code is not None:
470
- # Add agent response to chat history
471
- if response['content'][-4:] == ".gif" :
472
- # Provide a button to show the gif, we don't want it to run forever
473
- st.image(response['content'], use_column_width=True)
474
- response['content'] = ""
475
 
 
 
 
 
 
 
476
 
477
- print("Adding response : ")
478
 
 
 
 
 
 
 
 
 
 
479
 
480
- message_id = len(st.session_state.chat_history)
481
- st.session_state.chat_history.append({
482
- "question": st.session_state.prompt,
483
- "answer": response,
484
- "message_id": message_id,
485
- })
486
- display_answer()
487
-
488
-
489
- if no_response:
490
- print("No response")
491
- st.error(f"Failed to generate right output due to the following error:\n\n{exception}")
492
-
493
-
494
- # display_answer()
495
- # Pressing a button in feedback reruns the code.
496
- st.session_state.feedback = streamlit_feedback(
497
- feedback_type="thumbs",
498
- optional_text_label="[Optional]",
499
- align="flex-start",
500
- key=st.session_state.fbk,
501
- on_submit=fbcb
502
- )
503
- print("FeedBack",st.session_state.feedback)
504
- if st.session_state.feedback :
505
- push_to_dataset(st.session_state.feedback['score'],st.session_state.feedback['text'],answer,code,exception)
506
- st.success("Feedback submitted successfully!")
507
-
508
-
509
-
510
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 preprocess_and_load_df, load_agent, ask_agent, decorate_with_code, show_response, get_from_user, load_smart_df, ask_question
9
  from dotenv import load_dotenv
10
  from langchain_groq.chat_models import ChatGroq
 
 
 
 
11
  from streamlit_feedback import streamlit_feedback
12
+ from huggingface_hub import HfApi
 
 
 
13
 
14
+ load_dotenv()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  Groq_Token = os.environ["GROQ_API_KEY"]
16
+ hf_token = os.environ["HF_TOKEN"]
17
  models = {"llama3":"llama3-70b-8192","mixtral": "mixtral-8x7b-32768", "llama2": "llama2-70b-4096", "gemma": "gemma-7b-it"}
18
 
19
  self_path = os.path.dirname(os.path.abspath(__file__))
 
47
  # inference_server = "https://api-inference.huggingface.co/models/codellama/CodeLlama-13b-hf"
48
  # inference_server = "https://api-inference.huggingface.co/models/pandasai/bamboo-llm"
49
 
50
+ model_name = st.sidebar.selectbox("Select LLM:", ["llama3","mixtral", "gemma"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ questions = ['Custom Prompt']
53
+ with open(join(self_path, "questions.txt")) as f:
54
+ questions += f.read().split("\n")
 
 
 
 
 
55
 
56
+ waiting_lines = ("Thinking...", "Just a moment...", "Let me think...", "Working on it...", "Processing...", "Hold on...", "One moment...", "On it...")
57
 
58
+ # agent = load_agent(df, context="", inference_server=inference_server, name=model_name)
59
 
60
+ # Initialize chat history
61
+ if "responses" not in st.session_state:
62
+ st.session_state.responses = []
63
 
64
+ ### Old code for feedback
65
+ # def push_to_dataset(feedback, comments,output,code,error):
66
+ # # Load existing dataset or create a new one if it doesn't exist
67
+ # try:
68
+ # ds = load_dataset("YashB1/Feedbacks_eoc", split="evaluation")
69
+ # except FileNotFoundError:
70
+ # # If dataset doesn't exist, create a new one
71
+ # ds = Dataset.from_dict({"feedback": [], "comments": [], "error": [], "output": [], "code": []})
72
+
73
+ # # Add new feedback to the dataset
74
+ # new_data = {"feedback": [feedback], "comments": [comments], "error": [error], "output": [output], "code": [code]} # Convert feedback and comments to lists
75
+ # new_data = Dataset.from_dict(new_data)
76
+
77
+ # ds = concatenate_datasets([ds, new_data])
78
+
79
+ # # Push the updated dataset to Hugging Face Hub
80
+ # ds.push_to_hub("YashB1/Feedbacks_eoc", split="evaluation")
81
+
82
+ def upload_feedback():
83
+ print("Uploading feedback")
84
+ data = {
85
+ "feedback": feedback['score'],
86
+ "comment": feedback['text'], "error": error, "output": output, "prompt": last_prompt, "code": code}
87
 
88
+ # generate a random file name based on current time-stamp: YYYY-MM-DD_HH-MM-SS
89
+ random_folder_name = str(datetime.now()).replace(" ", "_").replace(":", "-").replace(".", "-")
90
+ print("Random folder:", random_folder_name)
91
+ save_path = f"/tmp/vayubuddy_feedback.md"
92
+ path_in_repo = f"data/{random_folder_name}/feedback.md"
93
+ with open(save_path, "w") as f:
94
+ template = f"""Prompt: {last_prompt}
95
 
96
+ Output: {output}
97
 
98
+ Code:
99
 
100
+ ```py
101
+ {code}
102
+ ```
 
 
 
 
 
 
103
 
104
+ Error: {error}
105
 
106
+ Feedback: {feedback['score']}
107
 
108
+ Comments: {feedback['text']}
109
+ """
110
+
111
+ print(template, file=f)
 
112
 
113
+ api = HfApi(token=hf_token)
114
+ api.upload_file(
115
+ path_or_fileobj=save_path,
116
+ path_in_repo=path_in_repo,
117
+ repo_id="SustainabilityLabIITGN/VayuBuddy_Feedback",
118
+ repo_type="dataset",
119
+ )
120
+ if status['is_image']:
121
+ api.upload_file(
122
+ path_or_fileobj=output,
123
+ path_in_repo=f"data/{random_folder_name}/plot.png",
124
+ repo_id="SustainabilityLabIITGN/VayuBuddy_Feedback",
125
+ repo_type="dataset",
126
+ )
127
+
128
+ print("Feedback uploaded successfully!")
129
+
130
+ # Display chat responses from history on app rerun
131
+ print("#"*10)
132
+ for response_id, response in enumerate(st.session_state.responses):
133
+ status = show_response(st, response)
134
+ if response["role"] == "assistant":
135
+ feedback_key = f"feedback_{int(response_id/2)}"
136
+ print("response_id", response_id, "feedback_key", feedback_key)
137
+
138
+ error = response["error"]
139
+ output = response["content"]
140
+ last_prompt = response["last_prompt"]
141
+ code = response["gen_code"]
142
+
143
+ if "feedback" in st.session_state.responses[response_id]:
144
+ st.write("Feedback:", st.session_state.responses[response_id]["feedback"])
145
+ else:
146
+ ## !!! This does on work on Safari !!!
147
+ # feedback = streamlit_feedback(feedback_type="thumbs",
148
+ # optional_text_label="[Optional] Please provide extra information", on_submit=upload_feedback, key=feedback_key)
149
+
150
+ # Display thumbs up/down buttons for feedback
151
+ thumbs = st.radio("We would appreciate your feedback!", ('👍', '👎'), index=None, key=feedback_key)
152
+
153
+ if thumbs:
154
+ # Text input for comments
155
+ comments = st.text_area("[Optional] Please provide extra information", key=feedback_key+"_comments")
156
+ feedback = {"score": thumbs, "text": comments}
157
+ if st.button("Submit", on_click=upload_feedback, key=feedback_key+"_submit"):
158
+ st.session_state.responses[response_id]["feedback"] = feedback
159
+ st.success("Feedback uploaded successfully!")
160
+
161
+
162
+ print("#"*10)
163
+
164
+ show = True
165
+ prompt = st.sidebar.selectbox("Select a Prompt:", questions, key="prompt_key")
166
+ if prompt == 'Custom Prompt':
167
+ show = False
168
+ # React to user input
169
+ prompt = st.chat_input("Ask me anything about air quality!", key=1000)
170
+ if prompt :
171
+ show = True
172
+
173
+ if "last_prompt" in st.session_state:
174
+ last_prompt = st.session_state["last_prompt"]
175
+ last_model_name = st.session_state["last_model_name"]
176
+ if (prompt == last_prompt) and (model_name == last_model_name):
177
+ show = False
178
+
179
+ if prompt:
180
+ st.sidebar.info("Select 'Custom Prompt' to ask your own questions.")
181
+
182
+ if show:
183
+ # Add user input to chat history
184
+ user_response = get_from_user(prompt)
185
+ st.session_state.responses.append(user_response)
186
+
187
+ # select random waiting line
188
+ with st.spinner(random.choice(waiting_lines)):
189
+ ran = False
190
+ for i in range(1):
191
+ print(f"Attempt {i+1}")
192
+ llm = ChatGroq(model=models[model_name], api_key=os.getenv("GROQ_API"), temperature=0)
193
+
194
+ df_check = pd.read_csv("Data.csv")
195
+ df_check["Timestamp"] = pd.to_datetime(df_check["Timestamp"])
196
+ df_check = df_check.head(5)
197
+
198
+ new_line = "\n"
199
+
200
+ parameters = {"font.size": 12}
201
+
202
+ template = f"""```python
203
  import pandas as pd
204
  import matplotlib.pyplot as plt
205
 
 
209
  df["Timestamp"] = pd.to_datetime(df["Timestamp"])
210
 
211
  import geopandas as gpd
212
+ india = gpd.read_file("https://gist.githubusercontent.com/jbrobst/56c13bbbf9d97d187fea01ca62ea5112/raw/e388c4cae20aa53cb5090210a42ebb9b765c0a36/india_states.geojson")
 
213
  india.loc[india['ST_NM'].isin(['Ladakh', 'Jammu & Kashmir']), 'ST_NM'] = 'Jammu and Kashmir'
214
 
 
215
  # df.dtypes
216
  {new_line.join(map(lambda x: '# '+x, str(df_check.dtypes).split(new_line)))}
217
 
218
+ # {prompt.strip()}
219
  # <your code here>
220
  ```
221
  """
222
 
223
+ query = f"""I have a pandas dataframe data of PM2.5 and PM10.
224
+ * The columns are 'Timestamp', 'station', 'PM2.5', 'PM10', 'address', 'city', 'latitude', 'longitude',and 'state'.
225
+ * Frequency of data is daily.
226
+ * `pollution` generally means `PM2.5`.
227
+ * You already have df, so don't read the csv file
228
+ * Don't print anything, but save result in a variable `answer` and make it global.
229
+ * Unless explicitly mentioned, don't consider the result as a plot.
230
+ * PM2.5 guidelines: India: 60, WHO: 15.
231
+ * PM10 guidelines: India: 100, WHO: 50.
232
+ * If result is a plot, show the India and WHO guidelines in the plot.
233
+ * If result is a plot make it in tight layout, save it and save path in `answer`. Example: `answer='plot.png'`
234
+ * If result is a plot, rotate x-axis tick labels by 45 degrees,
235
+ * If result is not a plot, save it as a string in `answer`. Example: `answer='The city is Mumbai'`
236
+ * I have a geopandas.geodataframe india containining the coordinates required to plot Indian Map with states.
237
+ * If the query asks you to plot on India Map, use that geodataframe to plot and then add more points as per the requirements using the similar code as follows : v = ax.scatter(df['longitude'], df['latitude']). If the colorbar is required, use the following code : plt.colorbar(v)
238
+ * If the query asks you to plot on India Map plot the India Map in Beige color
239
+ * Whenever you do any sort of aggregation, report the corresponding standard deviation, standard error and the number of data points for that aggregation.
240
+ * Whenever you're reporting a floating point number, round it to 2 decimal places.
241
+ * Always report the unit of the data. Example: `The average PM2.5 is 45.67 µg/m³`
242
+
243
+ Complete the following code.
244
+
245
+ {template}
246
+
 
 
 
 
 
 
 
 
 
 
247
  """
248
+ answer = None
249
+ code = None
250
+ error = None
251
+ try:
252
+ answer = llm.invoke(query)
253
+ code = f"""
254
+ {template.split("```python")[1].split("```")[0]}
255
+ {answer.content.split("```python")[1].split("```")[0]}
256
+ """
257
+ # update variable `answer` when code is executed
258
+ exec(code)
259
+ ran = True
260
+ except Exception as e:
261
+ error = e
262
+ if code is not None:
263
+ answer = f"!!!Faced an error while working on your query. Please try again!!!"
264
+
265
+ if type(answer) != str:
266
  answer = f"!!!Faced an error while working on your query. Please try again!!!"
267
+
268
+ response = {"role": "assistant", "content": answer, "gen_code": code, "ex_code": code, "last_prompt": prompt, "error": error}
 
 
 
 
269
 
270
+ # Get response from agent
271
+ # response = ask_question(model_name=model_name, question=prompt)
272
+ # response = ask_agent(agent, prompt)
273
+
274
+ if ran:
275
+ break
 
 
 
 
276
 
277
+ # Append agent response to chat history
278
+ st.session_state.responses.append(response)
279
+
280
+ st.session_state['last_prompt'] = prompt
281
+ st.session_state['last_model_name'] = model_name
282
+ st.rerun()
283
 
 
284
 
285
+ # contact details
286
+ contact_details = """
287
+ **Feel free to reach out to us:**
288
+ - [Yash J Bachwana](mailto:yash.bachwana@iitgn.ac.in) (Lead Developer)
289
+ - [Zeel B Patel](mailto:patel_zeel@iitgn.ac.in) (PhD Student)
290
+ - [Nipun Batra](mailto:nipun.batra@iitgn.ac.in) (Faculty)
291
+ """
292
+ for _ in range(9):
293
+ st.sidebar.markdown(" ")
294
 
295
+ # Display contact details with message
296
+ st.sidebar.markdown("<hr>", unsafe_allow_html=True)
297
+ st.sidebar.markdown(contact_details, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
questions.txt CHANGED
@@ -1,20 +1,20 @@
1
- 1. Plot the monthly average PM2.5 for the year 2023.
2
- 2. Which month in which year has the highest average PM2.5 overall?
3
- 3. Which month in which year has the highest PM2.5 overall?
4
- 4. Which month has the highest average PM2.5 in 2023 for Mumbai?
5
- 5. Plot and compare monthly timeseries of pollution for Mumbai and Bengaluru.
6
- 6. Plot the yearly average PM2.5.
7
- 7. Plot the monthly average PM2.5 of Delhi, Mumbai and Bengaluru for the year 2022.
8
- 8. Which month has the highest pollution?
9
- 9. Which city has the highest PM2.5 level in July 2022?
10
- 10. Plot and compare monthly timeseries of PM2.5 for Mumbai and Bengaluru.
11
- 11. Plot and compare the monthly average PM2.5 of Delhi, Mumbai and Bengaluru for the year 2022.
12
- 12. Plot the monthly average PM2.5.
13
- 13. Plot the monthly average PM10 for the year 2023.
14
- 14. Which (month, year) has the highest PM2.5?
15
- 15. Plot the monthly average PM2.5 of Delhi for the year 2022.
16
- 16. Plot the monthly average PM2.5 of Bengaluru for the year 2022.
17
- 17. Plot the monthly average PM2.5 of Mumbai for the year 2022.
18
- 18. Which state has the highest average PM2.5?
19
- 19. Plot monthly PM2.5 in Gujarat for 2023.
20
- 20. What is the name of the month with the highest average PM2.5 overall?
 
1
+ Plot the monthly average PM2.5 for the year 2023.
2
+ Which month in which year has the highest average PM2.5 overall?
3
+ Which month in which year has the highest PM2.5 overall?
4
+ Which month has the highest average PM2.5 in 2023 for Mumbai?
5
+ Plot and compare monthly timeseries of pollution for Mumbai and Bengaluru.
6
+ Plot the yearly average PM2.5.
7
+ Plot the monthly average PM2.5 of Delhi, Mumbai and Bengaluru for the year 2022.
8
+ Which month has the highest pollution?
9
+ Which city has the highest PM2.5 level in July 2022?
10
+ Plot and compare monthly timeseries of PM2.5 for Mumbai and Bengaluru.
11
+ Plot and compare the monthly average PM2.5 of Delhi, Mumbai and Bengaluru for the year 2022.
12
+ Plot the monthly average PM2.5.
13
+ Plot the monthly average PM10 for the year 2023.
14
+ Which (month, year) has the highest PM2.5?
15
+ Plot the monthly average PM2.5 of Delhi for the year 2022.
16
+ Plot the monthly average PM2.5 of Bengaluru for the year 2022.
17
+ Plot the monthly average PM2.5 of Mumbai for the year 2022.
18
+ Which state has the highest average PM2.5?
19
+ Plot monthly PM2.5 in Gujarat for 2023.
20
+ What is the name of the month with the highest average PM2.5 overall?
src.py CHANGED
@@ -7,7 +7,7 @@ from pandasai.llm import HuggingFaceTextGen
7
  from dotenv import load_dotenv
8
  from langchain_groq.chat_models import ChatGroq
9
 
10
- load_dotenv("Groq.txt")
11
  Groq_Token = os.environ["GROQ_API_KEY"]
12
  models = {"mixtral": "mixtral-8x7b-32768", "llama": "llama2-70b-4096", "gemma": "gemma-7b-it"}
13
 
@@ -74,6 +74,7 @@ def show_response(st, response):
74
  if "gen_code" in response:
75
  st.markdown(decorate_with_code(response), unsafe_allow_html=True)
76
  st.image(image)
 
77
  except Exception as e:
78
  if "gen_code" in response:
79
  display_content = decorate_with_code(response) + f"""</details>
@@ -82,6 +83,7 @@ def show_response(st, response):
82
  else:
83
  display_content = response["content"]
84
  st.markdown(display_content, unsafe_allow_html=True)
 
85
 
86
  def ask_question(model_name, question):
87
  llm = ChatGroq(model=models[model_name], api_key=os.getenv("GROQ_API"), temperature=0.1)
 
7
  from dotenv import load_dotenv
8
  from langchain_groq.chat_models import ChatGroq
9
 
10
+ load_dotenv()
11
  Groq_Token = os.environ["GROQ_API_KEY"]
12
  models = {"mixtral": "mixtral-8x7b-32768", "llama": "llama2-70b-4096", "gemma": "gemma-7b-it"}
13
 
 
74
  if "gen_code" in response:
75
  st.markdown(decorate_with_code(response), unsafe_allow_html=True)
76
  st.image(image)
77
+ return {"is_image": True}
78
  except Exception as e:
79
  if "gen_code" in response:
80
  display_content = decorate_with_code(response) + f"""</details>
 
83
  else:
84
  display_content = response["content"]
85
  st.markdown(display_content, unsafe_allow_html=True)
86
+ return {"is_image": False}
87
 
88
  def ask_question(model_name, question):
89
  llm = ChatGroq(model=models[model_name], api_key=os.getenv("GROQ_API"), temperature=0.1)