Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -1,25 +1,9 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
from gtts import gTTS
|
3 |
-
import speech_recognition as sr
|
4 |
import io
|
5 |
import requests
|
6 |
-
import json
|
7 |
-
import os
|
8 |
from datetime import datetime
|
9 |
import streamlit as st
|
10 |
|
11 |
-
# Initialize AI models
|
12 |
-
@st.cache_resource
|
13 |
-
def load_ai_models():
|
14 |
-
try:
|
15 |
-
return {
|
16 |
-
"text_gen": pipeline("text-generation", model="gpt2"),
|
17 |
-
"sentiment": pipeline("sentiment-analysis"),
|
18 |
-
"summarization": pipeline("summarization", model="facebook/bart-large-cnn")
|
19 |
-
}
|
20 |
-
except Exception:
|
21 |
-
return None
|
22 |
-
|
23 |
# Text-to-speech
|
24 |
def text_to_speech(text, lang="en"):
|
25 |
try:
|
@@ -31,24 +15,14 @@ def text_to_speech(text, lang="en"):
|
|
31 |
except Exception:
|
32 |
return None
|
33 |
|
34 |
-
# Speech-to-text
|
35 |
-
def speech_to_text(audio_path):
|
36 |
-
try:
|
37 |
-
r = sr.Recognizer()
|
38 |
-
with sr.AudioFile(audio_path) as source:
|
39 |
-
audio = r.record(source)
|
40 |
-
return r.recognize_google(audio)
|
41 |
-
except Exception:
|
42 |
-
return "Could not understand audio"
|
43 |
-
|
44 |
# Weather API
|
45 |
def get_weather_forecast(date):
|
46 |
try:
|
47 |
date_str = date.strftime("%Y-%m-%d")
|
48 |
latitude = 40.7128
|
49 |
longitude = -74.0060
|
50 |
-
url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&daily=
|
51 |
-
response = requests.get(url, timeout=
|
52 |
data = response.json()
|
53 |
|
54 |
if "daily" in data:
|
@@ -63,17 +37,9 @@ def get_weather_forecast(date):
|
|
63 |
# AI analysis
|
64 |
def generate_ai_response(prompt):
|
65 |
try:
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
# Generate insights
|
71 |
-
insights = models["text_gen"](
|
72 |
-
f"Analyze this reminder: {prompt}",
|
73 |
-
max_length=150,
|
74 |
-
num_return_sequences=1
|
75 |
-
)[0]['generated_text']
|
76 |
-
|
77 |
-
return f"**AI Insights**:\n{insights[:500]}..."
|
78 |
except Exception:
|
79 |
return "AI insights unavailable"
|
|
|
|
|
1 |
from gtts import gTTS
|
|
|
2 |
import io
|
3 |
import requests
|
|
|
|
|
4 |
from datetime import datetime
|
5 |
import streamlit as st
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Text-to-speech
|
8 |
def text_to_speech(text, lang="en"):
|
9 |
try:
|
|
|
15 |
except Exception:
|
16 |
return None
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Weather API
|
19 |
def get_weather_forecast(date):
|
20 |
try:
|
21 |
date_str = date.strftime("%Y-%m-%d")
|
22 |
latitude = 40.7128
|
23 |
longitude = -74.0060
|
24 |
+
url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&daily=temperature_2m_max,temperature_2m_min&timezone=auto&start_date={date_str}&end_date={date_str}"
|
25 |
+
response = requests.get(url, timeout=3)
|
26 |
data = response.json()
|
27 |
|
28 |
if "daily" in data:
|
|
|
37 |
# AI analysis
|
38 |
def generate_ai_response(prompt):
|
39 |
try:
|
40 |
+
# Simplified AI response
|
41 |
+
return (f"**AI Insights for your reminder**:\n\n"
|
42 |
+
f"Based on your reminder about '{prompt.split('.')[0]}', "
|
43 |
+
f"consider preparing in advance. Set multiple alerts if it's important.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
except Exception:
|
45 |
return "AI insights unavailable"
|