Front page

Story 03 · Digital School Hackathon

AI study helper

Flashcards and quizzes on demand


A study app built at a hackathon. Users sign up, then generate flashcards and quizzes through the Hugging Face AI API.

It includes user settings and a MySQL database.

Stack
PHP, MySQL, JavaScript, Hugging Face API
Event
Digital School Hackathon

Asking the model for flashcards

flashcards.php · lines 17 to 37

The student’s summary goes to the model with instructions to answer in JSON between <json> tags; the code pulls that block out and decodes it into cards.

flashcards.phpHackathon
$messages = [    [        'role' => 'system',        'content' => 'You are a helpful assistant for students. Generate flashcards in JSON format for academic topics. Output ONLY valid JSON in this format: [{"question":"...","answer":"..."}, ...]. Surround your JSON with <json>...</json> tags.'    ],    [        'role' => 'user',        'content' => "Create a set of flashcards (question/term and answer/explanation) to help me study the following topic:\n\n" . $summary    ]]; $response = callHuggingFaceAPI($messages, $user_id, $db, 1000); // Extract JSON between <json>...</json>if (preg_match('/<json>(.*?)<\/json>/is', $response, $m)) {    $json_str = trim($m[1]);} else {    $json_str = $response;} $cards = json_decode($json_str, true);

Calling the Hugging Face router

huggingface_api.php · lines 31 to 49

Requests go to the Hugging Face router’s chat-completions endpoint with the model and token each user saves in their settings.

huggingface_api.phpHackathon
// Hugging Face Router API endpoint - uses OpenAI-compatible chat completions format$endpoint = "https://router.huggingface.co/v1/chat/completions"; // Prepare payload in OpenAI-compatible format// Router will automatically select the best provider for this model$payload = [    'model' => $model,    'messages' => $messages,    'max_tokens' => $max_tokens,    'temperature' => 0.7,    'stream' => false]; $ch = curl_init($endpoint);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, [    'Content-Type: application/json',    'Authorization: Bearer ' . $token]);