feat: support timer and storage
Browse files
app.py
CHANGED
|
@@ -6,11 +6,104 @@ import openai
|
|
| 6 |
import logging
|
| 7 |
from gtts import gTTS
|
| 8 |
import re
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Set up logging
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def text_to_speech(text, lang='en'):
|
| 15 |
"""
|
| 16 |
Converts text to speech using gTTS and returns the path to the audio file.
|
|
@@ -132,16 +225,21 @@ def get_ai_info(street_name):
|
|
| 132 |
{"role": "user", "content": f"Location information:{street_name}."}
|
| 133 |
]
|
| 134 |
try:
|
|
|
|
| 135 |
client = openai.OpenAI()
|
| 136 |
completion = client.chat.completions.create(
|
| 137 |
-
model="gpt-4o-search-preview",
|
| 138 |
web_search_options={
|
| 139 |
"search_context_size": "low",
|
| 140 |
},
|
| 141 |
messages=messages,
|
| 142 |
# max_tokens=150,
|
| 143 |
)
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
except Exception as e:
|
| 146 |
return f"Error retrieving street information: {e}"
|
| 147 |
|
|
@@ -152,11 +250,15 @@ def preprocess_text(input_text):
|
|
| 152 |
return text_without_urls
|
| 153 |
|
| 154 |
|
|
|
|
| 155 |
def process_location(coords):
|
| 156 |
"""
|
| 157 |
Processes the user's coordinates to retrieve the address, street information,
|
| 158 |
and nearby points of interest.
|
| 159 |
"""
|
|
|
|
|
|
|
|
|
|
| 160 |
try:
|
| 161 |
lat, lon = map(float, coords.split(","))
|
| 162 |
address_data = get_address(lat, lon)
|
|
@@ -169,14 +271,27 @@ def process_location(coords):
|
|
| 169 |
poi_descriptions = '; '.join(f"{poi['name']} ({poi['type']})" for poi in pois)
|
| 170 |
combined_info = f"{address} Nearby points of interest include: {poi_descriptions}."
|
| 171 |
logging.info(f"Retrieved address: {combined_info}")
|
| 172 |
-
|
|
|
|
|
|
|
| 173 |
ai_info = preprocess_text(ai_info)
|
| 174 |
-
audio_file = None #text_to_speech(ai_info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
return combined_info, ai_info, audio_file
|
| 176 |
except ValueError:
|
| 177 |
return "Invalid coordinates provided.", "Unable to retrieve street information.", None
|
| 178 |
|
| 179 |
|
|
|
|
| 180 |
# Gradio Interface
|
| 181 |
with gr.Blocks() as demo:
|
| 182 |
coords_box = gr.Textbox(label="Your Coordinates (Lat, Lon)", visible=False)
|
|
@@ -187,9 +302,42 @@ with gr.Blocks() as demo:
|
|
| 187 |
with gr.Row():
|
| 188 |
get_location_btn = gr.Button("📍 Get My Location and Street Info")
|
| 189 |
read_aloud_btn = gr.Button("🔊 Read Aloud")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
# JavaScript to fetch geolocation and populate coords_box
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
() => new Promise((resolve, reject) => {
|
| 194 |
if (navigator.geolocation) {
|
| 195 |
navigator.geolocation.getCurrentPosition(
|
|
@@ -208,7 +356,11 @@ with gr.Blocks() as demo:
|
|
| 208 |
reject();
|
| 209 |
}
|
| 210 |
})
|
| 211 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
# Update outputs when coordinates change
|
| 214 |
coords_box.change(
|
|
@@ -224,5 +376,16 @@ with gr.Blocks() as demo:
|
|
| 224 |
outputs=audio_output
|
| 225 |
)
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
# Launch the Gradio application
|
| 228 |
demo.launch()
|
|
|
|
| 6 |
import logging
|
| 7 |
from gtts import gTTS
|
| 8 |
import re
|
| 9 |
+
from shillelagh.backends.apsw.db import connect
|
| 10 |
+
|
| 11 |
|
| 12 |
# Set up logging
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
# Google Sheets API setup
|
| 17 |
+
credentials_json = os.getenv('GOOGLE_API_JSON_CONTENT')
|
| 18 |
+
# Load the credentials from the JSON string
|
| 19 |
+
credentials_info = json.loads(credentials_json)
|
| 20 |
+
# credentials = service_account.Credentials.from_service_account_info(credentials_info)
|
| 21 |
+
|
| 22 |
+
# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'location-455719-d08225f31b58.json'
|
| 23 |
+
|
| 24 |
+
# URL format: https://docs.google.com/spreadsheets/d/{spreadsheet_id}/edit
|
| 25 |
+
# https://docs.google.com/spreadsheets/d/1CMdSSTxjw9ooYDzmAufL0j3EtUxAQ6vdwbAL5e3t7aY/edit?usp=sharing
|
| 26 |
+
spreadsheet_id = '1CMdSSTxjw9ooYDzmAufL0j3EtUxAQ6vdwbAL5e3t7aY'
|
| 27 |
+
sheet_table = {
|
| 28 |
+
"Location": 0,
|
| 29 |
+
"Config": 1310222727
|
| 30 |
+
}
|
| 31 |
+
location_table = f'https://docs.google.com/spreadsheets/d/{spreadsheet_id}/edit?gid={sheet_table["Location"]}'
|
| 32 |
+
config_table = f'https://docs.google.com/spreadsheets/d/{spreadsheet_id}/edit?gid={sheet_table["Config"]}'
|
| 33 |
+
|
| 34 |
+
# Connect to the Google Sheet
|
| 35 |
+
connection = connect(
|
| 36 |
+
":memory:",
|
| 37 |
+
adapter_kwargs={
|
| 38 |
+
"gsheetsapi": {
|
| 39 |
+
# "service_account_file": 'location-455719-d08225f31b58.json'
|
| 40 |
+
"service_account_info": credentials_info,
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
)
|
| 44 |
+
cursor = connection.cursor()
|
| 45 |
+
|
| 46 |
+
# Get latest config
|
| 47 |
+
rows = cursor.execute(f"SELECT * FROM '{config_table}' LIMIT 1").fetchall()
|
| 48 |
+
config = rows[0][1]
|
| 49 |
+
config = json.loads(config)
|
| 50 |
+
config["last_run"] = config.get("last_run", 0) + 1
|
| 51 |
+
updated_config = json.dumps(config)
|
| 52 |
+
try:
|
| 53 |
+
cursor.execute(
|
| 54 |
+
f"UPDATE '{config_table}' SET config = ? WHERE id = ?",
|
| 55 |
+
(updated_config, 1)
|
| 56 |
+
)
|
| 57 |
+
except Exception as e:
|
| 58 |
+
logger.error(f"Error updating config: {e}")
|
| 59 |
+
sample = 0
|
| 60 |
+
def update_location(obj):
|
| 61 |
+
global cursor
|
| 62 |
+
global config
|
| 63 |
+
global location_table
|
| 64 |
+
global sample
|
| 65 |
+
sample += 1
|
| 66 |
+
try:
|
| 67 |
+
# Update the Google Sheet with the new location
|
| 68 |
+
cursor.execute(
|
| 69 |
+
f"INSERT INTO '{location_table}' (Run, Sample, LocationObj, Address, AI) VALUES (?, ?, ?, ?, ?)",
|
| 70 |
+
(config["last_run"], sample, json.dumps(obj), obj["address"], obj["ai_info"])
|
| 71 |
+
)
|
| 72 |
+
connection.commit()
|
| 73 |
+
logger.info(f"Updated location in Google Sheets: {obj}")
|
| 74 |
+
except Exception as e:
|
| 75 |
+
logger.error(f"Error updating location in Google Sheets: {e}")
|
| 76 |
+
|
| 77 |
+
# Global state for timer control
|
| 78 |
+
is_timer_active = False
|
| 79 |
+
current_interval = 10.0 # Default to 1 second
|
| 80 |
+
timer = None
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def toggle_timer(state):
|
| 85 |
+
global is_timer_active, timer
|
| 86 |
+
is_timer_active = state
|
| 87 |
+
if state:
|
| 88 |
+
# Start the timer
|
| 89 |
+
timer = gr.Timer(current_interval)
|
| 90 |
+
# return f"Timer {'activated' if state else 'deactivated'}."
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
counter = 0
|
| 94 |
+
def auto_trigger():
|
| 95 |
+
"""
|
| 96 |
+
Simulates pressing the '📍 Get My Location and Street Info' button.
|
| 97 |
+
This function is triggered by the timer.
|
| 98 |
+
"""
|
| 99 |
+
global counter
|
| 100 |
+
global is_timer_active
|
| 101 |
+
counter += 1
|
| 102 |
+
if is_timer_active:
|
| 103 |
+
return f"Auto-triggered: Getting location...{counter}"
|
| 104 |
+
return None
|
| 105 |
+
|
| 106 |
+
|
| 107 |
def text_to_speech(text, lang='en'):
|
| 108 |
"""
|
| 109 |
Converts text to speech using gTTS and returns the path to the audio file.
|
|
|
|
| 225 |
{"role": "user", "content": f"Location information:{street_name}."}
|
| 226 |
]
|
| 227 |
try:
|
| 228 |
+
# TODO: remove commentted code
|
| 229 |
client = openai.OpenAI()
|
| 230 |
completion = client.chat.completions.create(
|
| 231 |
+
model="gpt-4o-mini-search-preview",
|
| 232 |
web_search_options={
|
| 233 |
"search_context_size": "low",
|
| 234 |
},
|
| 235 |
messages=messages,
|
| 236 |
# max_tokens=150,
|
| 237 |
)
|
| 238 |
+
j = completion
|
| 239 |
+
return j
|
| 240 |
+
r = completion.choices[0].message.content.strip()
|
| 241 |
+
return r
|
| 242 |
+
# return "Debug text"
|
| 243 |
except Exception as e:
|
| 244 |
return f"Error retrieving street information: {e}"
|
| 245 |
|
|
|
|
| 250 |
return text_without_urls
|
| 251 |
|
| 252 |
|
| 253 |
+
|
| 254 |
def process_location(coords):
|
| 255 |
"""
|
| 256 |
Processes the user's coordinates to retrieve the address, street information,
|
| 257 |
and nearby points of interest.
|
| 258 |
"""
|
| 259 |
+
global is_timer_active
|
| 260 |
+
if not is_timer_active:
|
| 261 |
+
return "Timer is not active. Please enable the timer to get location updates.", "Unable to retrieve street information.", None
|
| 262 |
try:
|
| 263 |
lat, lon = map(float, coords.split(","))
|
| 264 |
address_data = get_address(lat, lon)
|
|
|
|
| 271 |
poi_descriptions = '; '.join(f"{poi['name']} ({poi['type']})" for poi in pois)
|
| 272 |
combined_info = f"{address} Nearby points of interest include: {poi_descriptions}."
|
| 273 |
logging.info(f"Retrieved address: {combined_info}")
|
| 274 |
+
ai_obj = get_ai_info(address)
|
| 275 |
+
ai_info = ai_obj.choices[0].message.content.strip()
|
| 276 |
+
ai_obj = ai_obj.to_json()
|
| 277 |
ai_info = preprocess_text(ai_info)
|
| 278 |
+
audio_file = None #TODO text_to_speech(ai_info)
|
| 279 |
+
location_obj = {
|
| 280 |
+
"lat": lat,
|
| 281 |
+
"lon": lon,
|
| 282 |
+
"address": address,
|
| 283 |
+
"combined_info": combined_info,
|
| 284 |
+
"ai_obj": ai_obj,
|
| 285 |
+
"ai_info": ai_info,
|
| 286 |
+
}
|
| 287 |
+
# Update the Google Sheet with the new location
|
| 288 |
+
update_location(location_obj)
|
| 289 |
return combined_info, ai_info, audio_file
|
| 290 |
except ValueError:
|
| 291 |
return "Invalid coordinates provided.", "Unable to retrieve street information.", None
|
| 292 |
|
| 293 |
|
| 294 |
+
|
| 295 |
# Gradio Interface
|
| 296 |
with gr.Blocks() as demo:
|
| 297 |
coords_box = gr.Textbox(label="Your Coordinates (Lat, Lon)", visible=False)
|
|
|
|
| 302 |
with gr.Row():
|
| 303 |
get_location_btn = gr.Button("📍 Get My Location and Street Info")
|
| 304 |
read_aloud_btn = gr.Button("🔊 Read Aloud")
|
| 305 |
+
toggle_button = gr.Checkbox(label="Enable Auto Trigger", value=False)
|
| 306 |
+
interval_selector = gr.Dropdown(
|
| 307 |
+
choices=["10 sec", "1 min", "2 min", "5 min"], value="10 sec", label="Set Timer Interval"
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
|
| 311 |
# JavaScript to fetch geolocation and populate coords_box
|
| 312 |
+
|
| 313 |
+
debug_geolocation_js = """
|
| 314 |
+
() => new Promise((resolve, reject) => {
|
| 315 |
+
try {
|
| 316 |
+
// Generate realistic coordinates within populated areas
|
| 317 |
+
const locations = [
|
| 318 |
+
{lat: 40.7128, lon: -74.0060}, // New York, USA
|
| 319 |
+
{lat: 48.8566, lon: 2.3522}, // Paris, France
|
| 320 |
+
{lat: 35.6762, lon: 139.6503}, // Tokyo, Japan
|
| 321 |
+
{lat: 51.5074, lon: -0.1278}, // London, UK
|
| 322 |
+
{lat: -33.8688, lon: 151.2093},// Sydney, Australia
|
| 323 |
+
{lat: 19.0760, lon: 72.8777}, // Mumbai, India
|
| 324 |
+
{lat: -23.5505, lon: -46.6333} // São Paulo, Brazil
|
| 325 |
+
];
|
| 326 |
+
|
| 327 |
+
// Select a random location
|
| 328 |
+
const randomLocation = locations[Math.floor(Math.random() * locations.length)];
|
| 329 |
+
const coords = `${randomLocation.lat.toFixed(6)},${randomLocation.lon.toFixed(6)}`;
|
| 330 |
+
|
| 331 |
+
console.log("Debug Geolocation fetched:", coords);
|
| 332 |
+
resolve(coords);
|
| 333 |
+
} catch (error) {
|
| 334 |
+
console.error("Error generating random geolocation: " + error.message);
|
| 335 |
+
reject();
|
| 336 |
+
}
|
| 337 |
+
})
|
| 338 |
+
"""
|
| 339 |
+
|
| 340 |
+
real_geolocation_js = """
|
| 341 |
() => new Promise((resolve, reject) => {
|
| 342 |
if (navigator.geolocation) {
|
| 343 |
navigator.geolocation.getCurrentPosition(
|
|
|
|
| 356 |
reject();
|
| 357 |
}
|
| 358 |
})
|
| 359 |
+
"""
|
| 360 |
+
|
| 361 |
+
geolocation_js = real_geolocation_js
|
| 362 |
+
|
| 363 |
+
get_location_btn.click(None, js=geolocation_js, outputs=coords_box)
|
| 364 |
|
| 365 |
# Update outputs when coordinates change
|
| 366 |
coords_box.change(
|
|
|
|
| 376 |
outputs=audio_output
|
| 377 |
)
|
| 378 |
|
| 379 |
+
# Toggle Timer On/Off
|
| 380 |
+
toggle_button.change(toggle_timer, inputs=toggle_button, outputs=None)
|
| 381 |
+
|
| 382 |
+
# Hidden textbox that gets updated by the Timer to trigger JS code
|
| 383 |
+
trigger_box = gr.Textbox(label="Trigger Box", visible=True)
|
| 384 |
+
# Timer component - triggers every `current_interval` seconds
|
| 385 |
+
timer = gr.Timer(config.get("interval", 300))
|
| 386 |
+
timer.tick(fn=auto_trigger, outputs=trigger_box)
|
| 387 |
+
trigger_box.change(fn=None, inputs = None, outputs = coords_box, js=geolocation_js)
|
| 388 |
+
|
| 389 |
+
|
| 390 |
# Launch the Gradio application
|
| 391 |
demo.launch()
|
sheet.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from shillelagh.backends.apsw.db import connect
|
| 2 |
+
from google.oauth2 import service_account
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
# Set up environment variable for authentication
|
| 6 |
+
# If using a service account, set the environment variable for authentication
|
| 7 |
+
|
| 8 |
+
credentials_json = os.getenv('GOOGLE_API_JSON_CONTENT')
|
| 9 |
+
# Load the credentials from the JSON string
|
| 10 |
+
credentials_info = json.loads(credentials_json)
|
| 11 |
+
# credentials = service_account.Credentials.from_service_account_info(credentials_info)
|
| 12 |
+
|
| 13 |
+
# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'location-455719-d08225f31b58.json'
|
| 14 |
+
|
| 15 |
+
# URL format: https://docs.google.com/spreadsheets/d/{spreadsheet_id}/edit
|
| 16 |
+
# https://docs.google.com/spreadsheets/d/1CMdSSTxjw9ooYDzmAufL0j3EtUxAQ6vdwbAL5e3t7aY/edit?usp=sharing
|
| 17 |
+
spreadsheet_id = '1CMdSSTxjw9ooYDzmAufL0j3EtUxAQ6vdwbAL5e3t7aY'
|
| 18 |
+
sheet_name = 'Config'
|
| 19 |
+
sheet_table = {
|
| 20 |
+
"Location": 0,
|
| 21 |
+
"Config": 1310222727
|
| 22 |
+
}
|
| 23 |
+
url = f"https://docs.google.com/spreadsheets/d/{spreadsheet_id}/edit?gid={sheet_table[sheet_name]}"
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Connect to the Google Sheet
|
| 27 |
+
connection = connect(
|
| 28 |
+
":memory:",
|
| 29 |
+
adapter_kwargs={
|
| 30 |
+
"gsheetsapi": {
|
| 31 |
+
# "service_account_file": 'location-455719-d08225f31b58.json'
|
| 32 |
+
"service_account_info": credentials_info,
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
)
|
| 36 |
+
cursor = connection.cursor()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Example query to read data
|
| 40 |
+
rows = cursor.execute(f"SELECT * FROM '{url}' LIMIT 1").fetchall()
|
| 41 |
+
# rowid = rows[0][0]
|
| 42 |
+
config = rows[0][1]
|
| 43 |
+
config = json.loads(config)
|
| 44 |
+
config["last_run"] = config.get("last_run", 0) + 1
|
| 45 |
+
updated_config = json.dumps(config)
|
| 46 |
+
print(rows)
|
| 47 |
+
# cursor.execute(f"DELETE FROM '{url}' ")
|
| 48 |
+
try:
|
| 49 |
+
cursor.execute(
|
| 50 |
+
f"UPDATE '{url}' SET config = ? WHERE id = ?",
|
| 51 |
+
(updated_config, 1)
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Error inserting data: {e}")
|
| 56 |
+
|