Spaces:
Running
Running
Download index.html from FYRGPT/FYRStudyGPT: direct link, hf CLI and curl.
- Browser
- Download file 64.9 kB
-
https://huggingface.co/spaces/FYRGPT/FYRStudyGPT/resolve/main/index.html
- Command line
-
hf download hf://spaces/FYRGPT/FYRStudyGPT/index.html
-
curl -L -o index.html https://huggingface.co/spaces/FYRGPT/FYRStudyGPT/resolve/main/index.html
64.9 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
>
<title>FYRStudyGPT</title>
<link
rel="stylesheet"
href="style.css"
>
</head>
<body>
<!-- ==================================================
PICKER
================================================== -->
<div
id="pickerBackdrop"
class="picker-backdrop"
>
<div
class="picker-panel"
role="dialog"
aria-modal="true"
>
<div class="picker-header">
<div class="picker-header-text">
<div
id="pickerTitle"
class="picker-title"
>
Choose
</div>
<div
id="pickerSubtitle"
class="picker-subtitle"
></div>
</div>
<button
id="pickerClose"
class="picker-close"
type="button"
>
×
</button>
</div>
<div
id="pickerOptions"
class="picker-options"
></div>
</div>
</div>
<!-- ==================================================
APP
================================================== -->
<div class="app">
<!-- ==================================================
SIDEBAR
================================================== -->
<aside class="sidebar">
<div class="brand-row">
<div class="brand-icon">
<div class="equalizer">
<span></span>
<span></span>
<span></span>
</div>
</div>
<div class="brand-text">
<div class="brand-name">
FYRStudyGPT
</div>
<div class="brand-tagline">
YOUR THINKING
<br>
PARTNER
</div>
</div>
<button
id="closeSidebar"
class="brand-close"
type="button"
>
×
</button>
</div>
<button
id="newChat"
class="new-chat"
type="button"
>
<span class="new-plus">
+
</span>
New chat
<span class="key-badge">
N
</span>
</button>
<div class="sidebar-heading">
This space
</div>
<div class="session-card">
<div class="session-top">
<span class="session-spark">
✧
</span>
<span class="session-title">
Current session
</span>
<span class="session-dot"></span>
</div>
<div
id="sessionStatus"
class="session-sub"
>
Ready to begin
</div>
</div>
<button
id="subjectSidebarButton"
class="side-setting"
type="button"
>
<span class="side-setting-icon">
▤
</span>
<span class="side-setting-text">
<span class="side-setting-title">
Subject focus
</span>
<span
id="subjectSidebarValue"
class="side-setting-value"
>
Mathematics
</span>
</span>
<span class="side-chevron">
›
</span>
</button>
<button
id="modeSidebarButton"
class="side-setting"
type="button"
>
<span class="side-setting-icon">
♧
</span>
<span class="side-setting-text">
<span class="side-setting-title">
Study mode
</span>
<span
id="modeSidebarValue"
class="side-setting-value"
>
Explain Topic
</span>
</span>
<span class="side-chevron">
›
</span>
</button>
<button
id="modelSidebarButton"
class="side-setting"
type="button"
>
<span class="side-setting-icon">
◇
</span>
<span class="side-setting-text">
<span class="side-setting-title">
AI model
</span>
<span
id="modelSidebarValue"
class="side-setting-value"
>
Fast · SmolLM2 135M
</span>
</span>
<span class="side-chevron">
›
</span>
</button>
<div class="sidebar-divider"></div>
<div class="sidebar-heading">
Recent
</div>
<div
id="chatList"
class="chat-list"
></div>
<div
id="recentEmpty"
class="recent-empty"
>
Your conversations will appear here
as you study.
</div>
<div class="sidebar-bottom">
<div class="learn-card">
<div class="learn-icon">
♢
</div>
<div class="learn-text">
<div class="learn-title">
Learn, don't just copy
</div>
<div class="learn-sub">
Hints and steps before final answers.
</div>
</div>
</div>
<div class="appearance-row">
<div class="appearance-icon">
☼
</div>
<div class="appearance-info">
<div class="appearance-title">
Appearance
</div>
<div
id="themeLabel"
class="appearance-sub"
>
Light theme
</div>
</div>
<button
id="themeButton"
class="theme-button"
type="button"
>
☼
</button>
</div>
</div>
</aside>
<!-- ==================================================
MAIN
================================================== -->
<main class="main">
<header class="topbar">
<button
id="menuButton"
class="menu-button"
type="button"
>
☰
</button>
<div class="breadcrumb">
<span>
Study space
</span>
<span class="breadcrumb-slash">
/
</span>
<button
id="breadcrumbSubject"
class="breadcrumb-subject"
type="button"
>
Math
</button>
</div>
<div class="top-actions">
<button
id="focusButton"
class="focus-button"
type="button"
>
<span class="focus-dot"></span>
<span>
Focus mode
</span>
</button>
<button
id="moonButton"
class="moon-button"
type="button"
>
☾
</button>
</div>
</header>
<div
id="workspace"
class="workspace"
>
<div class="content">
<!-- ==================================================
HERO
================================================== -->
<section
id="hero"
class="hero"
>
<div class="hero-icon-wrap">
<span class="orbit-dot one"></span>
<span class="orbit-dot two"></span>
<div class="hero-icon">
♢
</div>
</div>
<div class="hero-kicker">
A CALMER WAY TO REVISE
</div>
<h1>
What would you like to understand?
</h1>
<p class="hero-subtitle">
Ask a question, test your thinking,
or make a plan for the next small step.
</p>
<div class="ai-intro">
<div class="ai-avatar">
◫
</div>
<div>
<div class="ai-name-row">
<span class="ai-name">
FYRStudyGPT
</span>
<span
id="introMode"
class="ai-mode-tag"
>
TUTOR MODE
</span>
</div>
<div class="ai-intro-text">
Hi, I'm FYRStudyGPT. I'm here to help you
understand the why, practise the how,
and feel a little more ready for what
comes next.
</div>
<div class="ai-intro-text">
Choose a subject and a mode, or tell me
what you're working on. We can take it
one step at a time.
</div>
</div>
</div>
</section>
<!-- ==================================================
CHAT
================================================== -->
<section
id="chatThread"
class="chat-thread"
></section>
<!-- ==================================================
PRE CHAT OPTIONS
================================================== -->
<section
id="preChatControls"
class="pre-chat-controls"
>
<div class="focus-heading">
Your focus
<span>
Set the context first
</span>
</div>
<div class="focus-grid">
<button
id="subjectPickerButton"
class="focus-picker"
type="button"
>
<span class="focus-picker-icon">
▤
</span>
<span class="focus-picker-text">
<span class="focus-picker-label">
Subject
</span>
<span
id="subjectPickerValue"
class="focus-picker-value"
>
Mathematics
</span>
</span>
<span class="focus-picker-chevron">
›
</span>
</button>
<button
id="modePickerButton"
class="focus-picker"
type="button"
>
<span class="focus-picker-icon">
♧
</span>
<span class="focus-picker-text">
<span class="focus-picker-label">
Study mode
</span>
<span
id="modePickerValue"
class="focus-picker-value"
>
Explain Topic
</span>
</span>
<span class="focus-picker-chevron">
›
</span>
</button>
</div>
<div class="suggestion-heading">
<span>
Try starting with
</span>
<span>
MAKE A TRICKY IDEA CLICK
</span>
</div>
<div
id="suggestions"
class="suggestions"
></div>
</section>
<!-- ==================================================
COMPOSER
================================================== -->
<section class="composer-area">
<div class="composer">
<textarea
id="messageInput"
rows="2"
placeholder="Ask about mathematics..."
></textarea>
<div class="composer-bottom">
<div class="composer-hint">
<span>
♧
</span>
Shift + Enter for a new line
</div>
<button
id="sendButton"
class="send-button"
type="button"
>
◇
</button>
</div>
</div>
<div
id="loadingBox"
class="loading-box"
>
<div class="loading-top">
<span
id="loadingName"
class="loading-name"
>
Preparing FYRStudyGPT…
</span>
<span
id="loadingDetail"
class="loading-detail"
>
0%
</span>
</div>
<div class="loading-track">
<div
id="loadingFill"
class="loading-fill"
></div>
</div>
</div>
<div
id="errorBox"
class="error-box"
></div>
<div class="footer-note">
FYRStudyGPT helps you learn.
Check important work with your teacher
or textbook.
</div>
</section>
</div>
</div>
</main>
</div>
<script>
/* ==================================================
TRANSFORMERS.JS
Loaded only when AI is needed.
This means the website itself still works
even if the AI library fails to load.
================================================== */
let transformersPipeline =
null;
let transformersEnv =
null;
async function ensureTransformers() {
if (
transformersPipeline
) {
return;
}
const module =
await import(
"https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1"
);
transformersPipeline =
module.pipeline;
transformersEnv =
module.env;
transformersEnv.allowLocalModels =
false;
transformersEnv.useBrowserCache =
true;
}
/* ==================================================
MODELS
================================================== */
const MODELS = {
fast: {
id:
"onnx-community/SmolLM2-135M-Instruct-ONNX-MHA",
label:
"Fast",
short:
"SmolLM2 135M",
tokens:
180,
gpuDtype:
"q4f16",
cpuDtype:
"q4"
},
balanced: {
id:
"onnx-community/SmolLM2-360M-Instruct-ONNX",
label:
"Balanced",
short:
"SmolLM2 360M",
tokens:
240,
gpuDtype:
"q4f16",
cpuDtype:
"q4"
},
smart: {
id:
"onnx-community/Qwen2.5-0.5B-Instruct",
label:
"Smart",
short:
"Qwen2.5 0.5B",
tokens:
300,
gpuDtype:
"q4f16",
cpuDtype:
"q4"
}
};
/* ==================================================
SUBJECTS
================================================== */
const SUBJECTS = [
{
value:
"Mathematics",
icon:
"➗",
description:
"Algebra, geometry and problem solving"
},
{
value:
"Science",
icon:
"⚗",
description:
"Biology, chemistry and physics"
},
{
value:
"English",
icon:
"✎",
description:
"Writing, comprehension and language"
},
{
value:
"Chinese",
icon:
"文",
description:
"Chinese language and comprehension"
},
{
value:
"History",
icon:
"⌛",
description:
"Events, sources and historical reasoning"
},
{
value:
"Geography",
icon:
"◎",
description:
"Places, environments and processes"
},
{
value:
"General",
icon:
"✦",
description:
"Anything outside a specific subject"
}
];
/* ==================================================
MODES
================================================== */
const MODES = [
{
value:
"Explain Topic",
icon:
"💡",
description:
"Understand an idea step by step"
},
{
value:
"Ask Anything",
icon:
"💬",
description:
"Normal conversation with your tutor"
},
{
value:
"Quiz Me",
icon:
"✓",
description:
"Test yourself and get answers marked"
},
{
value:
"Flashcards",
icon:
"▣",
description:
"Create interactive revision cards"
},
{
value:
"Study Plan",
icon:
"◷",
description:
"Create a realistic revision schedule"
},
{
value:
"Check Answer",
icon:
"✎",
description:
"Get feedback on your own answer"
}
];
/* ==================================================
MODEL PICKER
================================================== */
const MODEL_OPTIONS = [
{
value:
"fast",
icon:
"⚡",
name:
"Fast",
description:
"SmolLM2 135M · fastest and lightest"
},
{
value:
"balanced",
icon:
"⚖",
name:
"Balanced",
description:
"SmolLM2 360M · stronger answers"
},
{
value:
"smart",
icon:
"✦",
name:
"Smart",
description:
"Qwen2.5 0.5B · strongest but largest"
}
];
/* ==================================================
MODE UI
================================================== */
const MODE_DATA = {
"Explain Topic": {
tag:
"TUTOR MODE",
placeholder:
"Ask about {subject}...",
suggestions: [
"Explain photosynthesis simply",
"Why does the quadratic formula work?",
"Help me understand a difficult concept"
]
},
"Ask Anything": {
tag:
"ASK MODE",
placeholder:
"Ask FYRStudyGPT anything...",
suggestions: [
"Explain something from today's lesson",
"Can you put this in simpler words?",
"Give me an example so I understand it"
]
},
"Quiz Me": {
tag:
"QUIZ MODE",
placeholder:
"What should I quiz you on?",
suggestions: [
"Quiz me on algebra",
"Test me on cells and respiration",
"Give me a short history quiz"
]
},
"Flashcards": {
tag:
"FLASHCARD MODE",
placeholder:
"What should I make flashcards about?",
suggestions: [
"Make photosynthesis flashcards",
"Create algebra revision cards",
"Make history key-date flashcards"
]
},
"Study Plan": {
tag:
"PLANNER MODE",
placeholder:
"What do you need to revise?",
suggestions: [
"Make me a 1 hour revision plan",
"Plan my maths and science revision",
"Help me revise three topics tonight"
]
},
"Check Answer": {
tag:
"FEEDBACK MODE",
placeholder:
"Paste your question and answer...",
suggestions: [
"Check my maths working",
"Give feedback on my history response",
"Tell me how I can improve my answer"
]
}
};
/* ==================================================
STORAGE
If you previously had a login profile,
FYRStudyGPT keeps using it internally so
your old chats remain available.
Otherwise it uses "default".
================================================== */
const oldProfile =
localStorage.getItem(
"studyai.lastProfile"
);
const STORAGE_PROFILE =
(
oldProfile
||
"default"
)
.toLowerCase()
.replace(
/[^a-z0-9_-]/g,
"_"
);
function chatStorageKey() {
return (
"studyai.chats."
+
STORAGE_PROFILE
);
}
function modelStorageKey() {
return (
"studyai.model."
+
STORAGE_PROFILE
);
}
/* ==================================================
STATE
================================================== */
let chats =
[];
let currentChatId =
null;
let subject =
"Mathematics";
let mode =
"Explain Topic";
let selectedModelKey =
"fast";
let generator =
null;
let loadingPromise =
null;
let modelRuntime =
"";
let history =
[];
let activeQuiz =
null;
let activePicker =
null;
/* ==================================================
ELEMENTS
================================================== */
const workspace =
document.getElementById(
"workspace"
);
const hero =
document.getElementById(
"hero"
);
const chatThread =
document.getElementById(
"chatThread"
);
const chatList =
document.getElementById(
"chatList"
);
const recentEmpty =
document.getElementById(
"recentEmpty"
);
const messageInput =
document.getElementById(
"messageInput"
);
const sendButton =
document.getElementById(
"sendButton"
);
const loadingBox =
document.getElementById(
"loadingBox"
);
const loadingName =
document.getElementById(
"loadingName"
);
const loadingDetail =
document.getElementById(
"loadingDetail"
);
const loadingFill =
document.getElementById(
"loadingFill"
);
const errorBox =
document.getElementById(
"errorBox"
);
const pickerBackdrop =
document.getElementById(
"pickerBackdrop"
);
const pickerTitle =
document.getElementById(
"pickerTitle"
);
const pickerSubtitle =
document.getElementById(
"pickerSubtitle"
);
const pickerOptions =
document.getElementById(
"pickerOptions"
);
/* ==================================================
HELPERS
================================================== */
function makeId() {
return (
Date.now()
.toString(
36
)
+
Math.random()
.toString(
36
)
.slice(
2,
7
)
);
}
function getModel() {
return MODELS[
selectedModelKey
];
}
function subjectShortName() {
if (
subject
===
"Mathematics"
) {
return "Math";
}
return subject;
}
/* ==================================================
AUTO SCROLL
================================================== */
function scrollToBottom(
smooth = true
) {
requestAnimationFrame(
() => {
workspace.scrollTo({
top:
workspace.scrollHeight,
behavior:
smooth
?
"smooth"
:
"auto"
});
}
);
}
/* ==================================================
LOAD CHAT STORAGE
================================================== */
function loadChats() {
try {
chats =
JSON.parse(
localStorage.getItem(
chatStorageKey()
)
||
"[]"
);
}
catch {
chats =
[];
}
renderChatList();
}
/* ==================================================
SAVE CHAT STORAGE
================================================== */
function saveChats() {
chats.sort(
(a, b) =>
b.updated
-
a.updated
);
chats =
chats.slice(
0,
40
);
localStorage.setItem(
chatStorageKey(),
JSON.stringify(
chats
)
);
renderChatList();
}
/* ==================================================
CHAT SIDEBAR
================================================== */
function renderChatList() {
chatList.innerHTML =
"";
recentEmpty.style.display =
chats.length
?
"none"
:
"block";
chats
.slice(
0,
15
)
.forEach(
saved => {
const button =
document.createElement(
"button"
);
button.type =
"button";
button.className =
"saved-chat"
+
(
saved.id
===
currentChatId
?
" active"
:
""
);
button.textContent =
saved.title;
button.addEventListener(
"click",
() => {
openChat(
saved.id
);
}
);
chatList.appendChild(
button
);
}
);
}
/* ==================================================
GET CHAT
================================================== */
function getCurrentChat() {
return chats.find(
item =>
item.id
===
currentChatId
);
}
/* ==================================================
CREATE CHAT
================================================== */
function createChat(
firstMessage
) {
const saved = {
id:
makeId(),
title:
firstMessage
.slice(
0,
38
),
subject:
subject,
mode:
mode,
model:
selectedModelKey,
updated:
Date.now(),
messages:
[]
};
chats.unshift(
saved
);
currentChatId =
saved.id;
return saved;
}
function getOrCreateChat(
firstMessage
) {
return (
getCurrentChat()
||
createChat(
firstMessage
)
);
}
/* ==================================================
NEW CHAT
================================================== */
function newChat() {
currentChatId =
null;
history =
[];
activeQuiz =
null;
document.body
.classList
.remove(
"chat-started"
);
hero.style.display =
"block";
chatThread
.classList
.remove(
"active"
);
chatThread.innerHTML =
"";
document
.getElementById(
"sessionStatus"
)
.textContent =
generator
?
"Ready · "
+
getModel().label
:
"Ready to begin";
renderChatList();
updateAllUI();
workspace.scrollTo({
top:
0,
behavior:
"smooth"
});
}
document
.getElementById(
"newChat"
)
.addEventListener(
"click",
newChat
);
/* ==================================================
OPEN OLD CHAT
================================================== */
async function openChat(
id
) {
const saved =
chats.find(
item =>
item.id
===
id
);
if (
!saved
) {
return;
}
currentChatId =
saved.id;
subject =
saved.subject
||
"Mathematics";
mode =
saved.mode
||
"Explain Topic";
if (
saved.model
&&
MODELS[
saved.model
]
&&
saved.model
!==
selectedModelKey
) {
chooseModel(
saved.model
);
}
history =
[];
activeQuiz =
null;
hero.style.display =
"none";
chatThread
.classList
.add(
"active"
);
document.body
.classList
.add(
"chat-started"
);
chatThread.innerHTML =
"";
saved.messages
.forEach(
item => {
if (
item.role
===
"user"
) {
addMessage(
item.content,
"user",
false
);
}
else {
const bubble =
addMessage(
"",
"assistant",
false
);
renderAIResponse(
bubble,
item.content,
item.mode
||
mode,
false
);
}
history.push({
role:
item.role
===
"assistant"
?
"assistant"
:
"user",
content:
item.content
});
}
);
history =
history.slice(
-8
);
updateAllUI();
renderChatList();
setTimeout(
() => {
scrollToBottom(
false
);
},
50
);
}
/* ==================================================
CUSTOM PICKERS
================================================== */
function openPicker(
type
) {
activePicker =
type;
pickerOptions.innerHTML =
"";
let items =
[];
let current =
"";
if (
type
===
"subject"
) {
pickerTitle.textContent =
"Choose a subject";
pickerSubtitle.textContent =
"FYRStudyGPT will adapt its explanations to this subject.";
items =
SUBJECTS;
current =
subject;
}
else if (
type
===
"mode"
) {
pickerTitle.textContent =
"Choose a study mode";
pickerSubtitle.textContent =
"Choose how you want FYRStudyGPT to help.";
items =
MODES;
current =
mode;
}
else {
pickerTitle.textContent =
"Choose your AI";
pickerSubtitle.textContent =
"Larger models can give stronger answers but may take longer to load.";
items =
MODEL_OPTIONS;
current =
selectedModelKey;
}
items.forEach(
item => {
const value =
item.value;
const name =
item.name
||
item.value;
const button =
document.createElement(
"button"
);
button.type =
"button";
button.className =
"picker-option"
+
(
value
===
current
?
" selected"
:
""
);
const icon =
document.createElement(
"span"
);
icon.className =
"picker-option-icon";
icon.textContent =
item.icon;
const main =
document.createElement(
"span"
);
main.className =
"picker-option-main";
const title =
document.createElement(
"span"
);
title.className =
"picker-option-name";
title.textContent =
name;
const description =
document.createElement(
"span"
);
description.className =
"picker-option-description";
description.textContent =
item.description;
main.append(
title,
description
);
const check =
document.createElement(
"span"
);
check.className =
"picker-check";
check.textContent =
value
===
current
?
"✓"
:
"";
button.append(
icon,
main,
check
);
button.addEventListener(
"click",
async () => {
const pickerType =
activePicker;
closePicker();
if (
pickerType
===
"subject"
) {
subject =
value;
updateAllUI();
}
else if (
pickerType
===
"mode"
) {
mode =
value;
activeQuiz =
null;
updateAllUI();
}
else {
await chooseModel(
value
);
}
}
);
pickerOptions.appendChild(
button
);
}
);
pickerBackdrop
.classList
.add(
"visible"
);
}
function closePicker() {
pickerBackdrop
.classList
.remove(
"visible"
);
activePicker =
null;
}
document
.getElementById(
"pickerClose"
)
.addEventListener(
"click",
closePicker
);
pickerBackdrop.addEventListener(
"click",
event => {
if (
event.target
===
pickerBackdrop
) {
closePicker();
}
}
);
document
.getElementById(
"subjectPickerButton"
)
.addEventListener(
"click",
() => {
openPicker(
"subject"
);
}
);
document
.getElementById(
"modePickerButton"
)
.addEventListener(
"click",
() => {
openPicker(
"mode"
);
}
);
document
.getElementById(
"subjectSidebarButton"
)
.addEventListener(
"click",
() => {
openPicker(
"subject"
);
}
);
document
.getElementById(
"modeSidebarButton"
)
.addEventListener(
"click",
() => {
openPicker(
"mode"
);
}
);
document
.getElementById(
"modelSidebarButton"
)
.addEventListener(
"click",
() => {
openPicker(
"model"
);
}
);
document
.getElementById(
"breadcrumbSubject"
)
.addEventListener(
"click",
() => {
openPicker(
"subject"
);
}
);
/* ==================================================
UPDATE UI
================================================== */
function updateAllUI() {
document
.getElementById(
"breadcrumbSubject"
)
.textContent =
subjectShortName();
document
.getElementById(
"subjectSidebarValue"
)
.textContent =
subject;
document
.getElementById(
"modeSidebarValue"
)
.textContent =
mode;
document
.getElementById(
"subjectPickerValue"
)
.textContent =
subject;
document
.getElementById(
"modePickerValue"
)
.textContent =
mode;
document
.getElementById(
"introMode"
)
.textContent =
MODE_DATA[
mode
].tag;
messageInput.placeholder =
MODE_DATA[
mode
]
.placeholder
.replace(
"{subject}",
subject.toLowerCase()
);
const model =
getModel();
document
.getElementById(
"modelSidebarValue"
)
.textContent =
model.label
+
" · "
+
model.short;
renderSuggestions();
}
/* ==================================================
SUGGESTIONS
================================================== */
function renderSuggestions() {
const container =
document.getElementById(
"suggestions"
);
container.innerHTML =
"";
MODE_DATA[
mode
]
.suggestions
.forEach(
text => {
const button =
document.createElement(
"button"
);
button.type =
"button";
button.className =
"suggestion";
const label =
document.createElement(
"span"
);
label.textContent =
text;
const arrow =
document.createElement(
"span"
);
arrow.className =
"suggestion-arrow";
arrow.textContent =
"↗";
button.append(
label,
arrow
);
button.addEventListener(
"click",
() => {
messageInput.value =
text;
resizeInput();
updateSendState();
messageInput.focus();
}
);
container.appendChild(
button
);
}
);
}
/* ==================================================
MODEL MANAGEMENT
================================================== */
async function releaseModel() {
if (
generator
&&
typeof generator.dispose
===
"function"
) {
try {
await generator.dispose();
}
catch (
error
) {
console.warn(
error
);
}
}
generator =
null;
loadingPromise =
null;
modelRuntime =
"";
}
async function chooseModel(
key
) {
if (
!MODELS[
key
]
) {
return;
}
if (
key
===
selectedModelKey
&&
generator
) {
return;
}
await releaseModel();
selectedModelKey =
key;
localStorage.setItem(
modelStorageKey(),
key
);
updateAllUI();
document
.getElementById(
"sessionStatus"
)
.textContent =
"Preparing "
+
getModel().label
+
" AI";
loadAI()
.catch(
error => {
console.warn(
error
);
}
);
}
/* ==================================================
MODEL LOADING
================================================== */
function updateProgress(
percent
) {
const value =
Math.max(
0,
Math.min(
100,
Math.round(
percent
)
)
);
loadingFill.style.width =
Math.max(
3,
value
)
+
"%";
loadingDetail.textContent =
value
+
"%";
}
async function loadAttempt(
model,
options,
runtimeName
) {
let highest =
0;
return await transformersPipeline(
"text-generation",
model.id,
{
...options,
progress_callback:
progress => {
if (
progress.status
===
"progress"
&&
typeof progress.progress
===
"number"
) {
highest =
Math.max(
highest,
progress.progress
);
updateProgress(
highest
);
}
}
}
);
}
async function loadAI() {
if (
generator
) {
return generator;
}
if (
loadingPromise
) {
return loadingPromise;
}
const model =
getModel();
loadingPromise =
(
async () => {
loadingBox
.classList
.add(
"visible"
);
errorBox.textContent =
"";
updateProgress(
0
);
loadingName.textContent =
"Preparing FYRStudyGPT…";
await ensureTransformers();
const attempts =
[];
if (
"gpu"
in
navigator
) {
attempts.push({
name:
"GPU · "
+
model.gpuDtype,
options: {
device:
"webgpu",
dtype:
model.gpuDtype
}
});
}
attempts.push({
name:
"CPU · "
+
model.cpuDtype,
options: {
dtype:
model.cpuDtype
}
});
let lastError =
null;
for (
const attempt
of
attempts
) {
try {
updateProgress(
0
);
loadingName.textContent =
"Loading "
+
model.label
+
" AI";
loadingDetail.textContent =
attempt.name;
generator =
await loadAttempt(
model,
attempt.options,
attempt.name
);
modelRuntime =
attempt.name;
break;
}
catch (
error
) {
console.warn(
"Model load failed:",
attempt.name,
error
);
generator =
null;
lastError =
error;
loadingName.textContent =
"Trying another loading mode…";
}
}
if (
!generator
) {
throw (
lastError
||
new Error(
"The AI model could not be loaded."
)
);
}
updateProgress(
100
);
loadingName.textContent =
"FYRStudyGPT is ready";
loadingDetail.textContent =
modelRuntime;
document
.getElementById(
"sessionStatus"
)
.textContent =
"Ready · "
+
model.label;
setTimeout(
() => {
loadingBox
.classList
.remove(
"visible"
);
},
600
);
return generator;
}
)();
try {
return await loadingPromise;
}
catch (
error
) {
generator =
null;
loadingName.textContent =
"FYRStudyGPT couldn't start";
loadingDetail.textContent =
"Try another model";
errorBox.textContent =
error?.message
||
String(
error
);
document
.getElementById(
"sessionStatus"
)
.textContent =
"AI load failed";
throw error;
}
finally {
loadingPromise =
null;
}
}
/* ==================================================
SYSTEM PROMPT
================================================== */
function systemPrompt() {
return `You are FYRStudyGPT, a calm and supportive secondary-school study tutor.
Help the student understand instead of simply copying answers.
Be clear and accurate.
Break difficult ideas into small steps.
For mathematics, show useful working.
For humanities, explain evidence and reasoning.
Use examples where useful.
Keep answers focused.
Do not invent facts.
If you are uncertain, say so clearly.`;
}
/* ==================================================
CREATE PROMPT
================================================== */
function createPrompt(
text
) {
if (
mode
===
"Explain Topic"
) {
return `Subject: ${subject}
Explain this clearly:
${text}
Include:
1. A simple explanation
2. Important ideas
3. An example
4. A short recap`;
}
if (
mode
===
"Quiz Me"
) {
if (
activeQuiz
) {
const quiz =
activeQuiz;
activeQuiz =
null;
return `You previously gave this quiz:
${quiz}
The student answered:
${text}
Mark every answer.
For each question:
- say Correct, Partly Correct, or Incorrect
- explain briefly
- give the correct answer if needed
Finish with a score and what to revise.`;
}
return `Subject: ${subject}
Create a five-question quiz about:
${text}
Number every question.
Do not reveal the answers yet.
Ask the student to reply with their answers.`;
}
if (
mode
===
"Flashcards"
) {
return `Subject: ${subject}
Create exactly 8 revision flashcards about:
${text}
Use only this format:
Q: question
A: answer
Q: question
A: answer
Do not add any other text.`;
}
if (
mode
===
"Study Plan"
) {
return `Subject: ${subject}
Create a realistic one-hour study plan for:
${text}
Include:
- timed study blocks
- active recall
- practice
- a short break
- a final recap`;
}
if (
mode
===
"Check Answer"
) {
return `Subject: ${subject}
The student provided this question and/or answer:
${text}
Give feedback using:
1. What is correct
2. What needs improvement
3. A stronger answer or method
4. One revision tip`;
}
return `Subject: ${subject}
Student question:
${text}
Answer like a friendly tutor.
Explain the reasoning instead of only giving the final answer.`;
}
/* ==================================================
OUTPUT PARSER
================================================== */
function extractOutput(
result
) {
const generated =
result?.[0]
?.generated_text;
if (
Array.isArray(
generated
)
) {
const last =
generated[
generated.length
-
1
];
if (
typeof last?.content
===
"string"
) {
return last.content.trim();
}
}
if (
typeof generated
===
"string"
) {
return generated.trim();
}
return "";
}
/* ==================================================
GENERATE
================================================== */
async function generateAnswer(
prompt
) {
const messages = [
{
role:
"system",
content:
systemPrompt()
},
...history.slice(
-6
),
{
role:
"user",
content:
prompt
}
];
try {
const result =
await generator(
messages,
{
max_new_tokens:
getModel().tokens,
do_sample:
false,
repetition_penalty:
1.08
}
);
const answer =
extractOutput(
result
);
if (
answer
) {
return answer;
}
}
catch (
error
) {
console.warn(
"Chat format failed:",
error
);
}
const fallbackPrompt =
systemPrompt()
+
"\n\n"
+
prompt
+
"\n\nFYRStudyGPT:";
const result =
await generator(
fallbackPrompt,
{
max_new_tokens:
getModel().tokens,
do_sample:
false,
repetition_penalty:
1.08,
return_full_text:
false
}
);
return (
extractOutput(
result
)
||
"I couldn't generate a useful answer. Try asking differently."
);
}
/* ==================================================
MESSAGE UI
================================================== */
function addMessage(
text,
role,
autoScroll = true
) {
hero.style.display =
"none";
chatThread
.classList
.add(
"active"
);
const row =
document.createElement(
"div"
);
row.className =
"message "
+
role;
const bubble =
document.createElement(
"div"
);
bubble.className =
"message-bubble";
bubble.textContent =
text;
row.appendChild(
bubble
);
chatThread.appendChild(
row
);
if (
autoScroll
) {
scrollToBottom();
}
return bubble;
}
/* ==================================================
FLASHCARDS
================================================== */
function parseFlashcards(
text
) {
const lines =
text.split(
"\n"
);
const cards =
[];
let question =
null;
for (
const rawLine
of
lines
) {
const line =
rawLine.trim();
if (
/^Q\s*:/i
.test(
line
)
) {
question =
line
.replace(
/^Q\s*:/i,
""
)
.trim();
}
else if (
question
&&
/^A\s*:/i
.test(
line
)
) {
cards.push({
question:
question,
answer:
line
.replace(
/^A\s*:/i,
""
)
.trim()
});
question =
null;
}
}
return cards;
}
/* ==================================================
RESPONSE UI
================================================== */
function renderAIResponse(
bubble,
text,
responseMode,
autoScroll = true
) {
if (
responseMode
===
"Flashcards"
) {
const cards =
parseFlashcards(
text
);
if (
cards.length
>=
2
) {
bubble.className =
"flashcards";
bubble.textContent =
"";
cards.forEach(
data => {
const card =
document.createElement(
"button"
);
card.type =
"button";
card.className =
"flashcard";
const question =
document.createElement(
"div"
);
question.className =
"flash-q";
question.textContent =
data.question;
const answer =
document.createElement(
"div"
);
answer.className =
"flash-a";
answer.textContent =
data.answer;
const hint =
document.createElement(
"div"
);
hint.className =
"flash-hint";
hint.textContent =
"Tap to reveal";
card.append(
question,
answer,
hint
);
card.addEventListener(
"click",
() => {
card
.classList
.toggle(
"open"
);
hint.textContent =
card
.classList
.contains(
"open"
)
?
"Tap to hide"
:
"Tap to reveal";
}
);
bubble.appendChild(
card
);
}
);
if (
autoScroll
) {
scrollToBottom();
}
return;
}
}
bubble.textContent =
text;
if (
responseMode
===
"Explain Topic"
) {
bubble.classList.add(
"explain"
);
}
else if (
responseMode
===
"Quiz Me"
) {
bubble.classList.add(
"quiz"
);
}
else if (
responseMode
===
"Study Plan"
) {
bubble.classList.add(
"study-plan"
);
}
else if (
responseMode
===
"Check Answer"
) {
bubble.classList.add(
"check-answer"
);
}
if (
autoScroll
) {
scrollToBottom();
}
}
/* ==================================================
SEND
================================================== */
async function sendMessage() {
const text =
messageInput.value.trim();
if (
!text
||
sendButton.disabled
) {
return;
}
const responseMode =
mode;
const quizBefore =
activeQuiz;
const saved =
getOrCreateChat(
text
);
saved.subject =
subject;
saved.mode =
mode;
saved.model =
selectedModelKey;
saved.updated =
Date.now();
saved.messages.push({
role:
"user",
content:
text,
mode:
responseMode
});
saveChats();
/*
Hide subject/mode cards and
suggestions once chat begins.
*/
document.body
.classList
.add(
"chat-started"
);
addMessage(
text,
"user"
);
messageInput.value =
"";
resizeInput();
updateSendState();
sendButton.disabled =
true;
document
.getElementById(
"sessionStatus"
)
.textContent =
generator
?
"Thinking…"
:
"Starting AI…";
/*
Auto-scroll after question.
*/
scrollToBottom();
const responseBubble =
addMessage(
generator
?
"Thinking…"
:
"Preparing FYRStudyGPT…",
"assistant"
);
try {
await loadAI();
responseBubble.textContent =
"Thinking…";
scrollToBottom();
const prompt =
createPrompt(
text
);
const answer =
await generateAnswer(
prompt
);
renderAIResponse(
responseBubble,
answer,
responseMode
);
if (
responseMode
===
"Quiz Me"
&&
!quizBefore
) {
activeQuiz =
answer;
}
history.push(
{
role:
"user",
content:
prompt
},
{
role:
"assistant",
content:
answer
}
);
history =
history.slice(
-8
);
saved.messages.push({
role:
"assistant",
content:
answer,
mode:
responseMode
});
saved.updated =
Date.now();
saveChats();
document
.getElementById(
"sessionStatus"
)
.textContent =
"Ready · "
+
getModel().label;
/*
Auto-scroll after answer.
*/
scrollToBottom();
}
catch (
error
) {
console.error(
error
);
responseBubble.textContent =
"I couldn't start the AI. Try another model or reload the page.";
scrollToBottom();
}
finally {
sendButton.disabled =
false;
updateSendState();
}
}
/* ==================================================
TEXTAREA
================================================== */
function resizeInput() {
messageInput.style.height =
"auto";
messageInput.style.height =
Math.min(
messageInput.scrollHeight,
150
)
+
"px";
}
function updateSendState() {
sendButton
.classList
.toggle(
"ready",
messageInput.value
.trim()
.length
>
0
);
}
messageInput.addEventListener(
"input",
() => {
resizeInput();
updateSendState();
}
);
messageInput.addEventListener(
"keydown",
event => {
if (
event.key
===
"Enter"
&&
!event.shiftKey
) {
event.preventDefault();
sendMessage();
}
}
);
sendButton.addEventListener(
"click",
sendMessage
);
/* ==================================================
THEME
================================================== */
function setTheme(
dark
) {
document.body
.classList
.toggle(
"dark",
dark
);
document
.getElementById(
"themeLabel"
)
.textContent =
dark
?
"Dark theme"
:
"Light theme";
document
.getElementById(
"themeButton"
)
.textContent =
dark
?
"☾"
:
"☼";
localStorage.setItem(
"studyai.dark",
dark
?
"1"
:
"0"
);
}
function toggleTheme() {
setTheme(
!document.body
.classList
.contains(
"dark"
)
);
}
document
.getElementById(
"themeButton"
)
.addEventListener(
"click",
toggleTheme
);
document
.getElementById(
"moonButton"
)
.addEventListener(
"click",
toggleTheme
);
/* ==================================================
SIDEBAR / FOCUS MODE
================================================== */
document
.getElementById(
"focusButton"
)
.addEventListener(
"click",
() => {
document.body
.classList
.toggle(
"focus-mode"
);
}
);
document
.getElementById(
"menuButton"
)
.addEventListener(
"click",
() => {
if (
window.innerWidth
<=
720
) {
document.body
.classList
.toggle(
"sidebar-open"
);
}
else {
document.body
.classList
.toggle(
"focus-mode"
);
}
}
);
document
.getElementById(
"closeSidebar"
)
.addEventListener(
"click",
() => {
if (
window.innerWidth
<=
720
) {
document.body
.classList
.remove(
"sidebar-open"
);
}
else {
document.body
.classList
.add(
"focus-mode"
);
}
}
);
/* ==================================================
KEYBOARD SHORTCUTS
================================================== */
document.addEventListener(
"keydown",
event => {
const tag =
event.target
?.tagName
?.toLowerCase()
||
"";
if (
event.key
.toLowerCase()
===
"n"
&&
tag
!==
"input"
&&
tag
!==
"textarea"
&&
!pickerBackdrop
.classList
.contains(
"visible"
)
) {
newChat();
}
if (
event.key
===
"Escape"
) {
closePicker();
document.body
.classList
.remove(
"sidebar-open"
);
}
}
);
/* ==================================================
STARTUP
================================================== */
const savedModel =
localStorage.getItem(
modelStorageKey()
);
if (
savedModel
&&
MODELS[
savedModel
]
) {
selectedModelKey =
savedModel;
}
const savedDark =
localStorage.getItem(
"studyai.dark"
)
===
"1";
setTheme(
savedDark
);
loadChats();
updateAllUI();
newChat();
updateSendState();
if (
navigator.storage
&&
navigator.storage.persist
) {
navigator.storage
.persist()
.catch(
() => {}
);
}
/*
Optional background preload.
Wait a moment so the UI opens first.
*/
setTimeout(
() => {
loadAI()
.catch(
error => {
console.warn(
"Background AI load failed:",
error
);
}
);
},
800
);
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
>
<title>FYRStudyGPT</title>
<link
rel="stylesheet"
href="style.css"
>
</head>
<body>
<!-- ==================================================
PICKER
================================================== -->
<div
id="pickerBackdrop"
class="picker-backdrop"
>
<div
class="picker-panel"
role="dialog"
aria-modal="true"
>
<div class="picker-header">
<div class="picker-header-text">
<div
id="pickerTitle"
class="picker-title"
>
Choose
</div>
<div
id="pickerSubtitle"
class="picker-subtitle"
></div>
</div>
<button
id="pickerClose"
class="picker-close"
type="button"
>
×
</button>
</div>
<div
id="pickerOptions"
class="picker-options"
></div>
</div>
</div>
<!-- ==================================================
APP
================================================== -->
<div class="app">
<!-- ==================================================
SIDEBAR
================================================== -->
<aside class="sidebar">
<div class="brand-row">
<div class="brand-icon">
<div class="equalizer">
<span></span>
<span></span>
<span></span>
</div>
</div>