captionbot

package module
v0.0.0-...-3ff7bd5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2017 License: MIT Imports: 11 Imported by: 0

README

captionbot

Captionbot is a simple API wrapper for https://www.captionbot.ai/

Installation

go get github.com/nhatbui/captionbot

Usage

package main 
 
import ( 
        "fmt" 
        "os" 
 
        "github.com/nhatbui/captionbot" 
) 
 
func main() { 
 
        bot, err := captionbot.New() 
        if err != nil { 
                fmt.Printf("error instantiating bot %s\n", err) 
                os.Exit(1) 
        } 
 
        // caption a remote image by provideing a URL 
        imgURL := "http://www.nhatqbui.com/assets/me.jpg" 
 
        caption, err := bot.URLCaption(imgURL) 
        if err != nil { 
                fmt.Printf("error uploading caption %s\n", err) 
                os.Exit(1) 
        } 
        fmt.Println(caption) 
 
        // caption a local image by uploading it 
        imgFile := "./sample.jpg" 
 
        caption, err = bot.UploadCaption(imgFile) 
        if err != nil { 
                fmt.Printf("error uploading caption %s\n", err) 
                os.Exit(1) 
        } 
        fmt.Println(caption) 
}

Thanks

Thanks to @krikunts for their work on captionbot in Python that inspired this package.

Documentation

Overview

Captionbot is a simple API wrapper for https://www.captionbot.ai/

Usage

package main

import (
        "fmt"
        "os"

        "github.com/nhatbui/captionbot"
)

func main() {

        bot, err := captionbot.New()
        if err != nil {
                fmt.Printf("error instantiating bot %s\n", err)
                os.Exit(1)
        }

        // caption a remote image by provideing a URL
        imgURL := "http://www.nhatqbui.com/assets/me.jpg"

        caption, err := bot.URLCaption(imgURL)
        if err != nil {
                fmt.Printf("error uploading caption %s\n", err)
                os.Exit(1)
        }
        fmt.Println(caption)

        // caption a local image by uploading it
        imgFile := "./sample.jpg"

        caption, err = bot.UploadCaption(imgFile)
        if err != nil {
                fmt.Printf("error uploading caption %s\n", err)
                os.Exit(1)
        }
        fmt.Println(caption)
}

Index

Constants

This section is empty.

Variables

View Source
var BaseURL = "https://www.captionbot.ai/api/"

BaseURL is the root path of Caption Bot URL. All requests will be paths starting from here.

Functions

func CreateCaptionTask

func CreateCaptionTask(data bytes.Buffer) error

CreateCaptionTask is the request that starts a URL caption request on the server. Result will need to be retrieved by a subsequent GET request with the same parameters used here.

func MakeValuesFromState

func MakeValuesFromState(imgURL string, state CaptionBotClientState) url.Values

MakeValuesFromState creates values struct from state struct

Types

type CaptionBot

type CaptionBot struct {
	// contains filtered or unexported fields
}

CaptionBot is a struct representing one session with CaptionBot.

func New

func New() (*CaptionBot, error)

New creates and initializes a new CaptionBot object

func (*CaptionBot) Initialize

func (captionBot *CaptionBot) Initialize() error

Initialize sends request to /init endpoint to retrieve conversationID. This is a session variable used in the state struct.

func (*CaptionBot) URLCaption

func (captionBot *CaptionBot) URLCaption(url string) (string, error)

URLCaption is the entry method for getting caption for image pointed to by URL. Performs a POST request to start the caption task. Then performs a GET request to retrieve the result.

func (*CaptionBot) UploadCaption

func (captionBot *CaptionBot) UploadCaption(fileName string) (string, error)

UploadCaption uploads a file and runs URLCaption on the result

type CaptionBotClientState

type CaptionBotClientState struct {
	// contains filtered or unexported fields
}

CaptionBotClientState is a struct to hold "session" state.

  1. conversationID: given during call to Initialize() Should be used for subsequent requests.
  2. waterMark: is updated per URL caption response.

(Note: consequences of not maintaining state is unknown.)

type CaptionBotConnection

type CaptionBotConnection interface {
	URLCaption(url string) (string, error)
	Initialize() error
}

CaptionBotConnection is an interface for methods for one CaptionBot session.

type CaptionBotRequest

type CaptionBotRequest struct {
	ConversationID string `json:"conversationID"`
	UserMessage    string `json:"userMessage"`
	WaterMark      string `json:"waterMark"`
}

CaptionBotRequest is a struct to hold data for API URL caption requests.

type CaptionBotResponse

type CaptionBotResponse struct {
	ConversationID string
	UserMessage    string
	WaterMark      string
	Status         string
	BotMessages    []string
}

CaptionBotResponse is a struct to hold data for API URL caption responses.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL