From 2efb9eeb09c36f69af2b7c1bd605d759375e1558 Mon Sep 17 00:00:00 2001 From: goddonebianu Date: Tue, 30 Sep 2025 18:16:27 +0800 Subject: [PATCH] ... --- src/app/api/ipa/route.ts | 39 +++++++++++++++++++++++++++++++++---- src/app/ipa-reader/page.tsx | 29 ++++++++++++++++++++++----- src/components/Button.tsx | 21 ++++++++++++++++++-- src/utils.ts | 32 ------------------------------ 4 files changed, 78 insertions(+), 43 deletions(-) diff --git a/src/app/api/ipa/route.ts b/src/app/api/ipa/route.ts index 5bb1668..de14362 100644 --- a/src/app/api/ipa/route.ts +++ b/src/app/api/ipa/route.ts @@ -1,14 +1,45 @@ -import { getIPA } from "@/utils"; +import { GoogleGenAI } from "@google/genai"; import { NextRequest, NextResponse } from "next/server"; +import { env } from "process"; + +const api_key = env.GEMINI_API_KEY; +const ai = new GoogleGenAI(api_key ? { apiKey: api_key } : {}); +const prompt = `[TEXT] +请推断以上文本的语言,并返回其宽式国际音标(IPA),以JSON格式 +如: +{ +"lang": "german", +"ipa": "[ˈɡuːtn̩ ˈtaːk]" +} +注意:直接返回json文本, +不要带markdown记号, +ipa一定要加[], +lang的值是小写英语的语言名称`; + +async function getIPAFromGemini(text: string) { + const response = await ai.models.generateContent({ + model: "gemini-2.5-flash", + contents: prompt.replace("[TEXT]", text), + }); + if (response.text === undefined) return null; + return JSON.parse(response.text); +} + +export async function ggetIPA(text: string): Promise<{ lang: string, ipa: string } | null> { + return { + lang: `(这是的${text}的lang)`, + ipa: `(这是的${text}的ipa)` + }; +} export async function GET(request: NextRequest) { const searchParams = request.nextUrl.searchParams; const text = searchParams.get('text'); - if (!text) return NextResponse.json({ 'error': 400 }, { status: 400 }); + if (!text) return NextResponse.json("查询参数错误", { status: 400 }); - const r = await getIPA(text); - if (r === null) return NextResponse.json({ 'error': 424 }, { status: 424 }); + const r = await getIPAFromGemini(text); + if (r === null) return NextResponse.json("Gemini Api请求失败", { status: 424 }); return NextResponse.json({ r }, { status: 200 }); } diff --git a/src/app/ipa-reader/page.tsx b/src/app/ipa-reader/page.tsx index a95ded8..b34e2d2 100644 --- a/src/app/ipa-reader/page.tsx +++ b/src/app/ipa-reader/page.tsx @@ -1,20 +1,38 @@ "use client"; import Button from "@/components/Button"; -import { getIPA, urlGoto } from "@/utils"; import { useRef, useState } from "react"; export default function Home() { const respref = useRef(null); const inputref = useRef(null); const [ipa_result, set_ipa_result] = useState<{ lang: string, ipa: string } | null>(null); + const [genaiEnabled, setGenaiEnabled] = useState(true); const generateIPA = () => { + if (!genaiEnabled) return; + setGenaiEnabled(false); + const text = inputref.current!.value.trim(); if (text.length === 0) return; - getIPA(text).then((result: { lang: string, ipa: string } | null) => { - set_ipa_result(result); - }); + + const params = new URLSearchParams({ text: text }); + fetch(`/api/ipa?${params}`) + .then(response => { + if (response.ok) { + return response.json().then((data) => { + set_ipa_result(data); + }); + } else { + return response.text() + .then(errorText => { + console.error(errorText); + }); + } + }) + .finally(() => { + setGenaiEnabled(true); + }); } const readIPA = () => { const text = inputref.current!.value.trim(); @@ -33,8 +51,9 @@ export default function Home() {
- + + {/* */}
语言:{ipa_result?.lang}{'\n'} diff --git a/src/components/Button.tsx b/src/components/Button.tsx index 9be270f..c2ec0ba 100644 --- a/src/components/Button.tsx +++ b/src/components/Button.tsx @@ -1,6 +1,23 @@ -export default function Button({ label, onClick, className }: { label: string, onClick?: () => void, className?: string }) { +export default function Button({ + label, + onClick, + className, + disabled +}: { + label: + string, onClick?: () => void, + className?: string, + disabled?: boolean +}) { return ( - ); diff --git a/src/utils.ts b/src/utils.ts index ba9425b..b0e4fcc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,3 @@ -import { GoogleGenAI } from "@google/genai"; -import { env } from "process"; - export function inspect(word: string) { const goto = (url: string) => { window.open(url, '_blank'); @@ -14,32 +11,3 @@ export function inspect(word: string) { export function urlGoto(url: string) { window.open(url, '_blank'); } - -const api_key = env.GEMINI_API_KEY; -const ai = new GoogleGenAI(api_key ? { apiKey: api_key } : {}); -const prompt = `[TEXT] -请推断以上文本的语言,并返回其宽式国际音标(IPA),以JSON格式 -如: -{ -"lang": "german", -"ipa": "[ˈɡuːtn̩ ˈtaːk]" -} -注意:直接返回json文本, -不要带markdown记号, -ipa一定要加[], -lang的值是小写英语的语言名称`; -export async function getIPA(text: string) { - const response = await ai.models.generateContent({ - model: "gemini-2.5-flash", - contents: prompt.replace("[TEXT]", text), - }); - if (response.text === undefined) return null; - return JSON.parse(response.text); -} - -export async function ggetIPA(text: string): Promise<{ lang: string, ipa: string } | null> { - return { - lang: `(这是的${text}的lang)`, - ipa: `(这是的${text}的ipa)` - }; -}