diff options
| author | aidahosa1 <aisosa_idahosa@brown.edu> | 2024-05-16 23:04:38 -0400 |
|---|---|---|
| committer | aidahosa1 <aisosa_idahosa@brown.edu> | 2024-05-16 23:04:38 -0400 |
| commit | d56ce09de98d843cf46b0d6eda5f8c82446f7849 (patch) | |
| tree | dc18b4f7568128bd35cda842e290c3678bc3f303 /src/client/views/collections/CollectionCardDeckView.tsx | |
| parent | df7532553db3df270fcbd25a46e7a349a82bd712 (diff) | |
ab to do some silly thing
Diffstat (limited to 'src/client/views/collections/CollectionCardDeckView.tsx')
| -rw-r--r-- | src/client/views/collections/CollectionCardDeckView.tsx | 72 |
1 files changed, 63 insertions, 9 deletions
diff --git a/src/client/views/collections/CollectionCardDeckView.tsx b/src/client/views/collections/CollectionCardDeckView.tsx index 561b9f806..e7e2cba4a 100644 --- a/src/client/views/collections/CollectionCardDeckView.tsx +++ b/src/client/views/collections/CollectionCardDeckView.tsx @@ -18,9 +18,9 @@ import { DocumentType } from '../../documents/DocumentTypes'; import { forEach } from 'lodash'; import { SnappingManager } from '../../util/SnappingManager'; import { List } from '../../../fields/List'; -import { gptAPICall } from '../../apis/gpt/GPT'; +import { gptAPICall, gptImageLabel } from '../../apis/gpt/GPT'; import { GPTCallType } from '../../apis/gpt/GPT'; -import { ImageField } from '../../../fields/URLField'; +import { ImageField, PdfField, URLField } from '../../../fields/URLField'; @observer export class CollectionCardView extends CollectionSubView() { @@ -253,7 +253,7 @@ export class CollectionCardView extends CollectionSubView() { return this.sort(sorted, 'custom', desc); case 'chat': - this.smartSort() + this.getImageDesc() docs = this.myChildLayoutPairs; return {docs} @@ -493,28 +493,83 @@ export class CollectionCardView extends CollectionSubView() { // const - const imgurlperchance = Cast(this.childDocs[1].data, ImageField, null)?.url; + const imgurlperchance = Cast(this.childDocs[1].data, ImageField, null)?.url.href; + const perchance = this.imageUrlToBase64(imgurlperchance) + // const imgurlperchance = Cast(this.childDocs[1].data, PdfField, null)?.url.; - console.log('Print Front of cards: ' + RTFCast(DocCast(this.dataDoc[this.fieldKey + '_0']).text)?.Text); - console.log('Print Back of cards: ' + RTFCast(DocCast(this.dataDoc[this.fieldKey + '_1']).text)?.Text); + // console.log('Print Front of cards: ' + RTFCast(DocCast(this.dataDoc[this.fieldKey + '_0']).text)?.Text); + // console.log('Print Back of cards: ' + RTFCast(DocCast(this.dataDoc[this.fieldKey + '_1']).text)?.Text); + + // const perchance = StrCast(this.childDocs[0].text); + + + + // const pdf = (StrCast(this.myChildLayoutPairs[0].layout.text)); //pdf + // const queryText = (RTFCast(this.myChildLayoutPairs[0].layout.text)); //everything else - // const queryText = (RTFCast(this.myChildLayoutPairs[0].layout.text)).Text; // const queryText = RTFCast(DocCast(this.dataDoc[this.fieldKey + '_1']).text)?.Text; - console.log(imgurlperchance ?? "sad") + console.log(perchance ?? "sad") } childPairStringList () { let string = "" for (let i=0; i < this.myChildLayoutPairs.length; i++){ + + + let text = StrCast((RTFCast(this.myChildLayoutPairs[i].layout.text)).Text) string += StrCast((RTFCast(this.myChildLayoutPairs[i].layout.text)).Text) + ", " + this.textToDoc.has() } return string } @observable isLoading = false + imageUrlToBase64 = async (imageUrl: string): Promise<string> => { + try { + const response = await fetch(imageUrl); + const blob = await response.blob(); + + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.readAsDataURL(blob); + reader.onloadend = () => resolve(reader.result as string); + reader.onerror = error => reject(error); + }); + } catch (error) { + console.error('Error:', error); + throw error; + } + }; + + textToDoc = new Map<string, Doc>() + gptProccessedImages = new Set<Doc>() + + @action async getImageDesc(){ + const images = this.childDocs.filter(d => d.type == DocumentType.IMG).filter(d=> !this.gptProccessedImages.has(d)); + images.map(doc => { + let href = (doc['data'] as URLField).url.href; + let hrefParts = href.split('.'); + let hrefComplete = `${hrefParts[0]}_o.${hrefParts[1]}`; + return this.imageUrlToBase64(hrefComplete).then(hrefBase64 => { + return gptImageLabel(hrefBase64).then(response => { + this.textToDoc.set(response, doc) + this.gptProccessedImages.add(doc) + console.log(response); + + }); + }); + }); + } + + + + //a set of all the images that have already been given desc + //a map from the text to the Doc (for everything) + + @action async smartSort() { this.isLoading = true; console.log("loading") @@ -536,7 +591,6 @@ export class CollectionCardView extends CollectionSubView() { // If GPT call failed console.error('GPT call failed'); } else if (res != null) { - // If GPT call succeeded, set htmlCode;;; TODO: check if valid html console.log(res) } this.isLoading = false; |
