Spaces:
Sleeping
Sleeping
File size: 9,527 Bytes
9bc6550 d1cad4b 94f5dab 9bc6550 7678c37 9bc6550 6c6d1fa 9bc6550 5f10035 94f5dab 9bc6550 8c93fc6 7678c37 2ed6131 89bf64d 6c6d1fa 9bc6550 dbbf6b6 7678c37 dbbf6b6 d1cad4b 9bc6550 dbbf6b6 9bc6550 dbbf6b6 9bc6550 dbbf6b6 d1cad4b 9bc6550 d1cad4b 9bc6550 d1cad4b 9bc6550 d1cad4b 9bc6550 d1cad4b 9bc6550 d1cad4b 94f5dab dbbf6b6 9bc6550 dbbf6b6 7678c37 dbbf6b6 7678c37 dbbf6b6 7678c37 dbbf6b6 9bc6550 dbbf6b6 7678c37 dbbf6b6 d1cad4b 9bc6550 94f5dab 9bc6550 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { ChevronLeft, Menu } from "lucide-react"
import { useEffect, useState } from "react";
import { ToastContainer, toast } from "react-toastify"
import 'react-toastify/dist/ReactToastify.css';
import { Switch } from "@/components/ui/switch"
import { Label } from "@/components/ui/label"
import { Input } from "@/components/ui/input"
function App() {
const [schoolSubject, setSchoolSubject] = useState("biologia");
const [subject, setSubject] = useState("");
const [difficultie, setDifficultie] = useState("");
const [menuState, setMenuState] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const [questions, setQuestions] = useState(null);
const [promptMode, setPromptMode] = useState(false);
const [promptValue, setPromptValue] = useState('');
const schoolSubjects = [
{ label: "História", value: "historia" },
{ label: "Biologia", value: "biologia" },
];
const historySubjects = [
{ label: "Revoluções", value: "revoluções" },
{ label: "Grécia", value: "grécia" },
{ label: "Roma", value: "roma" },
{ label: "Primeira Guerra Mundial", value: "primeira guerra mundial" },
{ label: "Guerra Fria", value: "guerra fria" },
{ label: "Feudalismo", value: "feudalismo" },
]
const biologySubjects = [
{ label: "Fisiologia", value: "fisiologia" },
{ label: "Embriologia", value: "embriologia" },
{ label: "Citologia", value: "citologia" },
{ label: "Genética", value: "genetica" },
{ label: "Botânica", value: "botanica" },
{ label: "Zoologia", value: "zoologia" },
{ label: "Morfologia", value: "morfologia" },
{ label: "Ecologia", value: "ecologia" },
{ label: "Biofísica", value: "biofisica" },
]
const difficulties = [
{ label: "Fácil", value: "facil" },
{ label: "Intermediária", value: "intermediaria" },
{ label: "Difícil", value: "dificil" },
]
const handleSubmit = async (e) => {
e.preventDefault();
setIsLoading(true);
try {
const body = promptMode ? { promptValue } : { subject, difficultie, school_subject: schoolSubject };
const res = await fetch("/generate_questions", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(body)
});
console.log(res)
if (res.ok) {
const data = await res.json();
console.log(data)
setQuestions(data.rag_result.questions);
setPromptValue('');
} else {
toast.error("Erro ao gerar questões", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
className: "bg-slate-900 text-white font-semibold",
});
}
} catch (err) {
console.error(err);
toast.error("Erro ao gerar questões", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
className: "bg-slate-900 text-white font-semibold",
});
}
setIsLoading(false);
}
useEffect(() => {
const handleResize = () => {
if (window.innerWidth > 768) {
setMenuState(true);
} else {
setMenuState(false);
}
}
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}
, []);
return (
<div className="font-sans h-screen text-white min-h-screen bg-slate-900 flex flex-col relative overflow-hidden">
<header className="p-10 flex items-center gap-2.5">
<Menu className="md:hidden" onClick={() => setMenuState(prev => !prev)} />
<h1 className="text-3xl font-bold">
Pergunt.<span className="text-transparent bg-clip-text bg-gradient-to-r from-green-300 to bg-blue-500">AÍ</span>
</h1>
</header>
<main className="flex-1 flex overflow-hidden">
{menuState &&
<div className="min-w-72 p-10 flex flex-col gap-5 absolute h-full top-0 bg-black/95 md:h-fit z-10 md:static md:bg-transparent">
<div className="md:sr-only">
<ChevronLeft className="w-10 h-10" onClick={e => setMenuState(prev => !prev)} />
</div>
<div className="flex items-center space-x-2">
<Switch id="prompt-mode" checked={promptMode} onCheckedChange={setPromptMode} />
<Label htmlFor="prompt-mode">Prompt Mode</Label>
</div>
{!promptMode && (
<form
onSubmit={handleSubmit}
className="flex flex-col gap-2.5 top-0 h-full ">
<fieldset>
<h2>Selecione uma matéria:</h2>
<Select defaultValue="biologia" onValueChange={value => setSchoolSubject(value)}>
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Matéria" />
</SelectTrigger>
<SelectContent>
{
schoolSubjects.map(({ label, value }) => (
<SelectItem key={value} value={value}>{label}</SelectItem>
))
}
</SelectContent>
</Select>
<h2>Selecione um conteúdo:</h2>
<Select onValueChange={value => setSubject(value)}>
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Conteúdo" />
</SelectTrigger>
<SelectContent>
{schoolSubjects &&
schoolSubject === "historia" ?
historySubjects.map(({ label, value }) => (
<SelectItem key={value} value={value}>{label}</SelectItem>
))
:
biologySubjects.map(({ label, value }) => (
<SelectItem key={value} value={value}>{label}</SelectItem>
))
}
</SelectContent>
</Select>
<h2>Selecione uma dificuldade:</h2>
<Select onValueChange={value => setDifficultie(value)}>
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Dificuldade" />
</SelectTrigger>
<SelectContent onChange={e => console.log(e)}>
{difficulties && difficulties.map(({ label, value }) => (
<SelectItem key={value} value={value}>{label}</SelectItem>
))}
</SelectContent>
</Select>
</fieldset>
<button className="h-10 bg-purple-500 rounded px-2.5 py-1 mt-5 hover:brightness-110 transition-all flex items-center justify-center disabled:hover:brightness-75 disabled:brightness-75"
disabled={isLoading || !subject || !difficultie}>
{isLoading ?
<div className="animate-spin h-5 w-5 border-2 border-white border-r-purple-500 rounded-full"></div>
:
"Enviar"
}
</button>
</form>
)}
</div>
}
<section className="p-10 flex-1 md:border-l-2 md:border-l-white flex flex-col gap-2.5 ">
{promptMode &&
<form
onSubmit={handleSubmit}>
<h2 className="text-2xl font-bold mb-5">Digite seu prompt para gerarmos as questões!</h2>
<Input placeholder="Digite seu prompt:" value={promptValue} onChange={(e) => setPromptValue(e.target.value)} />
<button className="h-10 w-full bg-purple-500 rounded px-2.5 py-1 mt-5 hover:brightness-110 transition-all flex items-center justify-center disabled:hover:brightness-75 disabled:brightness-75"
disabled={isLoading || !promptValue}>
{isLoading ?
<div className="animate-spin h-5 w-5 border-2 border-white border-r-purple-500 rounded-full"></div>
:
"Enviar"
}
</button>
</form>
}
<h2 className="text-2xl font-bold">Questões</h2>
<div className="w-full h-full flex flex-col gap-5 rounded p-2.5 overflow-y-scroll">
{questions ?
questions.map(({ question, options, answer }, index) => (
<div key={index} className="bg-slate-950 ring-2 ring-white p-2.5 rounded">
{question}<br /><br />
{options.map((option, i) => (
<div key={i}>
{option}
</div>
))}<br />
Resposta correta: {answer}
</div>
))
:
!promptMode && <div className="p-2.5 bg-slate-950 ring-2 ring-white rounded">Escolha os filtros para criar as questões</div>
}
</div>
</section>
<ToastContainer />
</main>
</div>
)
}
export default App
|