first
This commit is contained in:
		
							
								
								
									
										210
									
								
								src/app/helper.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										210
									
								
								src/app/helper.tsx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,210 @@
 | 
			
		||||
"use client"
 | 
			
		||||
import dynamic from 'next/dynamic';
 | 
			
		||||
import { useEffect, useRef, useState } from 'react';
 | 
			
		||||
import '@/deps/live2d.min.js'
 | 
			
		||||
import useVoice2Txt from "@/hooks/useVoice2txt";
 | 
			
		||||
import useTxt2Voice from '@/hooks/useTxt2Voice';
 | 
			
		||||
import axios from 'axios';
 | 
			
		||||
import * as PIXI from 'pixi.js';
 | 
			
		||||
import { Live2DModel } from 'pixi-live2d-display/cubism2';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//import "@/deps/live2dcubismcore.min.js"
 | 
			
		||||
export default function Home() {
 | 
			
		||||
    function draggable(model: any) {
 | 
			
		||||
        model.buttonMode = true;
 | 
			
		||||
        model.on("pointerdown", (e: any) => {
 | 
			
		||||
            model.dragging = true;
 | 
			
		||||
            model._pointerX = e.data.global.x - model.x;
 | 
			
		||||
            model._pointerY = e.data.global.y - model.y;
 | 
			
		||||
        });
 | 
			
		||||
        model.on("pointermove", (e: any) => {
 | 
			
		||||
            if (model.dragging) {
 | 
			
		||||
                model.position.x = e.data.global.x - model._pointerX;
 | 
			
		||||
                model.position.y = e.data.global.y - model._pointerY;
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        model.on("pointerupoutside", () => (model.dragging = false));
 | 
			
		||||
        model.on("pointerup", () => (model.dragging = false));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function addFrame(model: any) {
 | 
			
		||||
        const foreground = PIXI.Sprite.from(PIXI.Texture.WHITE);
 | 
			
		||||
        foreground.width = model.internalModel.width;
 | 
			
		||||
        foreground.height = model.internalModel.height;
 | 
			
		||||
        foreground.alpha = 0.2;
 | 
			
		||||
 | 
			
		||||
        model.addChild(foreground);
 | 
			
		||||
 | 
			
		||||
        checkbox("Model Frames", (checked: any) => (foreground.visible = checked));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function addHitAreaFrames(model: any) {
 | 
			
		||||
        try {
 | 
			
		||||
            //@ts-ignore
 | 
			
		||||
            const hitAreaFrames = new PIXI.live2d.HitAreaFrames();
 | 
			
		||||
 | 
			
		||||
            model.addChild(hitAreaFrames);
 | 
			
		||||
 | 
			
		||||
            checkbox("Hit Area Frames", (checked: any) => (hitAreaFrames.visible = checked));
 | 
			
		||||
        } catch (err) {
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function checkbox(name: any, onChange: any) {
 | 
			
		||||
        const id = name.replace(/\W/g, "").toLowerCase();
 | 
			
		||||
 | 
			
		||||
        let checkbox = document.getElementById(id);
 | 
			
		||||
 | 
			
		||||
        if (!checkbox) {
 | 
			
		||||
            const p = document.createElement("p")!;
 | 
			
		||||
            p.innerHTML = `<input type="checkbox" id="${id}"> <label for="${id}">${name}</label>`;
 | 
			
		||||
 | 
			
		||||
            document!.getElementById("control")!.appendChild(p);
 | 
			
		||||
            checkbox = p.firstChild as HTMLElement;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        checkbox.addEventListener("change", () => {
 | 
			
		||||
            //@ts-ignore
 | 
			
		||||
            onChange(checkbox.checked);
 | 
			
		||||
        });
 | 
			
		||||
        //@ts-ignore
 | 
			
		||||
        onChange(checkbox.checked);
 | 
			
		||||
    }
 | 
			
		||||
    const send = (inputText: string) => {
 | 
			
		||||
        if (!inputText) return;
 | 
			
		||||
        console.log(inputText)
 | 
			
		||||
        let data = JSON.stringify({
 | 
			
		||||
            "messages": [
 | 
			
		||||
                {
 | 
			
		||||
                    "content": `回答用户的问题,尽可能简短。`,
 | 
			
		||||
                    "role": "system"
 | 
			
		||||
                },
 | 
			
		||||
                {
 | 
			
		||||
                    "content": inputText,
 | 
			
		||||
                    "role": "user"
 | 
			
		||||
                }
 | 
			
		||||
            ],
 | 
			
		||||
            "model": "deepseek-chat",
 | 
			
		||||
            "frequency_penalty": 0,
 | 
			
		||||
            "max_tokens": 2048,
 | 
			
		||||
            "presence_penalty": 0,
 | 
			
		||||
            "stop": null,
 | 
			
		||||
            "stream": false,
 | 
			
		||||
            "temperature": 1,
 | 
			
		||||
            "top_p": 1
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        let config = {
 | 
			
		||||
            method: 'post',
 | 
			
		||||
            maxBodyLength: Infinity,
 | 
			
		||||
            url: 'https://api.deepseek.com/chat/completions',
 | 
			
		||||
            headers: {
 | 
			
		||||
                'Content-Type': 'application/json',
 | 
			
		||||
                'Accept': 'application/json',
 | 
			
		||||
                'Authorization': 'Bearer sk-dd24ae704e8d4939aeed8f050d04d36b'
 | 
			
		||||
            },
 | 
			
		||||
            data: data
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        axios(config)
 | 
			
		||||
            .then((response) => {
 | 
			
		||||
                console.log(`response`, response);
 | 
			
		||||
                console.log(response.data);
 | 
			
		||||
                speak(response.data.choices[0].message.content);
 | 
			
		||||
                setResponse(response.data.choices[0].message.content);
 | 
			
		||||
            })
 | 
			
		||||
            .catch((error) => {
 | 
			
		||||
                console.log(error);
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const { start, end, text, isListening } = useVoice2Txt({
 | 
			
		||||
        lang: 'cmn-Hans-CN',
 | 
			
		||||
        continuous: false
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    const {
 | 
			
		||||
        isSpeaking,
 | 
			
		||||
        speak,
 | 
			
		||||
        stop
 | 
			
		||||
    } = useTxt2Voice();
 | 
			
		||||
    const [inputText, setInputText] = useState("");
 | 
			
		||||
    const isMouthOpen = useRef(false);
 | 
			
		||||
    const [response, setResponse] = useState("");
 | 
			
		||||
    useEffect(() => {
 | 
			
		||||
        console.log(text)
 | 
			
		||||
        if (!text) return;
 | 
			
		||||
        send(text);
 | 
			
		||||
    }, [text]);
 | 
			
		||||
    const [model, setModel] = useState<Live2DModel>();
 | 
			
		||||
    useEffect(() => {
 | 
			
		||||
        if (!isSpeaking) {
 | 
			
		||||
            isMouthOpen.current = false;
 | 
			
		||||
        }
 | 
			
		||||
    }, [isSpeaking]);
 | 
			
		||||
 | 
			
		||||
    useEffect(() => {
 | 
			
		||||
        if (!isListening && !isSpeaking) { }
 | 
			
		||||
    }, [isListening]);
 | 
			
		||||
 | 
			
		||||
    useEffect(() => {
 | 
			
		||||
        // expose PIXI to window so that this plugin is able to
 | 
			
		||||
        // reference window.PIXI.Ticker to automatically update Live2D models
 | 
			
		||||
        //@ts-ignore
 | 
			
		||||
        typeof window !== 'undefined' && (window.PIXI = PIXI);
 | 
			
		||||
        (async function () {
 | 
			
		||||
            const app = new PIXI.Application({
 | 
			
		||||
                view: document.getElementById('canvas') as HTMLCanvasElement,
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            const model = await Live2DModel.from('https://cdn.jsdelivr.net/gh/guansss/pixi-live2d-display/test/assets/shizuku/shizuku.model.json');
 | 
			
		||||
 | 
			
		||||
            app.stage.addChild(model);
 | 
			
		||||
            const scaleX = (innerWidth * 0.4) / model.width;
 | 
			
		||||
            const scaleY = (innerHeight * 0.8) / model.height;
 | 
			
		||||
 | 
			
		||||
            // fit the window
 | 
			
		||||
            model.scale.set(0.3);
 | 
			
		||||
 | 
			
		||||
            model.y = innerHeight * 0.1;
 | 
			
		||||
 | 
			
		||||
            draggable(model);
 | 
			
		||||
            addFrame(model);
 | 
			
		||||
            addHitAreaFrames(model);
 | 
			
		||||
            setModel(model);
 | 
			
		||||
            model.on('hit', (hitAreas) => {
 | 
			
		||||
                if (hitAreas.includes('body')) {
 | 
			
		||||
                    model.motion('tap_body');
 | 
			
		||||
                    model.motion('speak')
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        })();
 | 
			
		||||
    }, [])
 | 
			
		||||
    return (
 | 
			
		||||
 | 
			
		||||
        <main>
 | 
			
		||||
            {
 | 
			
		||||
                typeof window !== 'undefined'
 | 
			
		||||
                && typeof window.Live2D !== 'undefined'
 | 
			
		||||
                && (<div className='flex w-full flex-col h-full items-center justify-center relative'>
 | 
			
		||||
                    <canvas className='w-[40%] h-[40%]' id="canvas"></canvas>
 | 
			
		||||
                    <div className='absolute right-[20vw] top-4 bg-white rounded w-[20vw] h-auto text-sm text-black'>{response ? response : "请输入文字和我聊天吧"}</div>
 | 
			
		||||
                    <div id="control"></div>
 | 
			
		||||
                    <button onClick={start}>语音输入</button>
 | 
			
		||||
                    <button onClick={end}>结束语音输入</button>
 | 
			
		||||
                    <input className='text-black' value={inputText} onChange={(e) => {
 | 
			
		||||
                        setInputText(e.target.value);
 | 
			
		||||
                        console.log(e.target.value)
 | 
			
		||||
                    }}></input>
 | 
			
		||||
                    <button onClick={() => {
 | 
			
		||||
                        send(inputText);
 | 
			
		||||
                        setInputText("");
 | 
			
		||||
                    }}>发送</button>
 | 
			
		||||
                </div>)
 | 
			
		||||
            }
 | 
			
		||||
        </main>
 | 
			
		||||
    );
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +1,6 @@
 | 
			
		||||
import type { Metadata } from "next";
 | 
			
		||||
import { Inter } from "next/font/google";
 | 
			
		||||
import "./globals.css";
 | 
			
		||||
 | 
			
		||||
const inter = Inter({ subsets: ["latin"] });
 | 
			
		||||
 | 
			
		||||
export const metadata: Metadata = {
 | 
			
		||||
  title: "Create Next App",
 | 
			
		||||
@@ -16,7 +14,10 @@ export default function RootLayout({
 | 
			
		||||
}>) {
 | 
			
		||||
  return (
 | 
			
		||||
    <html lang="en">
 | 
			
		||||
      <body className={inter.className}>{children}</body>
 | 
			
		||||
      <head>
 | 
			
		||||
        {/* <script src="http://publicjs.supmiao.com/live2dcubismcore.min.js"></script> */}
 | 
			
		||||
      </head>
 | 
			
		||||
      <body>{children}</body>
 | 
			
		||||
    </html>
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										120
									
								
								src/app/page.tsx
									
									
									
									
									
								
							
							
						
						
									
										120
									
								
								src/app/page.tsx
									
									
									
									
									
								
							@@ -1,113 +1,9 @@
 | 
			
		||||
import Image from "next/image";
 | 
			
		||||
 | 
			
		||||
"use client"
 | 
			
		||||
import dynamic from "next/dynamic";
 | 
			
		||||
const Page = dynamic(() => import('./helper'), {
 | 
			
		||||
  ssr: false
 | 
			
		||||
})
 | 
			
		||||
//import "@/deps/live2dcubismcore.min.js"
 | 
			
		||||
export default function Home() {
 | 
			
		||||
  return (
 | 
			
		||||
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
 | 
			
		||||
      <div className="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex">
 | 
			
		||||
        <p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto  lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
 | 
			
		||||
          Get started by editing 
 | 
			
		||||
          <code className="font-mono font-bold">src/app/page.tsx</code>
 | 
			
		||||
        </p>
 | 
			
		||||
        <div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:size-auto lg:bg-none">
 | 
			
		||||
          <a
 | 
			
		||||
            className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0"
 | 
			
		||||
            href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
 | 
			
		||||
            target="_blank"
 | 
			
		||||
            rel="noopener noreferrer"
 | 
			
		||||
          >
 | 
			
		||||
            By{" "}
 | 
			
		||||
            <Image
 | 
			
		||||
              src="/vercel.svg"
 | 
			
		||||
              alt="Vercel Logo"
 | 
			
		||||
              className="dark:invert"
 | 
			
		||||
              width={100}
 | 
			
		||||
              height={24}
 | 
			
		||||
              priority
 | 
			
		||||
            />
 | 
			
		||||
          </a>
 | 
			
		||||
        </div>
 | 
			
		||||
      </div>
 | 
			
		||||
 | 
			
		||||
      <div className="relative z-[-1] flex place-items-center before:absolute before:h-[300px] before:w-full before:-translate-x-1/2 before:rounded-full before:bg-gradient-radial before:from-white before:to-transparent before:blur-2xl before:content-[''] after:absolute after:-z-20 after:h-[180px] after:w-full after:translate-x-1/3 after:bg-gradient-conic after:from-sky-200 after:via-blue-200 after:blur-2xl after:content-[''] before:dark:bg-gradient-to-br before:dark:from-transparent before:dark:to-blue-700 before:dark:opacity-10 after:dark:from-sky-900 after:dark:via-[#0141ff] after:dark:opacity-40 sm:before:w-[480px] sm:after:w-[240px] before:lg:h-[360px]">
 | 
			
		||||
        <Image
 | 
			
		||||
          className="relative dark:drop-shadow-[0_0_0.3rem_#ffffff70] dark:invert"
 | 
			
		||||
          src="/next.svg"
 | 
			
		||||
          alt="Next.js Logo"
 | 
			
		||||
          width={180}
 | 
			
		||||
          height={37}
 | 
			
		||||
          priority
 | 
			
		||||
        />
 | 
			
		||||
      </div>
 | 
			
		||||
 | 
			
		||||
      <div className="mb-32 grid text-center lg:mb-0 lg:w-full lg:max-w-5xl lg:grid-cols-4 lg:text-left">
 | 
			
		||||
        <a
 | 
			
		||||
          href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
 | 
			
		||||
          className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
 | 
			
		||||
          target="_blank"
 | 
			
		||||
          rel="noopener noreferrer"
 | 
			
		||||
        >
 | 
			
		||||
          <h2 className="mb-3 text-2xl font-semibold">
 | 
			
		||||
            Docs{" "}
 | 
			
		||||
            <span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
 | 
			
		||||
              ->
 | 
			
		||||
            </span>
 | 
			
		||||
          </h2>
 | 
			
		||||
          <p className="m-0 max-w-[30ch] text-sm opacity-50">
 | 
			
		||||
            Find in-depth information about Next.js features and API.
 | 
			
		||||
          </p>
 | 
			
		||||
        </a>
 | 
			
		||||
 | 
			
		||||
        <a
 | 
			
		||||
          href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
 | 
			
		||||
          className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
 | 
			
		||||
          target="_blank"
 | 
			
		||||
          rel="noopener noreferrer"
 | 
			
		||||
        >
 | 
			
		||||
          <h2 className="mb-3 text-2xl font-semibold">
 | 
			
		||||
            Learn{" "}
 | 
			
		||||
            <span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
 | 
			
		||||
              ->
 | 
			
		||||
            </span>
 | 
			
		||||
          </h2>
 | 
			
		||||
          <p className="m-0 max-w-[30ch] text-sm opacity-50">
 | 
			
		||||
            Learn about Next.js in an interactive course with quizzes!
 | 
			
		||||
          </p>
 | 
			
		||||
        </a>
 | 
			
		||||
 | 
			
		||||
        <a
 | 
			
		||||
          href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
 | 
			
		||||
          className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
 | 
			
		||||
          target="_blank"
 | 
			
		||||
          rel="noopener noreferrer"
 | 
			
		||||
        >
 | 
			
		||||
          <h2 className="mb-3 text-2xl font-semibold">
 | 
			
		||||
            Templates{" "}
 | 
			
		||||
            <span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
 | 
			
		||||
              ->
 | 
			
		||||
            </span>
 | 
			
		||||
          </h2>
 | 
			
		||||
          <p className="m-0 max-w-[30ch] text-sm opacity-50">
 | 
			
		||||
            Explore starter templates for Next.js.
 | 
			
		||||
          </p>
 | 
			
		||||
        </a>
 | 
			
		||||
 | 
			
		||||
        <a
 | 
			
		||||
          href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
 | 
			
		||||
          className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
 | 
			
		||||
          target="_blank"
 | 
			
		||||
          rel="noopener noreferrer"
 | 
			
		||||
        >
 | 
			
		||||
          <h2 className="mb-3 text-2xl font-semibold">
 | 
			
		||||
            Deploy{" "}
 | 
			
		||||
            <span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
 | 
			
		||||
              ->
 | 
			
		||||
            </span>
 | 
			
		||||
          </h2>
 | 
			
		||||
          <p className="m-0 max-w-[30ch] text-balance text-sm opacity-50">
 | 
			
		||||
            Instantly deploy your Next.js site to a shareable URL with Vercel.
 | 
			
		||||
          </p>
 | 
			
		||||
        </a>
 | 
			
		||||
      </div>
 | 
			
		||||
    </main>
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
  return <Page />
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user