Skip to content

Commit

Permalink
build: 更换hf构建方式
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Aug 21, 2023
1 parent 92977cc commit 39ddf96
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 75 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/huggingface.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ jobs:
sync-to-hub:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
lfs: true
- name: Push to hub
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
HF_USERNAME: hf4all
SPACE_NAME: bingo
run: git push https://$HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME main -f
run: |
git clone https://$HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME main
git commit -am "auto build" --amend
git push https://$HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME main -f
12 changes: 3 additions & 9 deletions src/components/chat-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,20 @@ import {
KeyboardEvent
} from "react"
import Image from 'next/image'
import { toast } from "react-hot-toast"
import PasteIcon from '@/assets/images/paste.svg'
import UploadIcon from '@/assets/images/upload.svg'
import CameraIcon from '@/assets/images/camera.svg'
import { useBing } from '@/lib/hooks/use-bing'
import { cn } from '@/lib/utils'
import { toast } from "react-hot-toast"
import { ImageUtils } from "@/lib/image"

interface ChatImageProps extends Pick<ReturnType<typeof useBing>, 'uploadImage'> {}

const preventDefault: MouseEventHandler<HTMLDivElement> = (event) => {
event.nativeEvent.stopImmediatePropagation()
}

const toBase64 = (file: File): Promise<string> => new Promise((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => resolve(reader.result as string)
reader.onerror = reject
})

