import React, { useState, useEffect, useRef } from "react"; import { useNavigate, useLocation } from "react-router-dom"; import Navbar from "./Components/Navbar"; import axios from "axios"; import YouTube from "react-youtube"; import ReactMarkdown from "react-markdown"; // import { fetchTranscript } from "youtube-subtitle-transcript"; // import Mindmap from "./Components/Mindmap"; import ForceGraph2D from "react-force-graph-2d"; import ForceGraph3D from "react-force-graph-3d"; import SpriteText from "three-spritetext"; function App() { const { useRef } = React; const fgRef = useRef(); const navigate = useNavigate(); const [messages, setMessages] = useState([]); const [userInput, setUserInput] = useState(""); const [timestamps, setTimestamps] = useState([]); const [transcript, setTranscript] = useState(""); const [loading, setLoading] = useState(false); const [transcriptStatus, setTranscriptStatus] = useState(false); const location = useLocation(); const videoLink = location.state?.videoLink; const metaData = location.state?.metaData; const [videoId, setVideoId] = useState(""); const endOfMessagesRef = useRef(null); const [player, setPlayer] = React.useState(null); const routebody = "http://localhost:5000"; const handleClick = async () => { if (!loading) { try { const response = await axios.post(routebody.concat("/transcript")); // console.log("Transcript", response.data.transcript); const temp = response.data.transcript; // This should now correctly reflect the updated state // console.log("Tt", temp); const element = document.createElement("a"); const file = new Blob([temp], { type: "text/plain" }); // Use the fetched transcript directly element.href = URL.createObjectURL(file); element.download = "transcript.txt"; document.body.appendChild(element); // Required for this to work in FireFox element.click(); } catch (error) { window.alert("Failed to fetch transcript:", error); } } }; const fetch = async () => { try { setTranscript(response.data.transcript); setTranscriptStatus(response.data.status); } catch (error) { console.error("Failed to fetch transcript:", error); } }; const onPlayerReady = (event) => { setPlayer(event.target); console.log("Player is ready"); }; const scrollToBottom = () => { if (endOfMessagesRef.current) { endOfMessagesRef.current.scrollIntoView({ behavior: "smooth" }); } }; useEffect(() => { scrollToBottom(); }, [messages]); console.log(videoLink); useEffect(() => { function convertToEmbedUrl(watchUrl) { return watchUrl.replace("/watch?v=", "/embed/"); } if (videoLink) { setVideoId(videoLink.split("v=")[1]); console.log(videoId); } else { setVideoId("dQw4w9WgXcQ") // Easter Egg πŸ₯š // navigate("/"); } }, []); const sendMessage = async () => { try { console.log("Sending message..."); setLoading(true); let formattedMessages = []; for (let i = 0; i < messages.length; i += 2) { let message = []; message.push(messages[i].content); message.push(messages[i + 1].content); formattedMessages.push(message); } console.log("Formatted", formattedMessages); // const formattedInput = // userInput + // "?. In bulleted points, respond with the key points of the answer for the question and provide a concise answer in markdown format highlighting the keypoints and features using markdown."; const response = await axios.post(routebody.concat("/response"), { query: userInput, chat_history: formattedMessages, }); console.log("Answer", response.data.answer); console.log("Time sta", response.data.timestamps); setTimestamps(response.data.timestamps.slice(0, 5)); setMessages([ ...messages, { role: "user", content: userInput + "?" }, { role: "assistant", content: response.data.answer }, ]); console.log(messages); setUserInput(""); setLoading(false); } catch (error) { console.log(error); } }; const handleKeyPress = (e) => { if (e.key === "Enter" && userInput.trim() !== "") { sendMessage(); e.preventDefault(); // To prevent any default behavior, such as a line break in a textarea } }; const markdown = ""; useEffect(() => { async function fetchSummary() { try { console.log("Summarizing..."); setLoading(true); const response = await axios.post(routebody.concat("/response"), { query: `You are a Intelligent Tutor. Your task is to smartly summarize the the Youtube video transcript provided as context. \ Don't mention the transcript as 'transcript' but refer to it as Youtube video or just video while having a conversation with a student.\ Give me in-depth response for the summaray part only... something like : Short-Intro to what is being said in the video transcript.\ 5 - KEY points to understand in bullet point format... Conclusion as to what can be learnt from this video transcript....\ Only the Conclusion in the summary should be application perspective.... \ Also you are a friendly tutor, so if the question asked is casual or something not realted to the video transcript then answer it casually like a friend, only if the question asked is casual or not realted to the context... more of general question ... only then you do this ... other wise stick to the context provided... From thsi point provide all the responses in this chat in Markdown Format highlighting keypoints and fetures. Also for every response you have to give introduction, keypoints and conclusion. Keep in mind that the markdown syntax should be followed for every response that you will give in fututre.`, chat_history: "", }); console.log("Answer", response.data.answer); console.log("Time stamp", response.data.timestamps); // setTimestamps(response.data.timestamps); // only set first five timestamps setTimestamps(response.data.timestamps.slice(0, 5)); setMessages([ ...messages, { role: "user", content: userInput }, { role: "assistant", content: response.data.answer }, ]); console.log(messages); setUserInput(""); setLoading(false); } catch (error) { console.log(error); } } return () => { // fetch fetchSummary(); } }, []); // /////////////// data for mindmap /////////////////////// const [data, setData] = useState(); useEffect(() => { async function fetchMindMap() { try { console.log("Mind Mapping..."); setLoading(true); const response = await axios.post(routebody.concat("/response"), { query: `Smart Tutor, please provide a consize creative Mind Map!... The Idea is to Show an intricate, easy to understand (in terms of concept "linking") mind map, based on the provided context\ so that the users can understand the video --key contepts-- helping them to know what the video is discussing in-depth!, remember not to over simplifiy the mind-map,\ WE can have beautiful looking mind map with branches of important points from the video... We want your response in json object for example: \ { "nodes": [ { "id": "Main Concept", "group": 1, "size": 30 }, { "id": "Sub-Concept 1", "group": 2, "size": 20 }, { "id": "Sub-Concept 2", "group": 2, "size": 20 }, { "id": "Sub-Concept 3", "group": 2, "size": 20 }, { "id": "Sub-Concept 4", "group": 2, "size": 20 }, { "id": "Sub-Concept 5", "group": 2, "size": 20 }, { "id": "Sub-Concept 6", "group": 2, "size": 20 }, { "id":"Sub-Sub-Concept 7", "group":2, "size": 15 } ], "links": [ { "source": "Main Concept", "target": "Sub-Concept 1", "value": 10 }, { "source": "Main Concept", "target": "Sub-Concept 2", "value": 9 }, { "source": "Main Concept", "target": "Sub-Concept 3", "value": 8 }, { "source": "Main Concept", "target": "Sub-Concept 4", "value": 7 }, { "source": "Main Concept", "target": "Sub-Concept 5", "value": 6 }, { "source": "Main Concept", "target": "Sub-Concept 6", "value": 5 }, { "source": "Sub-Concept 6", "target": "Sub-Sub-Concept 7", "value": 5 } ] } This structure can be used as a template or guide to create more specific mind maps based on the requirements of the youtube video transcript provided. You can customize the node names, group assignments, and link weights to reflect the content and relationships in your mind map. only json object of mindmap is expected as output`, chat_history: "", }); console.log("MindMap: ", response.data.answer); setData(JSON.parse(response.data.answer)); setLoading(false); } catch (error) { console.log(error); } } // fetchMindMap(); return () => { // fetch fetchMindMap(); } }, []); // /////////////// data for mindmap /////////////////////// const CameraOrbit = () => { return ( `${node.name}`} // nodeVal={(node) => 2} nodeThreeObject={(node) => { const sprite = new SpriteText(node.id); sprite.color = node.color; sprite.textHeight = 8; return sprite; }} linkColor={() => "black"} linkDirectionalParticleColor={() => "red"} linkDirectionalParticleWidth={6} linkHoverPrecision={10} onLinkClick={(link) => fgRef.current.emitParticle(link)} // reset position of camera on double click onNodeClick={(node) => { fgRef.current.centerAt(node.x, node.y, node.z, 1000); } } /> ); }; const downloadMindmapSVG = () => { console.log("Downloading SVG") const svg = fgRef.current.exportSVG(); const blob = new Blob([svg], { type: "image/svg+xml" }); const url = URL.createObjectURL(blob); const a = document.createElement ("a"); a.href = url; a.download = "mindmap.svg"; a.click(); URL.revokeObjectURL(url); } return (

{metaData}

{/*
*/}
{/*

Mindmap

*/}

πŸ“Ί Chat with the Video πŸ“Ί

{/*
{"# Markdown"}
*/} {messages.map((msg, idx) => (
{msg.content} {/* Show buttopns of 3 timestamps */} {/* {msg.content} */} {msg.role === "assistant" && (
{timestamps.map((time, idx) => ( ))}
)}
))}
setUserInput(e.target.value)} onKeyPress={handleKeyPress} placeholder="πŸ€” Ask me anything! ... " className="flex-grow w-5/6 px-8 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring focus:border-indigo-500" /> {/* {loading && (
)} */} {/* */}
); } export default App;