mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
252 lines
7.6 KiB
Go
252 lines
7.6 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
package object
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hanzoai/ai/util"
|
|
"github.com/hanzoai/ai/video"
|
|
"github.com/hanzoai/dbx"
|
|
)
|
|
|
|
type Label struct {
|
|
Id string `json:"id"`
|
|
User string `json:"user"`
|
|
Type string `json:"type"`
|
|
StartTime float64 `json:"startTime"`
|
|
EndTime float64 `json:"endTime"`
|
|
Text string `json:"text"`
|
|
Speaker string `json:"speaker"`
|
|
Tag1 string `json:"tag1"`
|
|
Tag2 string `json:"tag2"`
|
|
Tag3 string `json:"tag3"`
|
|
}
|
|
type Remark struct {
|
|
Timestamp string `json:"timestamp"`
|
|
User string `json:"user"`
|
|
Score string `json:"score"`
|
|
Text string `json:"text"`
|
|
IsPublic bool `json:"isPublic"`
|
|
}
|
|
type Video struct {
|
|
Owner string `db:"pk" json:"owner"`
|
|
Name string `db:"pk" json:"name"`
|
|
CreatedTime string `json:"createdTime"`
|
|
DisplayName string `json:"displayName"`
|
|
Description string `json:"description"`
|
|
Tag string `json:"tag"`
|
|
Type string `json:"type"`
|
|
VideoId string `json:"videoId"`
|
|
VideoLength string `json:"videoLength"`
|
|
CoverUrl string `json:"coverUrl"`
|
|
DownloadUrl string `json:"downloadUrl"`
|
|
AudioUrl string `json:"audioUrl"`
|
|
EditMode string `json:"editMode"`
|
|
Labels []*Label `json:"labels"`
|
|
Segments []*Label `json:"segments"`
|
|
LabelCount int `db:"-" json:"labelCount"`
|
|
SegmentCount int `db:"-" json:"segmentCount"`
|
|
WordCountMap map[string]int `json:"wordCountMap"`
|
|
DataUrls StringList `json:"dataUrls"`
|
|
DataUrl string `json:"dataUrl"`
|
|
TagOnPause bool `json:"tagOnPause"`
|
|
Remarks []*Remark `json:"remarks"`
|
|
Remarks2 []*Remark `json:"remarks2"`
|
|
ExcellentCount int `json:"excellentCount"`
|
|
State string `json:"state"`
|
|
ReviewState string `json:"reviewState"`
|
|
IsPublic bool `json:"isPublic"`
|
|
School string `json:"school"`
|
|
Stage string `json:"stage"`
|
|
Grade string `json:"grade"`
|
|
Unit string `json:"unit"`
|
|
Lesson string `json:"lesson"`
|
|
Class string `json:"class"`
|
|
Subject string `json:"subject"`
|
|
Topic string `json:"topic"`
|
|
Grade2 string `json:"grade2"`
|
|
Keywords StringList `json:"keywords"`
|
|
Template string `json:"template"`
|
|
Task1 string `json:"task1"`
|
|
Task2 string `json:"task2"`
|
|
Task3 string `json:"task3"`
|
|
PlayAuth string `db:"-" json:"playAuth"`
|
|
}
|
|
|
|
// The public listing is the rows that say they are public. The page has always
|
|
// drawn exactly this set — it drew it in the browser, after every row had already
|
|
// crossed the network — so the set is unchanged and the rows it discards are no
|
|
// longer sent. IsPublic is the row's own answer; ask the table for it.
|
|
func GetGlobalVideos() ([]*Video, error) {
|
|
return rowsWhere[Video]("video", dbx.HashExp{"is_public": true})
|
|
}
|
|
|
|
func GetVideos(owner string, lang string) ([]*Video, error) {
|
|
videos := []*Video{}
|
|
err := findAll(adapter.db, "video", &videos, dbx.HashExp{"owner": owner}, "created_time DESC")
|
|
if err != nil {
|
|
return videos, err
|
|
}
|
|
for _, v := range videos {
|
|
err = v.refineVideoAndCoverUrl(lang)
|
|
if err != nil {
|
|
return videos, err
|
|
}
|
|
}
|
|
return videos, nil
|
|
}
|
|
|
|
func getVideo(owner, name string) (*Video, error) {
|
|
return getRow[Video]("video", owner, name)
|
|
}
|
|
|
|
// GetVideo reads the row and nothing else. Handing back a credential that plays
|
|
// the video is a separate act with a separate cost — the VOD service can spend a
|
|
// minute admitting a fresh upload — so it is a separate call, made once the
|
|
// caller is known to be allowed the video.
|
|
func GetVideo(id string) (*Video, error) {
|
|
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return getVideo(owner, name)
|
|
}
|
|
|
|
// Play attaches the credential that plays this video. While the VOD service is
|
|
// still admitting an upload it reports "AuditStatus is Init", which is the one
|
|
// answer worth waiting on; a video that never finishes admitting comes back
|
|
// without a credential rather than as an error, as it always has.
|
|
func (v *Video) Play(lang string) error {
|
|
if v == nil || v.VideoId == "" {
|
|
return nil
|
|
}
|
|
if err := SetDefaultVodClient(lang); err != nil {
|
|
return err
|
|
}
|
|
for range 30 {
|
|
auth, err := video.GetVideoPlayAuth(v.VideoId)
|
|
if err == nil {
|
|
v.PlayAuth = auth
|
|
return nil
|
|
}
|
|
if !strings.Contains(err.Error(), "and AuditStatus is Init.") {
|
|
return err
|
|
}
|
|
time.Sleep(2 * time.Second)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func UpdateVideo(id string, video *Video) (bool, error) {
|
|
owner, name, err := util.GetOwnerAndNameFromIdWithError(id)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if video == nil {
|
|
return false, nil
|
|
}
|
|
video.Owner, video.Name = owner, name
|
|
return updated(video)
|
|
}
|
|
|
|
func AddVideo(video *Video) (bool, error) {
|
|
return addRow(video)
|
|
}
|
|
|
|
func DeleteVideo(video *Video) (bool, error) {
|
|
return deleteRow("video", video.Owner, video.Name)
|
|
}
|
|
|
|
func (video *Video) GetId() string {
|
|
return fmt.Sprintf("%s/%s", video.Owner, video.Name)
|
|
}
|
|
|
|
func (video *Video) Populate(lang string) error {
|
|
// store, err := GetDefaultStore("admin")
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// if store == nil {
|
|
// return nil
|
|
// }
|
|
//
|
|
// dataUrls, err := store.GetVideoData()
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// video.DataUrls = dataUrls
|
|
err := video.PopulateWordCountMap(lang)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if video.EditMode == "" {
|
|
if len(video.Segments) == 0 {
|
|
video.EditMode = "Labeling"
|
|
} else {
|
|
video.EditMode = "Text Recognition"
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v *Video) refineVideoAndCoverUrl(lang string) error {
|
|
excellentCount := 0
|
|
for _, remark := range v.Remarks {
|
|
if remark.Score == "Excellent" {
|
|
excellentCount++
|
|
}
|
|
}
|
|
v.ExcellentCount = excellentCount
|
|
if v.VideoId == "" || (v.CoverUrl != "" && v.DownloadUrl != "") {
|
|
return nil
|
|
}
|
|
err := SetDefaultVodClient(lang)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
coverUrl := video.GetVideoCoverUrl(v.VideoId)
|
|
v.CoverUrl = coverUrl
|
|
downloadUrl := video.GetVideoFileUrl(v.VideoId)
|
|
v.DownloadUrl = downloadUrl
|
|
_, err = UpdateVideo(v.GetId(), v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GetVideoCount(owner string, field string, value string) (int64, error) {
|
|
return rowCount("video", owner, field, value)
|
|
}
|
|
|
|
func GetPaginationVideos(owner string, offset int, limit int, field string, value string, sortField string, sortOrder string, lang string) ([]*Video, error) {
|
|
videos := []*Video{}
|
|
session := GetDbQuery(owner, offset, limit, field, value, sortField, sortOrder)
|
|
err := queryFind(session, "video", &videos)
|
|
if err != nil {
|
|
return videos, err
|
|
}
|
|
for _, v := range videos {
|
|
err = v.refineVideoAndCoverUrl(lang)
|
|
if err != nil {
|
|
return videos, err
|
|
}
|
|
}
|
|
return videos, nil
|
|
}
|