krishnasivaborra's picture
Update app.py
6edd378 verified
Raw
History Blame Contribute Delete
2.79 kB
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()