Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions parsing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <bits/stdc++.h>
using namespace std;

int main()
{
string s;
cin>>s;
int n = s.length();
int i = 0;
pair<vector<string>, vector<string>> parsed;
while(s[i] != ':')
{
i++;
}
vector<string> prereq;
vector<string> subtopics;
cout<<"Prereq: "<<endl;
while(s[i] != '2' && s[i+1] != ')')
{
string temp = "";
while(s[i] != '-' || (s[i] != '2' && s[i+1] != ')'))
{
temp += s[i];
i++;
}
cout<<temp<<endl;
prereq.push_back(temp);
}
while(s[i] != ':')
{
i++;
}
cout<<"subtopics: "<<endl;
while(s[i] != '}')
{
string temp = "";
while(s[i] != '-')
{
temp += s[i];
i++;
}
cout<<temp<<endl;
subtopics.push_back(temp);
}
parsed = make_pair(prereq, subtopics);
return 0;
}
Binary file added parsing.exe
Binary file not shown.
91 changes: 87 additions & 4 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,90 @@
import Home from "./home/home";
import { BrowserRouter as Router, Routes, Route, useNavigate } from "react-router-dom";
import { useState } from "react";
import RoadmapPage from "./RoadmapPage";
import MCQQuizPage from "./MCQQuizPage";

function App() {
return <Home></Home>;
function Home() {
const [topic, setTopic] = useState("");
const [loading, setLoading] = useState(false);
const [mode, setMode] = useState("roadmap"); // "roadmap" or "quiz"
const navigate = useNavigate();

const fetchData = async () => {
if (!topic.trim()) return;
setLoading(true);

let prompt = "";
if (mode === "roadmap") {
prompt = `Create a structured learning roadmap for ${topic}, strictly in this format:
1) prerequisite: {topic names}
2) subtopics: {topic names}
(keep {} brackets intact and comma-separated topic names)`;
} else {
prompt = `Generate a multiple-choice question (MCQ) for the topic "${topic}".
Provide exactly 4 options, and specify the correct answer in this format:
question: {question text}
options: {option1, option2, option3, option4}
answer: {correct option}`;
}

try {
const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
Authorization: "Bearer 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",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "deepseek/deepseek-r1-zero:free",
messages: [{ role: "user", content: prompt }],
}),
});

const data = await response.json();
const responseText = data.choices[0]?.message?.content || "No data found.";

if (mode === "roadmap") {
navigate("/roadmap", { state: { roadmap: responseText } });
} else {
navigate("/quiz", { state: { quiz: responseText } });
}
} catch (error) {
console.error("Error fetching data:", error);
} finally {
setLoading(false);
}
};

return (
<div style={{ textAlign: "center", padding: "20px" }}>
<h1>LearnRails.AI</h1>
<p>AI Powered Learning Path for You</p>
<input
type="text"
placeholder="Enter a topic..."
value={topic}
onChange={(e) => setTopic(e.target.value)}
style={{ padding: "10px", fontSize: "16px", width: "300px" }}
/>
<select value={mode} onChange={(e) => setMode(e.target.value)} style={{ marginLeft: "10px" }}>
<option value="roadmap">Generate Roadmap</option>
<option value="quiz">Generate Quiz</option>
</select>
<button onClick={fetchData} disabled={loading} style={{ marginLeft: "10px" }}>
{loading ? "Loading..." : "Generate"}
</button>
</div>
);
}

export default App;
export default function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/roadmap" element={<RoadmapPage />} />
<Route path="/quiz" element={<MCQQuizPage />} />
</Routes>
</Router>
);
}
100 changes: 100 additions & 0 deletions src/MCQQuizPage.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { useState } from "react";

