pr4nav101 commited on
Commit
1cc49b3
·
verified ·
1 Parent(s): 02dfadb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -111
app.py CHANGED
@@ -296,118 +296,12 @@ elif page == "PS3":
296
  elif page == "PS2":
297
  st.header("CLICK ON RUN SCRIPT TO START A TRAFFIC SIMULATION")
298
  script = st.button("RUN SCRIPT")
299
- st.session_state.con = -1
300
- if script:
301
- st.session_state.con += 1
302
- import gymnasium as gym
303
- import sumo_rl
304
- import os
305
- from stable_baselines3 import DQN
306
- from stable_baselines3.common.vec_env import DummyVecEnv
307
- from stable_baselines3.common.evaluation import evaluate_policy
308
- from sumo_rl import SumoEnvironment
309
- env = gym.make('sumo-rl-v0',
310
- net_file='single-intersection.net.xml',
311
- route_file='single-intersection-gen.rou.xml',
312
- out_csv_name='output',
313
- use_gui=True,
314
- single_agent=True,
315
- num_seconds=5000)
316
- model1 = DQN.load('DQN_MODEL3.zip',env=env)
317
- st.write("The Simulation is currently running for 5000 steps, Results will be shown shortly.....")
318
- one,two = evaluate_policy(model1,env = env,n_eval_episodes=5,render=True)
319
- st.write("Evaluation Results: \nPer Episode Rewards(Higher the better):",one,"\nPer-episode lengths (in number of steps):",two)
320
- import matplotlib.pyplot as plt
321
- def eval_plot(path,metric,path_compare = None):
322
- data = pd.read_csv(path)
323
- if path_compare is not None:
324
- data1 = pd.read_csv(path_compare)
325
- x = []
326
- for i in range(0,len(data)):
327
- x.append(i)
328
-
329
- y = data[metric]
330
- y_1 = pd.to_numeric(y)
331
- y_arr = np.array(y_1)
332
- if path_compare is not None:
333
- y2 = data1[metric]
334
- y_2 = pd.to_numeric(y2)
335
- y_arr2 = np.array(y_2)
336
-
337
- x_arr = np.array(x)
338
-
339
- fig = plt.figure()
340
- ax1 = fig.add_subplot(2, 1, 1)
341
- ax1.set_title(metric)
342
- if path_compare is not None:
343
- ax2 = fig.add_subplot(2, 1, 2,sharey=ax1)
344
- ax2.set_title('compare '+metric)
345
-
346
- ax1.plot(x_arr,y_arr)
347
-
348
- if path_compare is not None:
349
- ax2.plot(x_arr,y_arr2)
350
-
351
- return fig
352
- for i in range(1,2):
353
- st.pyplot(eval_plot(f'output_conn{st.session_state.con}_ep{i}.csv','system_mean_waiting_time'))
354
- st.pyplot(eval_plot(f'output_conn{st.session_state.con}_ep{i}.csv','agents_total_accumulated_waiting_time'))
355
 
356
  elif page == "Chat with Results":
357
  st.title('Chat with the Results')
358
  st.write("Please upload the relevant CSV data to get started")
359
- reload = st.button('Reload')
360
- if 'isran' not in st.session_state or reload == True:
361
- st.session_state['isran'] = False
362
-
363
-
364
- uploaded_file = st.file_uploader('Choose your .csv file', type=["csv"])
365
- if uploaded_file is not None and st.session_state['isran'] == False:
366
- with open("temp.csv", "wb") as f:
367
- f.write(uploaded_file.getvalue())
368
- loader = CSVLoader('temp.csv')
369
- docs = loader.load()
370
- text_splitter = RecursiveCharacterTextSplitter(chunk_size = 1000, chunk_overlap = 200)
371
- splits = text_splitter.split_documents(docs)
372
-
373
- embeddings = OllamaEmbeddings(model='mistral')
374
- st.session_state.vectorstore = Chroma.from_documents(documents=splits,embedding=embeddings)
375
- st.session_state['isran'] = True
376
-
377
- if st.session_state['isran'] == True:
378
- st.write("Embedding created")
379
-
380
- def fdocs(docs):
381
- return "\n\n".join(doc.page_content for doc in docs)
382
-
383
- def llm(question,context):
384
- formatted_prompt = f"Question: {question}\n\nContext:{context}"
385
- response = ollama.chat(model='mistral', messages=[
386
- {
387
- 'role': 'user',
388
- 'content': formatted_prompt
389
- },
390
- ])
391
- return response['message']['content']
392
-
393
-
394
-
395
- def rag_chain(question):
396
- retriever = st.session_state.vectorstore.as_retriever()
397
- retrieved_docs = retriever.invoke(question)
398
- formatted_context = fdocs(retrieved_docs)
399
- return llm(question,formatted_context)
400
-
401
- if 'messages' not in st.session_state:
402
- st.session_state.messages = []
403
-
404
- for message in st.session_state.messages:
405
- st.chat_message(message['role']).markdown(message['content'])
406
-
407
- prompt = st.chat_input("Say something")
408
- response = rag_chain(prompt)
409
- if prompt:
410
- st.chat_message('user').markdown(prompt)
411
- st.session_state.messages.append({'role':'user','content':prompt})
412
- st.session_state.messages.append({'role':'AI','content':response})
413
- st.chat_message('AI').markdown(response)
 
296
  elif page == "PS2":
297
  st.header("CLICK ON RUN SCRIPT TO START A TRAFFIC SIMULATION")
298
  script = st.button("RUN SCRIPT")
299
+ st.alert("This simulation, Unfortunately was not optimized for deployed but still works for locally run version")
300
+ st.write("check out this [link](https://github.com/pranavsrinivasa/Traffic-Optimization-Projects)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
 
302
  elif page == "Chat with Results":
303
  st.title('Chat with the Results')
304
  st.write("Please upload the relevant CSV data to get started")
305
+
306
+ st.alert("This model, Unfortunately was not optimized for deployed but still works for locally run version")
307
+ st.write("check out this [link](https://github.com/pranavsrinivasa/Traffic-Optimization-Projects)")