Abbas0786 commited on
Commit
ecd81b3
·
verified ·
1 Parent(s): 75ae17a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -42
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import streamlit as st
2
  import requests
3
  import time
@@ -7,13 +8,12 @@ import pandas as pd
7
  from streamlit.components.v1 import html
8
  from groq import Groq
9
 
10
- # Initialize Groq client with error handling
11
- try:
12
- client = Groq(api_key="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")
13
- st.write("Groq client initialized successfully.")
14
- except Exception as e:
15
- st.error(f"Failed to initialize Groq client: {e}")
16
- client = None # If the client fails to initialize, set it to None
17
 
18
  # Set up the title and description for the Streamlit app
19
  st.set_page_config(page_title="Gaia: Women Safety App", page_icon="🤖", layout="centered")
@@ -26,19 +26,18 @@ def get_response(user_input):
26
 
27
  st.session_state['messages'].append({"role": "user", "content": user_input})
28
 
29
- if client:
30
  # Call Groq API to get the AI's response
31
  chat_completion = client.chat.completions.create(
32
  messages=st.session_state['messages'],
33
  model="llama3-8b-8192" # Specify model you want to use from Groq
34
  )
35
-
36
  ai_message = chat_completion.choices[0].message.content
37
  st.session_state['messages'].append({"role": "assistant", "content": ai_message})
38
-
39
  return ai_message
40
- else:
41
- return "Error: Groq client not initialized."
 
42
 
43
  # Sidebar for navigation
44
  st.sidebar.title('Features')
@@ -78,7 +77,8 @@ elif page == "AI-Powered Support":
78
 
79
  if user_input:
80
  ai_response = get_response(user_input)
81
- st.markdown(f"**Gaia (AI):** {ai_response}")
 
82
 
83
  # Emergency Call Page
84
  elif page == "Emergency Call":
@@ -151,30 +151,37 @@ elif page == "ORS Route":
151
 
152
  if st.button("Calculate Route"):
153
  if start_lat and start_lon and end_lat and end_lon:
154
- # OpenRouteService API key
155
- api_key = '5b3ce3597851110001cf6248678e77a7fc474afbbb5ec203d721079c'
156
  start_point = f'{start_lon},{start_lat}' # ORS expects lon, lat
157
  end_point = f'{end_lon},{end_lat}'
158
-
159
- # API request to OpenRouteService
160
- url = f'https://api.openrouteservice.org/v2/directions/driving-car?api_key={api_key}&start={start_point}&end={end_point}'
161
- response = requests.get(url)
162
-
163
- if response.status_code == 200:
164
- data = response.json()
165
- # Extract the route information
166
- route = data['features'][0]['geometry']['coordinates']
167
- route_map = folium.Map(location=[start_lat, start_lon], zoom_start=12)
168
-
169
- # Plot the route on the map
170
- folium.PolyLine(locations=[(lat, lon) for lon, lat in route], color='blue', weight=5).add_to(route_map)
171
-
172
- # Display the route map in the app
173
- st.subheader("Calculated Route")
174
- route_html = route_map._repr_html_()
175
- html(route_html, height=500)
176
- else:
177
- st.error(f"Error: {response.status_code}")
 
 
 
 
 
 
 
178
  else:
179
  st.error("Please enter valid coordinates for both start and end locations.")
180
 
@@ -190,10 +197,4 @@ st.markdown("""
190
  }
191
  .css-15zrgwt {
192
  font-size: 1.1rem;
193
- line-height: 1.5;
194
- }
195
- .css-10hldgk {
196
- font-size: 1rem;
197
- }
198
- </style>
199
- """, unsafe_allow_html=True)
 
1
+ import os
2
  import streamlit as st
3
  import requests
4
  import time
 
8
  from streamlit.components.v1 import html
9
  from groq import Groq
10
 
11
+ # Use environment variables to store sensitive data
12
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY", "default_value_if_key_missing")
13
+ ORS_API_KEY = os.getenv("ORS_API_KEY", "default_value_if_key_missing")
14
+
15
+ # Initialize Groq client with the API key from the environment variable
16
+ client = Groq(api_key=GROQ_API_KEY)
 
17
 
18
  # Set up the title and description for the Streamlit app
19
  st.set_page_config(page_title="Gaia: Women Safety App", page_icon="🤖", layout="centered")
 
26
 
27
  st.session_state['messages'].append({"role": "user", "content": user_input})
28
 
29
+ try:
30
  # Call Groq API to get the AI's response
31
  chat_completion = client.chat.completions.create(
32
  messages=st.session_state['messages'],
33
  model="llama3-8b-8192" # Specify model you want to use from Groq
34
  )
 
35
  ai_message = chat_completion.choices[0].message.content
36
  st.session_state['messages'].append({"role": "assistant", "content": ai_message})
 
37
  return ai_message
38
+ except Exception as e:
39
+ st.error(f"Failed to get AI response: {e}")
40
+ return None
41
 
42
  # Sidebar for navigation
43
  st.sidebar.title('Features')
 
77
 
78
  if user_input:
79
  ai_response = get_response(user_input)
80
+ if ai_response:
81
+ st.markdown(f"**Gaia (AI):** {ai_response}")
82
 
83
  # Emergency Call Page
84
  elif page == "Emergency Call":
 
151
 
152
  if st.button("Calculate Route"):
153
  if start_lat and start_lon and end_lat and end_lon:
154
+ # OpenRouteService API key from environment variable
155
+ api_key = ORS_API_KEY
156
  start_point = f'{start_lon},{start_lat}' # ORS expects lon, lat
157
  end_point = f'{end_lon},{end_lat}'
158
+
159
+ try:
160
+ # API request to OpenRouteService
161
+ url = f'https://api.openrouteservice.org/v2/directions/driving-car?api_key={api_key}&start={start_point}&end={end_point}'
162
+ response = requests.get(url)
163
+ response.raise_for_status() # Check for HTTP errors
164
+
165
+ # Check if response is successful
166
+ if response.status_code == 200:
167
+ data = response.json()
168
+ # Extract the route information
169
+ route = data['features'][0]['geometry']['coordinates']
170
+ route_map = folium.Map(location=[start_lat, start_lon], zoom_start=12)
171
+
172
+ # Plot the route on the map
173
+ folium.PolyLine(locations=[(lat, lon) for lon, lat in route], color='blue', weight=5).add_to(route_map)
174
+
175
+ # Display the route map in the app
176
+ st.subheader("Calculated Route")
177
+ route_html = route_map._repr_html_()
178
+ html(route_html, height=500)
179
+ else:
180
+ st.error(f"Error: {response.status_code}")
181
+ except requests.exceptions.HTTPError as err:
182
+ st.error(f"HTTP error occurred: {err}")
183
+ except Exception as err:
184
+ st.error(f"An error occurred: {err}")
185
  else:
186
  st.error("Please enter valid coordinates for both start and end locations.")
187
 
 
197
  }
198
  .css-15zrgwt {
199
  font-size: 1.1rem;
200
+ line-height: 1.