Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function for deepgramTranscribeAudio() in Swift. #179

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Proof of Concept(MVP)
  • Loading branch information
darkcheftar committed Aug 30, 2023
commit 7acfcb20dce0c0ddf68642fcc14aeea5a42371e4
40 changes: 40 additions & 0 deletions swift/deepgram-transcribe-audio/Index.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import AsyncHTTPClient
import Foundation

func main(context: RuntimeContext) async throws -> RuntimeOutput{

//Settingup necessary variables
let bodyString = context.req.bodyRaw
let bodyDict = try JSONSerialization.jsonObject(with: bodyString.data(using: .utf8)! ,options: []) as! [String:Any]
let variables = bodyDict["variables"] as! [String:Any]
let payload = bodyDict["payload"] as! [String:Any]
let fileUrl = payload["fileUrl"] as! String
let apikey = variables["DEEPGRAM_API_KEY"] as! String
let jsonString = "{\"url\":\"\(fileUrl)\"}"

let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
var request = HTTPClientRequest(url: "https://api.deepgram.com/v1/listen")
request.method = .POST
request.headers.add(name: "Authorization", value: "Token \(apikey)")
request.headers.add(name: "Content-Type", value: "application/json")

request.body = .bytes([UInt8](jsonString.utf8))

let response = try await httpClient.execute(request, timeout: .seconds(30))
var body = try await response.body.collect(upTo: 1024*1024) // 1MB
let string = body.readString(length: body.readableBytes)!
let json = string.data(using: .utf8)!
let jsonDict = try JSONSerialization.jsonObject(with: json,options: [])
if(response.status.code == 200){
return try context.res.json([
"success":true,
"deepgramData": jsonDict
])
}else{
return try context.res.json([
"success":false,
"error": jsonDict
])
}

}
18 changes: 18 additions & 0 deletions swift/deepgram-transcribe-audio/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// swift-tools-version: 5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "ProllyFinal",
dependencies: [
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0"),
],
targets: [
.target(
name: "ProllyFinal",
dependencies: [
.product(name: "AsyncHTTPClient", package: "async-http-client"),
])
]
)