File size: 2,792 Bytes
8522fb6 605c908 8522fb6 5f8d324 22b1fb0 5f8d324 6edd378 5f8d324 8522fb6 5f8d324 8522fb6 22b1fb0 5f8d324 8522fb6 5f8d324 8522fb6 5f8d324 8522fb6 5f8d324 8522fb6 5f8d324 8522fb6 5f8d324 8522fb6 5f8d324 8522fb6 605c908 8522fb6 605c908 8522fb6 605c908 8522fb6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import os
from gtts import gTTS
import gradio as gr
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime
import requests
import traceback
# === CONFIG ===
WEATHER_API_KEY = "6e55658a10b85904fe0c3b589a54bf7f" # Replace with a valid OpenWeatherMap API key
SHEET_CREDS_FILE = "google_sheet_credentials.json" # Make sure this file exists in your directory
SHEET_NAME = "LocationLogs"
# === Setup Google Sheets ===
def setup_sheets():
try:
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name(SHEET_CREDS_FILE, scope)
client = gspread.authorize(creds)
sheet = client.open(SHEET_NAME).sheet1
return sheet
except Exception as e:
raise Exception(f"Google Sheets setup failed: {str(e)}")
# === Speak using gTTS ===
def speak(text, filename="voice.mp3"):
try:
tts = gTTS(text)
tts.save(filename)
return filename
except Exception as e:
raise Exception(f"Text-to-speech failed: {str(e)}")
# === Get Weather Info ===
def get_weather(city):
try:
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={WEATHER_API_KEY}&units=metric"
res = requests.get(url)
data = res.json()
desc = data["weather"][0]["description"]
temp = data["main"]["temp"]
return f"{desc.capitalize()}, {temp}°C"
except Exception as e:
return "Weather info unavailable"
# === Main Logic ===
def log_and_notify(place, city, note):
try:
sheet = setup_sheets()
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
weather = get_weather(city)
# Log to Google Sheet
sheet.append_row([now, place, city, note, weather])
reminder_text = f"Reminder! You are near {place}. Note: {note}. Weather is {weather}."
voice_file = speak(reminder_text)
return reminder_text, voice_file
except Exception as e:
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
return error_msg, None
# === Gradio Interface ===
interface = gr.Interface(
fn=log_and_notify,
inputs=[
gr.Textbox(label="Place Name", placeholder="eg. Starbucks, MG Road"),
gr.Textbox(label="City", placeholder="eg. Bengaluru"),
gr.Textbox(label="Reminder Note", placeholder="eg. Buy coffee here"),
],
outputs=[
gr.Textbox(label="Reminder Text"),
gr.Audio(type="filepath", label="Voice Reminder")
],
title="📍 Location Reminder App",
description="⏰ Save a place and note — get a voice reminder + weather info when near the location!"
)
if __name__ == "__main__":
interface.launch()
|