export default function MCQQuizPage() {
const [topic, setTopic] = useState("");
const [loading, setLoading] = useState(false);
const [questionData, setQuestionData] = useState(null);
const [selectedAnswer, setSelectedAnswer] = useState(null);
const [feedback, setFeedback] = useState(null);

const fetchMCQ = async () => {
if (!topic.trim()) return;
setLoading(true);
setQuestionData(null);
setFeedback(null);
setSelectedAnswer(null);

try {
const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
Authorization: "Bearer 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",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "deepseek/deepseek-r1-zero:free",
messages: [
{
role: "user",
content: `Generate a multiple-choice question for ${topic}. Provide four answer choices and specify the correct answer. Format strictly as follows:
question: {Your question text}
options: {option1, option2, option3, option4}
correct: {correct option text}`,
},
],
}),
});

const data = await response.json();
const text = data.choices[0]?.message?.content || "No question found.";

const questionMatch = text.match(/question:\s*{([^}]*)}/i);
const optionsMatch = text.match(/options:\s*{([^}]*)}/i);
const correctMatch = text.match(/correct:\s*{([^}]*)}/i);

if (questionMatch && optionsMatch && correctMatch) {
setQuestionData({
question: questionMatch[1],
options: optionsMatch[1].split(",").map((opt) => opt.trim()),
correct: correctMatch[1],
});
} else {
setQuestionData(null);
}
} catch (error) {
console.error("Error fetching MCQ:", error);
setQuestionData(null);
}
setLoading(false);
};

const handleAnswerSelection = (option) => {
setSelectedAnswer(option);
setFeedback(option === questionData.correct ? "Correct!" : "Incorrect.");
};

return (
<div style={{ padding: "20px", maxWidth: "600px", margin: "auto", textAlign: "center" }}>
<h1>MCQ Quiz</h1>
<input
type="text"
placeholder="Enter topic"
value={topic}
onChange={(e) => setTopic(e.target.value)}
style={{ padding: "10px", width: "100%", marginBottom: "10px" }}
/>
<button onClick={fetchMCQ} disabled={loading} style={{ padding: "10px 20px" }}>
{loading ? "Loading..." : "Generate Question"}
</button>
{questionData && (
<div style={{ marginTop: "20px", textAlign: "left" }}>
<h2>{questionData.question}</h2>
<ul style={{ listStyleType: "none", padding: 0 }}>
{questionData.options.map((option, index) => (
<li key={index} style={{ marginBottom: "10px" }}>
<button
onClick={() => handleAnswerSelection(option)}
disabled={selectedAnswer !== null}
style={{ padding: "10px", width: "100%" }}
>
{option}
</button>
</li>
))}
</ul>
{feedback && <p style={{ fontWeight: "bold", marginTop: "10px" }}>{feedback}</p>}
</div>
)}
</div>
);
}
50 changes: 50 additions & 0 deletions src/RoadmapPage.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useEffect, useState } from "react";
import { useLocation, useNavigate } from "react-router-dom";

export default function RoadmapPage() {
const location = useLocation();
const navigate = useNavigate();
const [roadmapData, setRoadmapData] = useState(null);

useEffect(() => {
if (location.state?.roadmap) {
parseRoadmap(location.state.roadmap);
}
}, [location.state]);

const parseRoadmap = (text) => {
const prerequisiteMatch = text.match(/prerequisite:\s*{([^}]*)}/i);
const subtopicsMatch = text.match(/subtopics:\s*{([^}]*)}/i);

const prerequisites = prerequisiteMatch ? prerequisiteMatch[1].split(',').map(item => item.trim()) : [];
const subtopics = subtopicsMatch ? subtopicsMatch[1].split(',').map(item => item.trim()) : [];

console.log(prerequisites, subtopics);
setRoadmapData({ prerequisites, subtopics });
};

return (
<div style={{ padding: "20px", maxWidth: "600px", margin: "auto" }}>
<h1 style={{ textAlign: "center" }}>Learning Roadmap</h1>
<button onClick={() => navigate("/")} style={{ marginBottom: "20px" }}>Go Back</button>
{roadmapData ? (
<div>
<h2>Prerequisites</h2>
<ul>
{roadmapData.prerequisites.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
<h2>Subtopics</h2>
<ul>
{roadmapData.subtopics.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
</div>
) : (
<p>Loading roadmap...</p>
)}
</div>
);
}