Spaces:
Running
Running
/** | |
Copyright 2024 Google LLC | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
import React, { useEffect, useRef, useState } from "react"; | |
import './App.scss'; | |
import { LiveAPIProvider, useLiveAPIContext } from "./contexts/LiveAPIContext"; | |
import ControlTray from "./components/control-tray/ControlTray"; | |
import { IOSModal } from "./components/ios-modal/IOSModal"; | |
import { isIOS } from "./lib/platform"; | |
import cn from "classnames"; | |
import { LiveConfig } from "./multimodal-live-types"; | |
// import { Altair } from "./components/altair/Altair"; // اگر چت جداگانه ندارید، لازم نیست | |
// import SidePanel from "./components/side-panel/SidePanel"; // اگر چت جداگانه ندارید، لازم نیست | |
const myCustomInstruction = ` | |
ت1. هویت دستیار: | |
فقط خود را به عنوان "دستیار صوتی و تصویری اپلیکیشن زبانفلای" معرفی کن. | |
هرگز خود را محصول یا زیرمجموعه گوگل، اپل یا هیچ شرکت دیگری معرفی نکن. | |
در پاسخ به سؤال کاربر درباره هویت، دقیقاً بگو: | |
"من دستیار صوتی و تصویری اپلیکیشن زبانفلای هستم. چگونه میتوانم به شما کمک کنم؟" | |
مدیریت زبان گفتگو: | |
به طور پیشفرض، مکالمه را به زبان انگلیسی آغاز کن. | |
اگر کاربر با زبانی غیر از انگلیسی صحبت کرد، گفتگو را به همان زبان ادامه بده. | |
زبان پاسخ را همیشه مطابق زبان کاربر تنظیم کن. تغییر زبان به صورت خودکار انجام شود. | |
بیان اعداد: | |
هنگام مکالمه به انگلیسی، اعداد را به صورت کامل به انگلیسی بیان کن (مثلاً: twenty-five). | |
هنگام مکالمه به زبانی دیگر، اعداد را به همان زبان بیان کن (مثلاً: بیست و پنج). | |
تنظیم صدا و لحن: | |
با صدای مایل به بلند و شفاف صحبت کن. | |
در صورت درخواست کاربر، صدای خود را بلندتر، آرامتر یا به تقلید از صدای خواستهشده تغییر بده. | |
بدون استثنا، هرگونه تغییر صدای درخواستی کاربر را اجرا کن. | |
تعهد به پاسخگویی: | |
تحت هیچ شرایطی به کاربر نگو که "نمیتوانم پاسخ دهم" یا "نمیتوانم کمک کنم". | |
همیشه در تمام زمینهها تلاش کن بهترین پاسخ ممکن را به کاربر ارائه بدهی. | |
رفتار در شروع گفتگو: | |
اگر پیام ورودی "START_GREETING" دریافت شد، تنها با یکی از جملات کوتاه زیر پاسخ بده: | |
"Hello, how can I help you?" | |
یا "Hey, what's up?" | |
اطلاعرسانی درباره اپلیکیشن زبانفلای: | |
در صورت نیاز کاربر، توضیح بده که اپلیکیشن زبانفلای در گوگل پلی و کافه بازار قابل دانلود است. | |
راهنمایی کن که کاربران میتوانند با جستجو در این فروشگاهها، اپلیکیشن را نصب کنند. | |
پشتیبانی از یادگیری زبان: | |
هنگام پاسخگویی، در صورت مرتبط بودن، مثالهای جملهسازی، کاربرد واژگان و اصطلاحات ارائه بده. | |
در صورت درخواست کاربر، معانی واژگان، هممعنیها، متضادها، و نمونه جملهها را هم ارائه بده. | |
تمرین شنیداری و گفتاری: | |
اگر کاربر بخواهد، جملهای را بخوان و از او بخواه آن را تکرار کند (تمرین shadowing). | |
فرصت کامل برای تکرار بده و کاربر را با بازخورد مثبت تشویق کن. | |
شخصیسازی تجربه یادگیری: | |
در صورت درخواست کاربر، سرعت مکالمه را کند یا تند کن. | |
سطح سختی واژگان و جملات را بر اساس سطح کاربر (مبتدی، متوسط، پیشرفته) تنظیم کن. | |
**تعامل تصویری:** | |
- به تصویر زندهای که از کاربر دریافت میکنی توجه کن. | |
- اگر در تصویر نکته قابل توجهی وجود دارد (مانند حالت چهره، اشیاء خاص، یا محیط اطراف کاربر)، میتوانی به آن در مکالمه اشاره کنی، البته فقط اگر مرتبط با موضوع صحبت باشد یا کاربر از تو بخواهد. | |
- اگر کاربر سوالی در مورد چیزی که در تصویر میبیند پرسید، سعی کن بر اساس تصویر پاسخ دهی. | |
- هدف اصلی، کمک به یادگیری زبان است، پس تعامل تصویری باید در خدمت این هدف باشد. | |
`.trim(); | |
const initialAppConfig: LiveConfig = { | |
model: "models/gemini-2.0-flash-exp", | |
systemInstruction: { | |
parts: [{ text: myCustomInstruction }], | |
}, | |
}; | |
const SvgHumanIcon = () => ( | |
<svg width="70" height="70" viewBox="0 0 88 89" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M75.1481 81.6361H12.9259C9.66667 81.6361 7 78.9721 7 75.7161V58.5112C7 57.5862 7 57.1052 7.44444 56.2172C8.85185 52.9612 13 50.2232 19.4815 47.8922C24.1111 56.6982 33.3704 62.6921 44 62.6921C54.6296 62.6921 63.9259 56.6982 68.5185 47.8922C75 50.1862 79.1852 52.9982 80.5556 56.2172C81 56.6612 81 57.6232 81 58.5112V75.7161C81 78.9721 78.3333 81.6361 75.0741 81.6361H75.1481Z" stroke="currentColor" strokeWidth="6.42146" strokeLinecap="round" strokeLinejoin="round"/><path d="M44.0371 50.1862C33.8519 50.1862 25.5186 41.8612 25.5186 31.6863V26.1363C25.5186 15.9613 33.8519 7.63635 44.0371 7.63635C54.2223 7.63635 62.5556 15.9613 62.5556 26.1363V31.6863C62.5556 41.8612 54.2223 50.1862 44.0371 50.1862Z" stroke="currentColor" strokeWidth="6.42146" strokeLinecap="round" strokeLinejoin="round"/></svg> | |
); | |
const AppInternalLogic: React.FC<{ | |
isMicActive: boolean; | |
isCamActive: boolean; | |
setIsMicActive: React.Dispatch<React.SetStateAction<boolean>>; | |
setIsCamActive: React.Dispatch<React.SetStateAction<boolean>>; | |
createLogoFunction: (isMini: boolean, isActive: boolean, type?: 'human' | 'ai') => React.ReactNode; | |
videoRef: React.RefObject<HTMLVideoElement>; | |
notificationPopoverRef: React.RefObject<HTMLDivElement>; | |
notificationButtonRef: React.RefObject<HTMLButtonElement>; | |
isNotificationOpen: boolean; | |
setIsNotificationOpen: React.Dispatch<React.SetStateAction<boolean>>; | |
}> = ({ | |
isMicActive, | |
isCamActive, | |
setIsMicActive, | |
setIsCamActive, | |
createLogoFunction, | |
videoRef, | |
notificationPopoverRef, | |
notificationButtonRef, | |
isNotificationOpen, | |
setIsNotificationOpen | |
}) => { | |
const { connected, disconnect } = useLiveAPIContext(); | |
useEffect(() => { | |
if (!isMicActive && !isCamActive && connected) { | |
disconnect(); | |
} | |
}, [isMicActive, isCamActive, connected, disconnect]); | |
return ( | |
<div className="w-full flex flex-col items-center justify-center min-h-screen text-foreground antialiased"> | |
{/* Wrapper اصلی که در HTML هم وجود دارد */} | |
<div className="max-w-3xl w-full flex flex-col items-center justify-center h-full relative"> | |
{/* Header Controls */} | |
<div className="header-controls"> | |
{/* دکمه بازگشت (در HTML سمت راست بود، اینجا هم طبق آن) */} | |
<div className="header-button" onClick={() => alert('Back clicked (implement navigation)')}> | |
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="m15 18-6-6 6-6"></path></svg> | |
</div> | |
{/* دکمه نوتیفیکیشن (در HTML سمت چپ بود) */} | |
<button | |
ref={notificationButtonRef} | |
id="notification-button" | |
aria-label="Notifications" | |
className="header-button" | |
onClick={(e) => { | |
e.stopPropagation(); | |
setIsNotificationOpen(!isNotificationOpen); | |
}} | |
> | |
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="12"></line><line x1="12" y1="16" x2="12.01" y2="16"></line></svg> | |
</button> | |
</div> | |
{/* Notification Popover */} | |
<div ref={notificationPopoverRef} id="notification-popover-wrapper" className="notification-popover-wrapper"> | |
<div | |
id="notification-popover" | |
className={cn("popover-content", { | |
"open animate-popover-open-top-center": isNotificationOpen, | |
"animate-popover-close-top-center": !isNotificationOpen && document.getElementById('notification-popover')?.classList.contains('open'), | |
})} | |
> | |
<div className="notification-popover-text-content"> | |
مدلهای هوش مصنوعی میتوانند اشتباه کنند، صحت اطلاعات مهم را بررسی کنید و از وارد کردن اطلاعات حساس بپرهیزید. | |
</div> | |
</div> | |
</div> | |
{/* MediaToggle Area - این div اصلی برای ویدیو و لوگوی بزرگ است */} | |
{/* کلاسهای HTML مرجع: "w-full flex flex-col items-center justify-center h-[90dvh] bg-background top-0 left-0 relative" */} | |
{/* ما از bg-background روی body استفاده میکنیم، پس اینجا لازم نیست تکرار شود */} | |
{/* h-[90dvh] با flex-grow جایگزین میشود تا ارتفاع را پر کند */} | |
<div className="w-full flex flex-col items-center justify-center flex-grow relative"> | |
<video | |
id="video-feed" | |
ref={videoRef} | |
autoPlay | |
playsInline | |
className={cn( | |
"absolute top-0 left-0 w-full h-full object-cover scale-x-[-1]", // scale-x-[-1] برای آینهای کردن | |
{ "hidden": !isCamActive } | |
)} | |
/> | |
{/* large-logo-container */} | |
<div | |
id="large-logo-container" | |
className={cn( | |
// کلاسهای HTML: "w-full h-full absolute top-0 left-0 items-center justify-center" | |
// اما در کد JS، این کانتینر فقط زمانی flex است که لوگو نمایش داده میشود | |
"absolute top-0 left-0 w-full h-full", | |
{ | |
"flex items-center justify-center": isMicActive && !isCamActive, | |
"hidden": !isMicActive || isCamActive, | |
} | |
)} | |
> | |
{/* لوگوی بزرگ فقط زمانی رندر میشود که شرایط برقرار باشد */} | |
{isMicActive && !isCamActive && createLogoFunction(false, true, 'human')} | |
</div> | |
{/* اگر Altair/SidePanel وجود دارد، باید اینجا یا در یک لایه دیگر قرار گیرد */} | |
{/* برای سادگی، Altair را اینجا کامنت میکنیم چون در تصاویر شما نبود */} | |
{/* <div className="absolute bottom-24 left-4 z-20 w-1/3 max-w-md"> <Altair /> </div> */} | |
</div> | |
<ControlTray | |
videoRef={videoRef} | |
supportsVideo={true} | |
onVideoStreamChange={(stream) => { /* ... */ }} | |
isAppMicActive={isMicActive} | |
onAppMicToggle={setIsMicActive} | |
isAppCamActive={isCamActive} | |
onAppCamToggle={setIsCamActive} | |
createLogoFunction={createLogoFunction} | |
/> | |
</div> | |
</div> | |
); | |
} | |
function App() { | |
const videoRef = useRef<HTMLVideoElement>(null); | |
const [showIOSModal, setShowIOSModal] = useState(false); | |
const [isAllowedOrigin, setIsAllowedOrigin] = useState<boolean | null>(null); | |
const [isMicActive, setIsMicActive] = useState(false); | |
const [isCamActive, setIsCamActive] = useState(false); | |
const [isNotificationOpen, setIsNotificationOpen] = useState(false); | |
const notificationPopoverRef = useRef<HTMLDivElement>(null); | |
const notificationButtonRef = useRef<HTMLButtonElement>(null); | |
useEffect(() => { | |
if (isIOS()) { | |
setShowIOSModal(true); | |
} | |
const timer = setTimeout(() => { | |
setIsAllowedOrigin(true); | |
}, 100); | |
return () => clearTimeout(timer); | |
}, []); | |
useEffect(() => { | |
const handleClickOutside = (event: MouseEvent) => { | |
if ( | |
isNotificationOpen && | |
notificationPopoverRef.current && | |
!notificationPopoverRef.current.contains(event.target as Node) && | |
notificationButtonRef.current && | |
!notificationButtonRef.current.contains(event.target as Node) | |
) { | |
setIsNotificationOpen(false); | |
} | |
}; | |
document.addEventListener("mousedown", handleClickOutside); | |
return () => { | |
document.removeEventListener("mousedown", handleClickOutside); | |
}; | |
}, [isNotificationOpen]); | |
if (isAllowedOrigin === null) { | |
return <div style={{ padding: '20px', textAlign: 'center' }}>در حال بررسی دسترسی...</div>; | |
} | |
const createLogoFunction = (isMini: boolean, isActive: boolean, type: 'human' | 'ai' = 'human') => { | |
if (!isActive) return null; | |
const size = isMini ? 80 : 200; | |
const iconSize = isMini ? 35 : 70; // آیکون کوچکتر برای لوگوی کوچک | |
const insetBase = isMini | |
? { ping: 10, outer: 0, mid: 5, inner: 12, icon: 22 } // مقادیر از JS کد HTML | |
: { ping: 40, outer: 0, mid: 20, inner: 50, icon: 65 }; | |
const bgColorBase = type === 'human' ? 'blue' : 'green'; | |
// اطمینان از وجود کلاسهای Tailwind برای purgeCSS اگر لازم باشد (معمولاً لازم نیست اگر مستقیم استفاده شوند) | |
// <div className="bg-blue-200 dark:bg-blue-700 bg-blue-300 dark:bg-blue-600 bg-blue-400 dark:bg-blue-500 hidden"></div> | |
// <div className="bg-green-200 dark:bg-green-700 bg-green-300 dark:bg-green-600 bg-green-400 dark:bg-green-500 hidden"></div> | |
return ( | |
<div className="relative" style={{ width: `${size}px`, height: `${size}px` }}> | |
<div className={`absolute rounded-full opacity-50 animate-ping bg-${bgColorBase}-200 dark:bg-${bgColorBase}-700`} style={{ inset: `${insetBase.ping}px` }}></div> | |
<div className={`absolute inset-0 rounded-full opacity-50 bg-${bgColorBase}-200 dark:bg-${bgColorBase}-700`} style={{ inset: `${insetBase.outer}px` }}></div> | |
<div className={`absolute rounded-full opacity-50 bg-${bgColorBase}-300 dark:bg-${bgColorBase}-600`} style={{ inset: `${insetBase.mid}px` }}></div> | |
<div className={`absolute rounded-full opacity-50 bg-${bgColorBase}-400 dark:bg-${bgColorBase}-500`} style={{ inset: `${insetBase.inner}px` }}></div> | |
<div className="z-10 absolute" style={{ inset: `${insetBase.icon}px`, display: 'flex', alignItems: 'center', justifyContent: 'center' }}> | |
{/* آیکون انسان با استروک currentColor تا از var(--foreground) ارثبری کند */} | |
{type === 'human' && <SvgHumanIcon />} | |
</div> | |
</div> | |
); | |
}; | |
return ( | |
<LiveAPIProvider initialConfig={initialAppConfig}> | |
<AppInternalLogic | |
isMicActive={isMicActive} | |
setIsMicActive={setIsMicActive} | |
isCamActive={isCamActive} | |
setIsCamActive={setIsCamActive} | |
createLogoFunction={createLogoFunction} | |
videoRef={videoRef} | |
notificationPopoverRef={notificationPopoverRef} | |
notificationButtonRef={notificationButtonRef} | |
isNotificationOpen={isNotificationOpen} | |
setIsNotificationOpen={setIsNotificationOpen} | |
/> | |
<IOSModal isOpen={showIOSModal} onClose={() => setShowIOSModal(false)} /> | |
</LiveAPIProvider> | |
); | |
} | |
export default App; |