diff --git a/parsing.cpp b/parsing.cpp new file mode 100644 index 0000000..4d29476 --- /dev/null +++ b/parsing.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +int main() +{ + string s; + cin>>s; + int n = s.length(); + int i = 0; + pair, vector> parsed; + while(s[i] != ':') + { + i++; + } + vector prereq; + vector subtopics; + cout<<"Prereq: "<; +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 ( +
+

LearnRails.AI

+

AI Powered Learning Path for You

+ setTopic(e.target.value)} + style={{ padding: "10px", fontSize: "16px", width: "300px" }} + /> + + +
+ ); } -export default App; +export default function App() { + return ( + + + } /> + } /> + } /> + + + ); +} diff --git a/src/MCQQuizPage.jsx b/src/MCQQuizPage.jsx new file mode 100644 index 0000000..c9cd053 --- /dev/null +++ b/src/MCQQuizPage.jsx @@ -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 ( +
+

MCQ Quiz

+ setTopic(e.target.value)} + style={{ padding: "10px", width: "100%", marginBottom: "10px" }} + /> + + {questionData && ( +
+

{questionData.question}

+
    + {questionData.options.map((option, index) => ( +
  • + +
  • + ))} +
+ {feedback &&

{feedback}

} +
+ )} +
+ ); +} \ No newline at end of file diff --git a/src/RoadmapPage.jsx b/src/RoadmapPage.jsx new file mode 100644 index 0000000..eb792c4 --- /dev/null +++ b/src/RoadmapPage.jsx @@ -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 ( +
+

Learning Roadmap

+ + {roadmapData ? ( +
+

Prerequisites

+
    + {roadmapData.prerequisites.map((item, index) => ( +
  • {item}
  • + ))} +
+

Subtopics

+
    + {roadmapData.subtopics.map((item, index) => ( +
  • {item}
  • + ))} +
+
+ ) : ( +

Loading roadmap...

+ )} +
+ ); +}