Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -2,52 +2,78 @@ from transformers import pipeline
|
|
| 2 |
from gtts import gTTS
|
| 3 |
import speech_recognition as sr
|
| 4 |
import io
|
| 5 |
-
import
|
| 6 |
-
import
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Initialize AI models
|
| 9 |
@st.cache_resource
|
| 10 |
def load_ai_models():
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Text-to-speech
|
| 18 |
def text_to_speech(text, lang="en"):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Speech-to-text
|
| 25 |
def speech_to_text(audio_path):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# AI analysis
|
| 32 |
def generate_ai_response(prompt):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
insights
|
| 48 |
-
max_length=80,
|
| 49 |
-
min_length=30,
|
| 50 |
-
do_sample=False
|
| 51 |
-
)[0]['summary_text']
|
| 52 |
-
|
| 53 |
-
return f"**AI Insights**: {summary}\n\n**Sentiment**: {sentiment['label']} ({sentiment['score']:.2f})"
|
|
|
|
| 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:
|
| 26 |
+
tts = gTTS(text=text, lang=lang)
|
| 27 |
+
audio_bytes = io.BytesIO()
|
| 28 |
+
tts.write_to_fp(audio_bytes)
|
| 29 |
+
audio_bytes.seek(0)
|
| 30 |
+
return audio_bytes
|
| 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=weathercode,temperature_2m_max,temperature_2m_min&timezone=auto&start_date={date_str}&end_date={date_str}"
|
| 51 |
+
response = requests.get(url, timeout=5)
|
| 52 |
+
data = response.json()
|
| 53 |
+
|
| 54 |
+
if "daily" in data:
|
| 55 |
+
temp_max = data["daily"]["temperature_2m_max"][0]
|
| 56 |
+
temp_min = data["daily"]["temperature_2m_min"][0]
|
| 57 |
+
return f"High: {temp_max}°C, Low: {temp_min}°C"
|
| 58 |
+
|
| 59 |
+
return "Weather data unavailable"
|
| 60 |
+
except Exception:
|
| 61 |
+
return "Weather service unavailable"
|
| 62 |
|
| 63 |
# AI analysis
|
| 64 |
def generate_ai_response(prompt):
|
| 65 |
+
try:
|
| 66 |
+
models = load_ai_models()
|
| 67 |
+
if not models:
|
| 68 |
+
return "AI service unavailable"
|
| 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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|