speechmatics 0.3.1.0 → 0.4.0.0
raw patch · 3 files changed
+36/−21 lines, 3 filesdep +http-typesPVP ok
version bump matches the API change (PVP)
Dependencies added: http-types
API changes (from Hackage documentation)
+ Speechmatics.Client: JsonV1 :: Format
+ Speechmatics.Client: JsonV2 :: Format
+ Speechmatics.Client: data Format
- Speechmatics.Client: transcribe :: String -> UserID -> AuthToken -> Options -> [Part] -> IO (Either Error Value)
+ Speechmatics.Client: transcribe :: Format -> String -> UserID -> AuthToken -> Options -> [Part] -> IO (Either Error Value)
- Speechmatics.Client: transcribeBytes :: String -> UserID -> AuthToken -> ModelName -> LazyByteFile -> IO (Either Error Value)
+ Speechmatics.Client: transcribeBytes :: Format -> String -> UserID -> AuthToken -> ModelName -> LazyByteFile -> IO (Either Error Value)
- Speechmatics.Client: type AuthToken = Maybe String
+ Speechmatics.Client: type AuthToken = Maybe Text
Files
- app/Main.hs +4/−3
- speechmatics.cabal +4/−2
- src/Speechmatics/Client.hs +28/−16
app/Main.hs view
@@ -8,11 +8,12 @@ import Data.Digest.Pure.SHA import Speechmatics.Client import Network.Mime(MimeType)+import Data.Text(pack, Text) data MainOptions = MainOptions { user_id :: Maybe UserID,- token :: Maybe AuthToken,+ token :: Maybe String, targetFile :: Maybe FilePath } @@ -28,7 +29,7 @@ main :: IO () main = runCommand $ \opts args -> do- case withBearAndFile <$> user_id opts <*> token opts <*> targetFile opts of+ case withBearAndFile <$> user_id opts <*> (Just . pack <$> token opts) <*> targetFile opts of Nothing -> print "wrong options, see --help" Just compute -> compute @@ -39,6 +40,6 @@ let mime = (defaultMimeLookup "d.mp3") print mime print . sha256 $ bytes- result <- transcribeBytes "https://api.speechmatics.com/v1.0/user/" userId bearToken "en-US" (LazyByteFile bytes file mime)+ result <- transcribeBytes JsonV2 "http://192.168.38.91:8082/v1/user/" userId Nothing "en-AU" (LazyByteFile bytes file mime) putStrLn . show $ result
speechmatics.cabal view
@@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 824e37b823d1a54f6601ecae18db2ec21ab2646cf2e9e09a330bb53ae3fa49ba+-- hash: 5d7987afdc6f3b85ab67e58f53f104c67c8ba87caf64cbf122684e90d565ff7e name: speechmatics-version: 0.3.1.0+version: 0.4.0.0 synopsis: Speechmatics api client description: Upload audio files to speechmatics to get a transcription category: API@@ -32,6 +32,7 @@ , bytestring , http-client , http-client-openssl+ , http-types , json-autotype , lens , mime-types@@ -57,6 +58,7 @@ , mime-types , options , speechmatics+ , text other-modules: Paths_speechmatics default-language: Haskell2010
src/Speechmatics/Client.hs view
@@ -8,9 +8,11 @@ UserID, ModelName, LazyByteFile(..),- Error(..)+ Error(..),+ Format(..) ) where +import Network.HTTP.Types.URI import Control.Concurrent (threadDelay) import OpenSSL.Session (context) import Control.Lens@@ -26,7 +28,7 @@ import Network.HTTP.Client.OpenSSL import Data.Bifunctor -import qualified Data.ByteString.Char8 as BS+import qualified Data.ByteString.Char8 as C8BS import qualified Data.ByteString.Lazy.Char8 as C8LBS import qualified Data.ByteString.Lazy as LBS import qualified Network.Wreq.Session as Sess@@ -34,9 +36,10 @@ import Data.Monoid import qualified Speechmatics.JSON.PostJob as Post import qualified Speechmatics.JSON.PeekJob as Peek+import Data.Maybe -- | Authorization token obtained from speechmatics-type AuthToken = Maybe String+type AuthToken = Maybe Text -- | UserID obtained from speechmatics type UserID = Integer -- | Which langauge model to use@@ -62,11 +65,13 @@ modelName :: Text modelName = pack "model" +data Format = JsonV1 | JsonV2+ -- | Transcribe a file that is already loaded in memory -- Does not catch exeptions-transcribeBytes :: String -> UserID -> AuthToken -> ModelName -> LazyByteFile -> IO(Either Error Value)-transcribeBytes url userID bearerToken model (LazyByteFile content filename mimetype) = do- transcribe url userID bearerToken defaults parts+transcribeBytes :: Format -> String -> UserID -> AuthToken -> ModelName -> LazyByteFile -> IO(Either Error Value)+transcribeBytes format url userID bearerToken model (LazyByteFile content filename mimetype) = do+ transcribe format url userID bearerToken defaults parts where parts = [ partLBS inputName content @@ -80,23 +85,30 @@ & manager .~ Left (opensslManagerSettings context) & manager .~ Left (defaultManagerSettings { managerResponseTimeout = timeout} ) -tokenUri :: AuthToken -> String-tokenUri (Just auth) = "?auth_token=" <> auth-tokenUri Nothing = ""+formatQuery :: Format -> QueryText+formatQuery JsonV1 = []+formatQuery JsonV2 = [("format", Just "json-v2")] +authQuery :: AuthToken -> QueryText+authQuery auth = maybeToList $ (\x -> (pack "auth_token", Just x)) <$> auth slash = mappend . (flip mappend "/") -- play around to much with ghci jobsUri :: String -> UserID -> String -> String jobsUri url userID x = url <> (show userID) `slash` "jobs" `slash` x +printQuery :: QueryText -> String+printQuery = C8BS.unpack . (renderQuery True) . queryTextToQuery + -- | More general transcription interface, allows custom parts to be inserted -- with whatever the user wants. -- Does not catch exeptions-transcribe :: String -> UserID -> AuthToken -> Options -> [Part] -> IO(Either Error Value)-transcribe uri userID token options parts = do+transcribe :: Format -> String -> UserID -> AuthToken -> Options -> [Part] -> IO(Either Error Value)+transcribe format uri userID token options parts = do session <- Sess.newSession+ print "begin" response <- Sess.postWith auth session postUri parts- -- print $ response ^. responseBody+ print $ response ^. responseBody+ print uri case Post.parse (response ^. responseBody) of Left message -> return $ Left $ ParseError message Right parsed -> do@@ -104,16 +116,16 @@ pollStatus uri userID token session jobID >>= \case Just error -> return $ Left $ error Nothing -> do - let transcriptUri = url userID ((show jobID) `slash` "transcript" <> (tokenUri token))+ let transcriptUri = (url userID ((show jobID) `slash` "transcript")) <> (printQuery $ formatQuery format <> authQuery token) result <- Sess.getWith makeOpts session transcriptUri return $ first ParseError $ eitherDecode (result ^. responseBody) where auth = (withAuthorization options)- postUri = url userID (tokenUri token) + postUri = url userID (printQuery $ authQuery token ) url = jobsUri uri pollStatus :: String -> UserID -> AuthToken -> Sess.Session -> JobID -> IO(Maybe Error)-pollStatus url userID token session jobID = pollStatus' Nothing url userID token session jobID+pollStatus = pollStatus' Nothing waitFor :: Int -> IO() waitFor x = threadDelay (1000000*x)@@ -122,7 +134,7 @@ pollStatus' (Just wait) url user auth sess jobid = waitFor (fromIntegral wait) >> (pollStatus' Nothing) url user auth sess jobid pollStatus' Nothing url userID token session jobID = do - let statusUri = (jobsUri url userID $ show $ jobID) <> (tokenUri token)+ let statusUri = (jobsUri url userID $ show $ jobID) <> (printQuery $ authQuery token) statusResponse <- Sess.getWith makeOpts session statusUri -- print statusResponse let body = statusResponse ^. responseBody