Skip to content

Commit

Permalink
Fix timer and weather for whisper.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kercre123 committed Apr 9, 2024
1 parent 8ef7c45 commit 45b620e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
18 changes: 17 additions & 1 deletion chipper/pkg/wirepod/ttr/intentparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func ParamChecker(req interface{}, intent string, speechText string, botSerial s
var botUnits string = "F"
var botPlaySpecific bool = false
var botIsEarlyOpus bool = false
var DoWeatherError bool = false

// see if jdoc exists
botJdoc, jdocExists := vars.GetJdoc("vic:"+botSerial, "vic.RobotSettings")
Expand Down Expand Up @@ -139,7 +140,13 @@ func ParamChecker(req interface{}, intent string, speechText string, botSerial s
isParam = true
newIntent = intent
condition, is_forecast, local_datetime, speakable_location_string, temperature, temperature_unit := weatherParser(speechText, botLocation, botUnits)
intentParams = map[string]string{"condition": condition, "is_forecast": is_forecast, "local_datetime": local_datetime, "speakable_location_string": speakable_location_string, "temperature": temperature, "temperature_unit": temperature_unit}
if local_datetime == "test" {
newIntent = "intent_system_unmatched"
isParam = false
DoWeatherError = true
} else {
intentParams = map[string]string{"condition": condition, "is_forecast": is_forecast, "local_datetime": local_datetime, "speakable_location_string": speakable_location_string, "temperature": temperature, "temperature_unit": temperature_unit}
}
} else if strings.Contains(intent, "intent_imperative_volumelevel_extend") {
isParam = true
newIntent = intent
Expand Down Expand Up @@ -307,6 +314,15 @@ func ParamChecker(req interface{}, intent string, speechText string, botSerial s
}
}
IntentPass(req, newIntent, speechText, intentParams, isParam)
if DoWeatherError {
if vars.APIConfig.Weather.Enable {
logger.Println("The weather API is not configured properly.")
KGSim(botSerial, "The weather API is not configured properly. Please check the wire pod logs for more details.")
} else {
logger.Println("The weather API is not configured.")
KGSim(botSerial, "The weather API is not configured.")
}
}
}

// stintent
Expand Down
44 changes: 31 additions & 13 deletions chipper/pkg/wirepod/ttr/weather.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

"github.com/kercre123/wire-pod/chipper/pkg/logger"
"github.com/kercre123/wire-pod/chipper/pkg/vars"
Expand Down Expand Up @@ -93,6 +94,21 @@ type openWeatherMapAPIResponseStruct struct {

//2.5 API

func removeEndPunctuation(s string) string {
if s == "" {
return s
}

runes := []rune(s)
lastIndex := len(runes) - 1

if unicode.IsPunct(runes[lastIndex]) {
return string(runes[:lastIndex])
}

return s
}

type WeatherStruct struct {
Id int `json:"id"`
Main string `json:"main"`
Expand Down Expand Up @@ -362,20 +378,22 @@ func weatherParser(speechText string, botLocation string, botUnits string) (stri
var speechLocation string
var hoursFromNow int
if strings.Contains(speechText, lcztn.GetText(lcztn.STR_WEATHER_IN)) {
splitPhrase := strings.SplitAfter(speechText, lcztn.GetText(lcztn.STR_WEATHER_IN))
splitPhrase := strings.SplitAfter(removeEndPunctuation(speechText), lcztn.GetText(lcztn.STR_WEATHER_IN))
speechLocation = strings.TrimSpace(splitPhrase[1])
if len(splitPhrase) == 3 {
speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2])
} else if len(splitPhrase) == 4 {
speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2]) + " " + strings.TrimSpace(splitPhrase[3])
} else if len(splitPhrase) > 4 {
speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2]) + " " + strings.TrimSpace(splitPhrase[3])
}
splitLocation := strings.Split(speechLocation, " ")
if len(splitLocation) == 2 {
speechLocation = splitLocation[0] + ", " + splitLocation[1]
} else if len(splitLocation) == 3 {
speechLocation = splitLocation[0] + " " + splitLocation[1] + ", " + splitLocation[2]
if os.Getenv("STT_SERVICE") != "whisper.cpp" {
if len(splitPhrase) == 3 {
speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2])
} else if len(splitPhrase) == 4 {
speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2]) + " " + strings.TrimSpace(splitPhrase[3])
} else if len(splitPhrase) > 4 {
speechLocation = speechLocation + " " + strings.TrimSpace(splitPhrase[2]) + " " + strings.TrimSpace(splitPhrase[3])
}
splitLocation := strings.Split(speechLocation, " ")
if len(splitLocation) == 2 {
speechLocation = splitLocation[0] + ", " + splitLocation[1]
} else if len(splitLocation) == 3 {
speechLocation = splitLocation[0] + " " + splitLocation[1] + ", " + splitLocation[2]
}
}
logger.Println("Location parsed from speech: " + "`" + speechLocation + "`")
specificLocation = true
Expand Down
32 changes: 32 additions & 0 deletions chipper/pkg/wirepod/ttr/words2num.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package wirepod_ttr

import (
"os"
"regexp"
"strconv"
"strings"
)
Expand Down Expand Up @@ -32,7 +34,37 @@ func basicspeechText2num(speechText string) int {
return 0
}

func whisperSpeechtoNum(input string) string {
// whisper returns actual numbers in its response
// ex. "set a timer for 10 minutes and 11 seconds"
totalSeconds := 0

minutePattern := regexp.MustCompile(`(\d+)\s*minute`)
secondPattern := regexp.MustCompile(`(\d+)\s*second`)

minutesMatches := minutePattern.FindStringSubmatch(input)
secondsMatches := secondPattern.FindStringSubmatch(input)

if len(minutesMatches) > 1 {
minutes, err := strconv.Atoi(minutesMatches[1])
if err == nil {
totalSeconds += minutes * 60
}
}
if len(secondsMatches) > 1 {
seconds, err := strconv.Atoi(secondsMatches[1])
if err == nil {
totalSeconds += seconds
}
}

return strconv.Itoa(totalSeconds)
}

func words2num(speechText string) string {
if os.Getenv("STT_SERVICE") == "whisper.cpp" {
return whisperSpeechtoNum(speechText)
}
number = basicspeechText2num(speechText)
if number == 0 {
number = 1
Expand Down

0 comments on commit 45b620e

Please sign in to comment.