Add postfile function

This commit is contained in:
kwaroran
2024-02-26 23:13:29 +09:00
parent 47c10be8f1
commit 66fd70c01a
16 changed files with 412 additions and 62 deletions

View File

@@ -10,7 +10,7 @@ import { cloneDeep } from "lodash"
import { selectedCharID } from "./stores"
import { convertImage, hasher } from "./parser"
import { reencodeImage } from "./image"
import { reencodeImage } from "./process/files/image"
import { PngChunk } from "./pngChunk"
import type { OnnxModelFiles } from "./process/embedding/transformers"

View File

@@ -7,7 +7,7 @@ import { checkNullish, findCharacterbyId, selectMultipleFile, selectSingleFile,
import { v4 as uuidv4 } from 'uuid';
import { selectedCharID } from "./stores";
import { checkCharOrder, downloadFile, getFileSrc } from "./storage/globalApi";
import { reencodeImage } from "./image";
import { reencodeImage } from "./process/files/image";
import { updateInlayScreen } from "./process/inlayScreen";
import { PngChunk } from "./pngChunk";

View File

@@ -10,7 +10,7 @@ import css from '@adobe/css-tools'
import { selectedCharID } from './stores';
import { calcString } from './process/infunctions';
import { findCharacterbyId } from './util';
import { getInlayImage } from './image';
import { getInlayImage } from './process/files/image';
import { autoMarkNew } from './plugins/automark';
import { getModuleLorebooks } from './process/modules';

View File

@@ -5,7 +5,7 @@ import { alertError, alertNormal, alertStore } from "./alert"
import { downloadFile, readImage } from "./storage/globalApi"
import { language } from "src/lang"
import { cloneDeep } from "lodash"
import { reencodeImage } from "./image"
import { reencodeImage } from "./process/files/image"
import { PngChunk } from "./pngChunk"
export async function selectUserImg() {

View File

@@ -1,30 +1,21 @@
import localforage from "localforage";
import { selectSingleFile } from "./util";
import { selectSingleFile } from "../../util";
import { v4 } from "uuid";
import { DataBase } from "./storage/database";
import { DataBase } from "../../storage/database";
import { get } from "svelte/store";
import { checkImageType } from "./parser";
import { checkImageType } from "../../parser";
const inlayStorage = localforage.createInstance({
name: 'inlay',
storeName: 'inlay'
})
export async function postInlayImage(){
const img = await selectSingleFile([
//image format
'jpg',
'jpeg',
'png',
'webp'
])
if(!img){
return null
}
export async function postInlayImage(img:{
name:string,
data:Uint8Array
}){
const extention = img.name.split('.').at(-1)
const imgObj = new Image()
imgObj.src = URL.createObjectURL(new Blob([img.data], {type: `image/${extention}`}))

View File

@@ -0,0 +1,192 @@
import { DataBase, setDatabase } from 'src/ts/storage/database';
import { selectedCharID } from 'src/ts/stores';
import { get } from 'svelte/store';
import { doingChat, sendChat } from '..';
import { downloadFile, isTauri } from 'src/ts/storage/globalApi';
import { HypaProcesser } from '../memory/hypamemory';
import { BufferToText as BufferToText, selectSingleFile, sleep } from 'src/ts/util';
import { postInlayImage } from './image';
type sendFileArg = {
file:string
query:string
}
async function sendPofile(arg:sendFileArg){
let result = ''
let msgId = ''
let parseMode = 0
const db = get(DataBase)
let currentChar = db.characters[get(selectedCharID)]
let currentChat = currentChar.chats[currentChar.chatPage]
const lines = arg.file.split('\n')
for(let i=0;i<lines.length;i++){
console.log(i)
const line = lines[i]
if(line === ''){
if(msgId === ''){
result += '\n'
continue
}
const id = msgId
currentChat.message.push({
role: 'user',
data: id
})
currentChar.chats[currentChar.chatPage] = currentChat
db.characters[get(selectedCharID)] = currentChar
setDatabase(db)
doingChat.set(false)
await sendChat(-1);
currentChar = db.characters[get(selectedCharID)]
currentChat = currentChar.chats[currentChar.chatPage]
const res = currentChat.message[currentChat.message.length-1]
const msgStr = res.data.split('\n').filter((a) => {
return a !== ''
}).map((str) => {
return `"${str.replaceAll('"', '\\"')}"`
}).join('\n')
result += `msgstr ""\n${msgStr}\n\n`
msgId = ''
if(isTauri){
await downloadFile('translated.po', result)
}
continue
}
if(line.startsWith('msgid')){
parseMode = 0
msgId = line.replace('msgid ', '').trim().replaceAll('\\"', '♠#').replaceAll('"', '').replaceAll('♠#', '\\"')
if(msgId === ''){
parseMode = 1
}
result += line + '\n'
continue
}
if(parseMode === 1 && line.startsWith('"') && line.endsWith('"')){
msgId += line.substring(1, line.length-1).replaceAll('\\"', '"')
result += line + '\n'
continue
}
if(line.startsWith('msgstr')){
if(msgId === ''){
result += line + '\n'
parseMode = 0
}
else{
parseMode = 2
}
continue
}
if(parseMode === 2 && line.startsWith('"') && line.endsWith('"')){
continue
}
result += line + '\n'
if(i > 100){
break //prevent too long message in testing
}
}
await downloadFile('translated.po', result)
}
async function sendPDFFile(arg:sendFileArg) {
const pdfjsLib = (await import('pdfjs-dist')).default;
const pdf = await pdfjsLib.getDocument({data: arg.file}).promise;
const db = get(DataBase)
const texts:string[] = []
for(let i = 1; i<=pdf.numPages; i++){
const page = await pdf.getPage(i);
const content = await page.getTextContent();
const items = content.items as {str:string}[];
for(const item of items){
texts.push(item.str)
}
}
const hypa = new HypaProcesser('MiniLM')
hypa.addText(texts)
let currentChar = db.characters[get(selectedCharID)]
let currentChat = currentChar.chats[currentChar.chatPage]
const result = await hypa.similaritySearch(arg.query)
let message = arg.query
for(let i = 0; i<result.length; i++){
message += "\n" + texts[result[i]]
if(i>5){
break
}
}
currentChat.message.push({
role: 'user',
data: message
})
currentChar.chats[currentChar.chatPage] = currentChat
db.characters[get(selectedCharID)] = currentChar
setDatabase(db)
await sendChat(-1)
}
type postFileResult = postFileResultImage | postFileResultVoid
type postFileResultImage = {
data: string,
type: 'image',
}
type postFileResultVoid = {
type: 'void',
}
export async function postChatFile(query:string):Promise<postFileResult>{
const file = await selectSingleFile([
//image format
'jpg',
'jpeg',
'png',
'webp',
'po',
'pdf'
])
if(!file){
return null
}
const extention = file.name.split('.').at(-1)
console.log(extention)
switch(extention){
case 'po':{
await sendPofile({
file: BufferToText(file.data),
query: query
})
return {
type: 'void'
}
}
case 'pdf':{
await sendPDFFile({
file: BufferToText(file.data),
query: query
})
return {
type: 'void'
}
}
case 'jpg':
case 'jpeg':
case 'png':
case 'webp':{
const postData = await postInlayImage(file)
return {
data: postData,
type: 'image'
}
}
}
return
}

View File

@@ -19,7 +19,7 @@ import { runTrigger } from "./triggers";
import { HypaProcesser } from "./memory/hypamemory";
import { additionalInformations } from "./embedding/addinfo";
import { cipherChat, decipherChat } from "./cipherChat";
import { getInlayImage, supportsInlayImage } from "../image";
import { getInlayImage, supportsInlayImage } from "./files/image";
import { getGenerationModelString } from "./models/modelString";
import { sendPeerChar } from "../sync/multiuser";
import { runInlayScreen } from "./inlayScreen";

View File

@@ -1,4 +1,4 @@
import { writeInlayImage } from "../image";
import { writeInlayImage } from "./files/image";
import type { character } from "../storage/database";
import { generateAIImage } from "./stableDiff";

View File

@@ -17,7 +17,7 @@ import { HttpRequest } from "@smithy/protocol-http";
import { Sha256 } from "@aws-crypto/sha256-js";
import { v4 } from "uuid";
import { cloneDeep } from "lodash";
import { supportsInlayImage } from "../image";
import { supportsInlayImage } from "./files/image";
import { OaifixBias } from "../plugins/fixer";
import { Capacitor } from "@capacitor/core";
import { getFreeOpenRouterModel } from "../model/openrouter";

View File

@@ -3,7 +3,7 @@ import type { Tokenizer } from "@mlc-ai/web-tokenizers";
import { DataBase, type character } from "./storage/database";
import { get } from "svelte/store";
import type { OpenAIChat } from "./process";
import { supportsInlayImage } from "./image";
import { supportsInlayImage } from "./process/files/image";
import { risuChatParser } from "./parser";
import { tokenizeGGUFModel } from "./process/models/local";

View File

@@ -42,8 +42,9 @@ export function checkNullish(data:any){
return data === undefined || data === null
}
const domSelect = true
export async function selectSingleFile(ext:string[]){
if(await !isTauri){
if(domSelect){
const v = await selectFileByDom(ext, 'single')
const file = v[0]
return {name: file.name,data:await readFileAsUint8Array(file)}
@@ -375,4 +376,11 @@ export function getCurrentCharacter(){
export function toState<T>(t:T):Writable<T>{
return writable(t)
}
export function BufferToText(data:Uint8Array){
if(!TextDecoder){
return Buffer.from(data).toString('utf-8')
}
return new TextDecoder().decode(data)
}