Skip to content

Commit

Permalink
Consider os line separator and add graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
pbernet committed Feb 23, 2024
1 parent de49245 commit d2c01dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/main/scala/tools/SrtParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class SrtParser(sourceFilePath: String) {

def runSync(): Seq[SubtitleBlock] = {
val resultFut = source.runWith(Sink.seq)
Await.result(resultFut, 10.seconds)
val result = Await.result(resultFut, 10.seconds)
system.terminate()
result
}
}

Expand All @@ -67,11 +69,12 @@ object SrtParser extends App {

case class SubtitleBlock(start: Long, end: Long, lines: Seq[String]) {
val logger: Logger = LoggerFactory.getLogger(this.getClass)
val ls = sys.props("line.separator")
val endLineTag = "\n" // for openAI API
val ls = sys.props("line.separator") // for file IO

def allLines: String = lines.mkString(" ")

def allLinesEnd: String = allLines + ls + ls
def allLinesEnd: String = allLines + endLineTag + endLineTag

def formatOutBlock(blockCounter: Long): String = {
// Spec: https://wiki.videolan.org/SubRip
Expand Down
13 changes: 6 additions & 7 deletions src/main/scala/tools/SubtitleTranslator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object SubtitleTranslator extends App {
private val fallbackModel = "gpt-3.5-turbo-instruct"

private val maxGapSeconds = 1 // gap time between two scenes (= session windows)
private val endBlockTag = sys.props("line.separator") // one block per line
private val endLineTag = "\n"
private val maxCharPerTranslatedLine = 40 // recommendation
private val conversationPrefix = "-"

Expand Down Expand Up @@ -127,7 +127,7 @@ object SubtitleTranslator extends App {

val sceneTranslated: Vector[SubtitleBlock] =
rawResponseText
.split(endBlockTag)
.split(endLineTag)
.filterNot(each => each.isEmpty)
.zipWithIndex
.foldLeft(seed) { (acc: Vector[SubtitleBlock], rawResponseTextSplit: (String, Int)) =>
Expand All @@ -144,14 +144,13 @@ object SubtitleTranslator extends App {
logger.info(s"Translated block to: ${translatedBlock.allLines}")
acc.appended(translatedBlock)
}
logger.info(s"Finished line by line translation of scene with: ${sceneTranslated.size} blocks")

logger.info(s"Finished translation of scene with: ${sceneTranslated.size} blocks")
sceneTranslated
}

private def isTranslationPlausible(rawResponseText: String, originalSize: Int) = {
val resultSize = rawResponseText
.split(endBlockTag)
.split(endLineTag)
.filterNot(each => each.isEmpty)
.length

Expand All @@ -173,7 +172,7 @@ object SubtitleTranslator extends App {

private def generateShortenPrompt(text: String) = {
s"""
|Rewrite to ${maxCharPerTranslatedLine * 2} characters at most:
|Rewrite to ${maxCharPerTranslatedLine * 2 - 10} characters at most:
|$text
|
|""".stripMargin
Expand All @@ -185,7 +184,7 @@ object SubtitleTranslator extends App {
if (textCleaned.startsWith(conversationPrefix)) {
textCleaned.split(conversationPrefix).map(line => conversationPrefix + line).toList.tail
}
else if (textCleaned.length > maxCharPerTranslatedLine * 2) {
else if (textCleaned.length > maxCharPerTranslatedLine * 2 + 10) {
logger.warn(s"Translated block text is too long (${textCleaned.length} chars). Try to shorten via API call. Check result manually")
val toShorten = generateShortenPrompt(textCleaned)
logger.info(s"Shorten prompt: $toShorten")
Expand Down

0 comments on commit d2c01dd

Please sign in to comment.