Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import datetime
|
| 3 |
-
import pyttsx3
|
| 4 |
-
import requests
|
| 5 |
import gspread
|
| 6 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 7 |
-
from
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# 🔑 Config
|
| 14 |
-
from config import WEATHER_API_KEY, SHEET_CREDS_FILE
|
| 15 |
-
|
| 16 |
-
# 🔄 Location to trigger reminder (example: a store)
|
| 17 |
-
TARGET_LOCATION = (28.6139, 77.2090) # Example: Connaught Place, Delhi
|
| 18 |
-
TRIGGER_RADIUS_METERS = 500
|
| 19 |
-
|
| 20 |
-
# ✅ Init TTS engine
|
| 21 |
-
tts = pyttsx3.init()
|
| 22 |
-
|
| 23 |
-
# ✅ Google Sheets Setup
|
| 24 |
-
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
| 25 |
-
creds = ServiceAccountCredentials.from_json_keyfile_name(SHEET_CREDS_FILE, scope)
|
| 26 |
-
client = gspread.authorize(creds)
|
| 27 |
-
sheet = client.open("LocationLogs").sheet1
|
| 28 |
-
|
| 29 |
-
# 🌦️ Get weather info
|
| 30 |
-
def get_weather(lat, lon):
|
| 31 |
-
url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={WEATHER_API_KEY}&units=metric"
|
| 32 |
-
resp = requests.get(url).json()
|
| 33 |
-
weather = resp.get("weather", [{}])[0].get("description", "N/A")
|
| 34 |
-
temp = resp.get("main", {}).get("temp", "N/A")
|
| 35 |
-
return f"{weather.capitalize()}, {temp}°C"
|
| 36 |
-
|
| 37 |
-
# 📍 Main reminder logic
|
| 38 |
-
def reminder_app(user_lat, user_lon, task_text):
|
| 39 |
-
now = datetime.datetime.now(pytz.timezone("Asia/Kolkata"))
|
| 40 |
-
time_str = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 41 |
-
|
| 42 |
-
user_location = (user_lat, user_lon)
|
| 43 |
-
dist = geodesic(user_location, TARGET_LOCATION).meters
|
| 44 |
-
|
| 45 |
-
result_msg = f"📍 Your Distance: {int(dist)}m\n⏰ Time: {time_str}\n"
|
| 46 |
-
|
| 47 |
-
if dist <= TRIGGER_RADIUS_METERS:
|
| 48 |
-
result_msg += f"✅ You're near the location!\n"
|
| 49 |
-
|
| 50 |
-
# Weather alert
|
| 51 |
-
weather = get_weather(user_lat, user_lon)
|
| 52 |
-
result_msg += f"🌦️ Weather: {weather}\n"
|
| 53 |
-
|
| 54 |
-
# Voice reminder
|
| 55 |
-
tts.say(f"Reminder: {task_text}")
|
| 56 |
-
tts.runAndWait()
|
| 57 |
-
|
| 58 |
-
# Google Sheets logging
|
| 59 |
-
sheet.append_row([time_str, user_lat, user_lon, task_text, weather, "Triggered"])
|
| 60 |
-
else:
|
| 61 |
-
result_msg += "🚫 Not close enough to trigger the reminder."
|
| 62 |
-
|
| 63 |
-
sheet.append_row([time_str, user_lat, user_lon, task_text, "N/A", "Skipped"])
|
| 64 |
-
|
| 65 |
-
return result_msg
|
| 66 |
-
# Old:
|
| 67 |
-
# import pyttsx3
|
| 68 |
-
# tts = pyttsx3.init()
|
| 69 |
-
# tts.say("Hello")
|
| 70 |
-
# tts.runAndWait()
|
| 71 |
-
|
| 72 |
-
# New:
|
| 73 |
-
from gtts import gTTS
|
| 74 |
-
import os
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
def speak(text, filename="voice.mp3"):
|
| 77 |
-
tts = gTTS(text
|
| 78 |
tts.save(filename)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
inputs=[
|
| 85 |
-
gr.
|
| 86 |
-
gr.
|
| 87 |
-
gr.Textbox(label="Reminder
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
],
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
description="Enter your current GPS coords to check if you're near the target location. Voice + weather alert + logging enabled.",
|
| 92 |
-
live=False
|
| 93 |
)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import time
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 5 |
import gspread
|
| 6 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# === CONFIG SECTION ===
|
| 11 |
+
WEATHER_API_KEY = "your_openweather_api_key" # Replace with your API key
|
| 12 |
+
SHEET_CREDS_FILE = "google_sheet_credentials.json"
|
| 13 |
+
SHEET_NAME = "LocationLogs"
|
| 14 |
+
|
| 15 |
+
# === Setup Google Sheets ===
|
| 16 |
+
def setup_sheets():
|
| 17 |
+
scope = ["https://spreadsheets.google.com/feeds",
|
| 18 |
+
"https://www.googleapis.com/auth/drive"]
|
| 19 |
+
creds = ServiceAccountCredentials.from_json_keyfile_name(SHEET_CREDS_FILE, scope)
|
| 20 |
+
client = gspread.authorize(creds)
|
| 21 |
+
sheet = client.open(SHEET_NAME).sheet1
|
| 22 |
+
return sheet
|
| 23 |
+
|
| 24 |
+
# === Speak using gTTS ===
|
| 25 |
def speak(text, filename="voice.mp3"):
|
| 26 |
+
tts = gTTS(text)
|
| 27 |
tts.save(filename)
|
| 28 |
+
return filename # Will be used in Gradio audio component
|
| 29 |
+
|
| 30 |
+
# === Weather Helper ===
|
| 31 |
+
def get_weather(city):
|
| 32 |
+
try:
|
| 33 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={WEATHER_API_KEY}&units=metric"
|
| 34 |
+
res = requests.get(url)
|
| 35 |
+
data = res.json()
|
| 36 |
+
desc = data["weather"][0]["description"]
|
| 37 |
+
temp = data["main"]["temp"]
|
| 38 |
+
return f"{desc.capitalize()}, {temp}°C"
|
| 39 |
+
except:
|
| 40 |
+
return "Weather unavailable"
|
| 41 |
+
|
| 42 |
+
# === Log Location and Trigger Reminder ===
|
| 43 |
+
def log_and_notify(place, city, note):
|
| 44 |
+
sheet = setup_sheets()
|
| 45 |
+
|
| 46 |
+
# Log to Google Sheets
|
| 47 |
+
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 48 |
+
weather = get_weather(city)
|
| 49 |
+
sheet.append_row([now, place, city, note, weather])
|
| 50 |
+
|
| 51 |
+
reminder_text = f"Reminder! You are near {place}. Note: {note}. Weather is {weather}."
|
| 52 |
+
voice_file = speak(reminder_text)
|
| 53 |
+
return reminder_text, voice_file
|
| 54 |
+
|
| 55 |
+
# === Gradio Interface ===
|
| 56 |
+
interface = gr.Interface(
|
| 57 |
+
fn=log_and_notify,
|
| 58 |
inputs=[
|
| 59 |
+
gr.Textbox(label="Place Name", placeholder="eg. Starbucks, MG Road"),
|
| 60 |
+
gr.Textbox(label="City", placeholder="eg. Bengaluru"),
|
| 61 |
+
gr.Textbox(label="Reminder Note", placeholder="eg. Buy coffee here"),
|
| 62 |
+
],
|
| 63 |
+
outputs=[
|
| 64 |
+
gr.Textbox(label="Reminder Text"),
|
| 65 |
+
gr.Audio(type="filepath", label="Voice Reminder")
|
| 66 |
],
|
| 67 |
+
title="📍 Location Reminder App",
|
| 68 |
+
description="⏰ Save a place and note — get a voice reminder + weather info when near the location!"
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
interface.launch()
|