refactor, TODO remove mainObject

This commit is contained in:
zramsay 2025-01-11 19:02:12 -05:00
parent d48108429b
commit cc8bc7f057
3 changed files with 42 additions and 20 deletions

View File

@ -1,19 +1,35 @@
// src/app/animals/page.tsx
'use client'
import React, { useEffect, useState } from 'react'
import Navigation from '../../components/Navigation'
import { fetchAnimalRecords } from '../../services/laconicQueryService'
import { useState, useEffect } from 'react'
import { MapPin } from 'lucide-react'
import { fetchAnimalRecords } from '../../services/laconicQueryService'
import Navigation from '../../components/Navigation'
interface AnimalRecord {
id: string
species: string
mainObject: string
location: {
latitude: number
longitude: number
}
description: string
imageUrl: string
species: string
// Records from Laconic Registry use `attributes` to store the content
// and have a separate creation timestamp
attributes?: {
mainObject: string
location: {
latitude: number
longitude: number
}
description: string
imageUrl: string
species: string
}
bondId?: string
createTime: string
}
@ -81,7 +97,7 @@ export default function AnimalsPage() {
}
return (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{records.map((record, index) => (
<div
key={`${record.id}-${index}`}

View File

@ -4,6 +4,7 @@ import { ANIMAL_LABELS } from './constants'
export interface VisionAnalysisResult {
description: string
mainObject: string // Added to store the primary detected object
isAnimal: boolean
rawResponse: any
}
@ -55,14 +56,22 @@ export async function analyzeImageWithVision(imageBuffer: Buffer): Promise<Visio
ANIMAL_LABELS.includes(label.description.toLowerCase())
)
// Get the first animal label as main object
const mainObject = labels.find(label =>
ANIMAL_LABELS.includes(label.description.toLowerCase())
)?.description || 'Unknown'
const { description } = constructDescription(labels, objects)
return {
description: constructDescription(labels, objects),
description,
mainObject,
isAnimal,
rawResponse: data.responses[0]
}
}
function constructDescription(labels: any[], objects: any[]): string {
function constructDescription(labels: any[], objects: any[]): { description: string } {
const topLabels = labels
.filter(label => label.score > 0.7)
.map(label => label.description)
@ -74,15 +83,9 @@ function constructDescription(labels: any[], objects: any[]): string {
.filter((value, index, self) => self.indexOf(value) === index)
.slice(0, 3)
let description = "In this image, I can see "
// Only include "The scene includes..." part as description
let description = "The scene includes "
description += topLabels.join(", ") + "."
if (topObjects.length > 0) {
description += topObjects.join(", ") + ". "
}
if (topLabels.length > 0) {
description += "The scene includes " + topLabels.join(", ") + "."
}
return description
return { description }
}

View File

@ -1,6 +1,8 @@
// src/services/laconicQueryService.ts
//
interface AnimalRecord {
id: string
mainObject: string
species: string
location: {
latitude: number
@ -8,9 +10,9 @@ interface AnimalRecord {
}
description: string
imageUrl: string
createTime: string
}
const LACONIC_GQL_ENDPOINT = process.env.NEXT_PUBLIC_LACONIC_GQL_ENDPOINT || 'https://laconicd-sapo.laconic.com/api'
const ANIMAL_RECORDS_QUERY = `
@ -88,7 +90,8 @@ export async function fetchAnimalRecords(): Promise<AnimalRecord[]> {
longitude: location.longitude || 0
},
description: attributesMap.description || 'No description',
imageUrl: attributesMap.imageUrl || null
imageUrl: attributesMap.imageUrl || null,
createTime: record.createTime || ''
}
})
// Filter out records without an imageUrl