Spaces:
Sleeping
Sleeping
File size: 1,296 Bytes
f9766bc 53c5e59 f9766bc af0fb63 f9766bc | 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 | """
flashcard_generator.py
Generates flashcard sets from a topic using Groq LLM.
"""
import json
import os
from groq import Groq
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
def generate_flashcards(topic: str, level: str, num_cards: int = 10) -> list:
"""Return a list of {front, back} flashcard dicts."""
prompt = (
f"Generate {num_cards} flashcards for '{topic}' at {level} level.\n"
"Return ONLY a JSON object with key 'cards' (array).\n"
"Each card needs:\n"
" - front: a short question or term (max 15 words)\n"
" - back: the answer or definition (max 40 words)\n"
"JSON only, no markdown, no backticks, no extra text."
)
try:
client = Groq(api_key=GROQ_API_KEY)
r = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": prompt}],
max_tokens=1500,
)
raw = r.choices[0].message.content.strip()
if raw.startswith("```"):
raw = raw.split("```", 2)[1]
if raw.startswith("json"):
raw = raw[4:]
data = json.loads(raw.strip())
return data.get("cards", [])
except Exception as e:
return [{"front": "Generation failed", "back": str(e)}]
|