export function ChatImage({ children, uploadImage }: React.PropsWithChildren<ChatImageProps>) {
const videoRef = useRef<HTMLVideoElement>(null)
const canvasRef = useRef<HTMLCanvasElement>(null)
Expand All @@ -46,7 +40,7 @@ export function ChatImage({ children, uploadImage }: React.PropsWithChildren<Cha
const onUpload = useCallback(async (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0]
if (file) {
const fileDataUrl = await toBase64(file)
const fileDataUrl = await ImageUtils.getCompressedImageDataAsync(file)
if (fileDataUrl) {
upload(fileDataUrl)
}
Expand Down
22 changes: 0 additions & 22 deletions src/lib/bots/bing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,28 +249,6 @@ export class BingWebBot {
return wsp
}

private async useWs(params: Params) {
const wsp = await this.sendWs()
const watchDog = new WatchDog()
wsp.onUnpackedMessage.addListener((events) => {
watchDog.watch(() => {
wsp.sendPacked({ type: 6 })
})
this.parseEvents(params, events)
})

wsp.onClose.addListener(() => {
watchDog.reset()
params.onEvent({ type: 'DONE' })
wsp.removeAllListeners()
})

params.signal?.addEventListener('abort', () => {
wsp.removeAllListeners()
wsp.close()
})
}

private async createImage(prompt: string, id: string) {
try {
const headers = {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/hooks/use-bing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function useBing(botId: BotId = 'bing') {
speaker.reset()
await chatState.bot.sendMessage({
prompt: input,
imageUrl: /\?bcid=([^&]+)/.test(imageUrl ?? '') ? `https://www.bing.com/images/blob?bcid=${RegExp.$1}` : imageUrl,
imageUrl,
options: {
...options,
bingConversationStyle,
Expand Down Expand Up @@ -92,7 +92,7 @@ export function useBing(botId: BotId = 'bing') {
setAttachmentList([{ url: imgUrl, status: 'loading' }])
const response = await chatState.bot.uploadImage(imgUrl, bingConversationStyle)
if (response?.blobId) {
setAttachmentList([{ url: `/api/blob?bcid=${response.blobId}`, status: 'loaded' }])
setAttachmentList([{ url: new URL(`/api/blob.jpg?bcid=${response.blobId}`, location.href).toString(), status: 'loaded' }])
} else {
setAttachmentList([{ url: imgUrl, status: 'error' }])
}
Expand Down
132 changes: 132 additions & 0 deletions src/lib/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
export class FileUtils {
static getDataUrlAsync(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onabort = () => {
reject('Load Aborted')
}

reader.onerror = () => {
reject(reader.error)
}

reader.onload = () => {
resolve(reader.result as string)
}

reader.readAsDataURL(file)
})
}
static getArrayBufferAsync(file: File): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onabort = () => {
reject('Load Aborted')
}

reader.onerror = () => {
reject(reader.error)
}

reader.onload = () => {
resolve(reader.result as ArrayBuffer)
}

reader.readAsArrayBuffer(file)
})
}
static getImageElementFromDataUrl(dataURL: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const image = new Image()
image.onerror = () => {
reject(null)
}

image.onload = () => {
resolve(image)
}

image.src = dataURL
})
}

static dataURLtoFile(dataURL: string, type?: string) {
const typeRe = /:(.*?);/
let [meta, base64] = dataURL.split(',')
if (!base64) {
base64 = meta
} else if (!type) {
type = typeRe.test(meta) ? RegExp.$1 : undefined
}

const rawData = atob(base64)
let len = rawData.length
const unitArray = new Uint8Array(len)
while(len--) {
unitArray[len] = rawData.charCodeAt(len)
}
return new File([unitArray], 'temp', type ? {
type
} : undefined)
}
static isFile(file: File | string) {
return Boolean((file as File).stream)
}
}

export class ImageUtils {
static async getCompressedImageDataAsync(file: File | string) {
let dataURI: string
let fileObj: File
if (FileUtils.isFile(file)) {
fileObj = file as File
dataURI = await FileUtils.getDataUrlAsync(fileObj)
} else {
dataURI = file as string
fileObj = FileUtils.dataURLtoFile(dataURI)
}

if (typeof document === 'undefined' || !document.createElement) {
return dataURI
}
const image = await FileUtils.getImageElementFromDataUrl(dataURI)
if (!image.width || !image.height)
throw new Error('Failed to load image.')

let { width, height } = image
const rate = 36e4 / (width * height)
if (rate < 1) {
const scaleRate = Math.sqrt(rate)
width *= scaleRate
height *= scaleRate
}

const canvas = document.createElement('canvas')
ImageUtils.processImage(canvas, width, height, image)
return ImageUtils.getImageDataOnCanvas(canvas)
}

static drawImageOnCanvas(canvas: HTMLCanvasElement, image: HTMLImageElement, width: number, height: number) {
const ctx = canvas.getContext('2d')
if (ctx) {
canvas.width = width,
canvas.height = height,
ctx.drawImage(image, 0, 0, width, height)
}
}
static getImageDataOnCanvas(canvas: HTMLCanvasElement) {
return canvas.toDataURL('image/jpeg', 0.7)
}
static processImage(canvas: HTMLCanvasElement, targetWidth: number, targetHeight: number, image: HTMLImageElement) {
const { width, height } = canvas.style
const ctx = canvas.getContext('2d')!
canvas.width = targetWidth
canvas.height = targetHeight
canvas.style.width = width
canvas.style.height = height

ctx.fillStyle = '#FFFFFF'
ctx.fillRect(0, 0, targetWidth, targetHeight)
ctx.drawImage(image, 0, 0, targetWidth, targetHeight)
}
}
10 changes: 5 additions & 5 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ export function parseUA(ua?: string, default_ua = DEFAULT_UA) {
export function mockUser(cookies: Partial<{ [key: string]: string }>) {
const {
BING_UA = process.env.BING_UA,
BING_IP,
BING_IP = '',
_U = defaultUID,
} = cookies
const ua = parseUA(BING_UA)

return {
'x-forwarded-for': BING_IP!,
'x-forwarded-for': BING_IP,
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'User-Agent': ua!,
Expand All @@ -124,10 +124,10 @@ export function mockUser(cookies: Partial<{ [key: string]: string }>) {
}
}

export function createHeaders(cookies: Partial<{ [key: string]: string }>, type?: string) {
export function createHeaders(cookies: Partial<{ [key: string]: string }>, type?: 'image') {
let {
BING_HEADER = process.env.BING_HEADER,
BING_IP,
BING_IP = '',
IMAGE_ONLY = process.env.IMAGE_ONLY ?? '1',
} = cookies
const imageOnly = /^(1|true|yes)$/.test(String(IMAGE_ONLY))
Expand All @@ -140,7 +140,7 @@ export function createHeaders(cookies: Partial<{ [key: string]: string }>, type?
BING_HEADER,
...cookies,
}) || {}
headers['x-forward-for'] = BING_IP!
headers['x-forward-for'] = BING_IP
return headers
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/pages/api/blob.ts → src/pages/api/blob.jpg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Readable } from 'node:stream'
import { fetch } from '@/lib/isomorphic'
import { createHeaders } from '@/lib/utils'

const API_DOMAIN = 'https://www.bing.com'

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { bcid } = req.query

const { headers, body } = await fetch(`${API_DOMAIN}/images/blob?bcid=${bcid}`,
{
method: 'GET',
headers: {
"sec-ch-ua": "\"Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"115\", \"Chromium\";v=\"115\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"Referrer-Policy": "origin-when-cross-origin",
},
headers: createHeaders(req.cookies, 'image'),
},
)

Expand All @@ -27,10 +22,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
'Content-Type': headers.get('content-type')!,
})
// @ts-ignore
return Readable.fromWeb(body!).pipe(res)
Readable.fromWeb(body!).pipe(res)
} catch (e) {
console.log('Error', e)
return res.json({
res.json({
result: {
value: 'UploadFailed',
message: `${e}`
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}))
} catch (e) {
console.log('error', e)
return res.end(JSON.stringify({
res.end(JSON.stringify({
result: {
value: 'UnauthorizedRequest',
message: `${e}`
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
res.writeHead(200, {
'Content-Type': 'text/plain; charset=UTF-8',
})
return res.end(response)
res.end(response)
} catch (e) {
return res.json({
res.json({
result: {
value: 'Error',
message: `${e}`
Expand Down
25 changes: 14 additions & 11 deletions src/pages/api/kblob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import { NextApiRequest, NextApiResponse } from 'next'
import FormData from 'form-data'
import { fetch } from '@/lib/isomorphic'
import { debug, fetch } from '@/lib/isomorphic'
import { KBlobRequest } from '@/lib/bots/bing/types'
import { createHeaders } from '@/lib/utils'

const API_DOMAIN = 'https://bing.vcanbb.top'
const API_DOMAIN = 'https://www.bing.com'

export const config = {
api: {
Expand All @@ -18,6 +19,7 @@ export const config = {
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { knowledgeRequest, imageBase64 } = req.body as KBlobRequest
const headers = createHeaders(req.cookies, 'image')

const formData = new FormData()
formData.append('knowledgeRequest', JSON.stringify(knowledgeRequest))
Expand All @@ -30,23 +32,24 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
method: 'POST',
body: formData.getBuffer(),
headers: {
"sec-ch-ua": "\"Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"115\", \"Chromium\";v=\"115\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"Referer": `${API_DOMAIN}/web/index.html`,
"Referrer-Policy": "origin-when-cross-origin",
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
'x-forward-for': headers['x-forwarded-for'],
'user-agent': headers['User-Agent'],
cookie: headers['cookie'],
'Referer': 'https://www.bing.com/search',
...formData.getHeaders()
}
}
).then(res => res.text())
)

if (response.status !== 200) {
throw new Error('图片上传失败')
}
res.writeHead(200, {
'Content-Type': 'application/json',
})
res.end(response || JSON.stringify({ result: { value: 'UploadFailed', message: '请更换 IP 或代理后重试' } }))
res.end(await response.text())
} catch (e) {
return res.json({
res.json({
result: {
value: 'UploadFailed',
message: `${e}`
Expand Down
Loading

0 comments on commit 39ddf96

Please sign in to comment.