nlpodyssey / cybertron

Cybertron: the home planet of the Transformers in Go
BSD 2-Clause "Simplified" License
289 stars 26 forks source link

Answers haven't answer #32

Closed devalexandre closed 1 year ago

devalexandre commented 1 year ago

I try use a questionanswering, but not work

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "github.com/nlpodyssey/cybertron/pkg/tasks"
    "github.com/nlpodyssey/cybertron/pkg/tasks/questionanswering"
    "github.com/rs/zerolog"
    "github.com/rs/zerolog/log"
    "time"
)

func main() {
    zerolog.SetGlobalLevel(zerolog.DebugLevel)

    modelsDir := "models"                                               //directory where the model will be downloaded
    modelName := "deepset/bert-large-uncased-whole-word-masking-squad2" //model name for token classification in Hugging Face

    m, err := tasks.Load[questionanswering.Interface](&tasks.Config{ModelsDir: modelsDir, ModelName: modelName})

    if err != nil {
        log.Fatal().Err(err).Send()
    }
    defer tasks.Finalize(m)

    text := "My name is John and I live in Franc"
    opts := &questionanswering.Options{
        MaxCandidates: 1,
    }

    question := "What is the full name of the person mentioned?"
    start := time.Now()
    result, err := m.Answer(context.Background(), text, question, opts)
    if err != nil {
        fmt.Println("something are erro")
    }

    fmt.Println("time taken: ", time.Since(start))

    //display the result in json format

    fmt.Println(MarshalJSON(result))

}

func MarshalJSON(data any) string {
    m, _ := json.MarshalIndent(data, "", "  ")
    return string(m)
}

When run the output is

{
  "Answers": [
    {
      "Text": "full name of the person mentioned?",
      "Start": 12,
      "End": 46,
      "Score": 1
    }
  ]
}

but the correct is

{
  "Answers": [
    {
      "Text": "John",
      "Start": 11,
      "End": 15,
      "Score": 1
    }
  ]
}