File size: 2,336 Bytes
c2b3c1e
 
6fe9572
c2b3c1e
 
 
 
 
 
 
 
0c14d47
c2b3c1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
import json
from groq import Groq
import os

STYLE_TIMES = {
    "Summary Notes": "3-5 min",
    "Detailed Explanation": "8-12 min",
    "Bullet Points": "2-4 min",
    "Concept Map": "5-7 min"
}

GROQ_API_KEY = os.environ.get("GROQ_API_KEY")


def generate_content(topic, level, content_style, focus=""):
    focus_note = " Focus specifically on: " + focus + "." if focus else ""
    prompt = (
        "Generate structured study notes for '" + topic + "' at " + level + " level "
        "in the style of " + content_style + "." + focus_note + "\n\n"
        "Return ONLY a JSON object with:\n"
        "- sections: array of 3 objects each with 'title' and 'content'\n"
        "- key_terms: array of 4-6 objects each with 'term' and 'definition'\n"
        "- summary: one string (1-2 sentences)\n\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()
        # Strip markdown fences if model adds them anyway
        if raw.startswith("```"):
            raw = raw.split("```", 2)[1]
            if raw.startswith("json"):
                raw = raw[4:]
        data = json.loads(raw.strip())
        return {
            "topic": topic.title(),
            "level": level,
            "style": content_style,
            "read_time": STYLE_TIMES.get(content_style, "5 min"),
            "sections": data["sections"],
            "key_terms": data["key_terms"],
            "summary": data["summary"],
            "ai_generated": True,
        }
    except Exception as e:
        # Graceful fallback so the app never crashes
        return {
            "topic": topic.title(),
            "level": level,
            "style": content_style,
            "read_time": STYLE_TIMES.get(content_style, "5 min"),
            "sections": [
                {"title": "Overview", "content": f"Study notes for {topic} at {level} level could not be generated. Please try again."},
            ],
            "key_terms": [],
            "summary": f"Content generation failed: {str(e)}",
            "ai_generated": False,
        }