File size: 5,305 Bytes
0cef3b8
 
 
e72d1f6
 
9de1a4f
0cef3b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a240e27
 
0cef3b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3dbc1f
0cef3b8
 
 
3e8013c
 
 
0cef3b8
 
3e8013c
 
0cef3b8
 
e72d1f6
9c97441
e72d1f6
 
 
3e8013c
 
 
a240e27
e72d1f6
 
9c97441
e72d1f6
 
9de1a4f
 
a3dbc1f
9c97441
 
 
 
 
 
 
 
 
 
9de1a4f
e72d1f6
9de1a4f
 
 
 
 
9c97441
9de1a4f
3e8013c
9c97441
 
 
9de1a4f
9c97441
9de1a4f
9c97441
 
9de1a4f
9c97441
 
 
 
 
 
 
 
 
 
 
9de1a4f
e72d1f6
 
9c97441
a3dbc1f
e72d1f6
9c97441
d6c1db9
e72d1f6
9de1a4f
e72d1f6
 
 
 
 
 
0cef3b8
9c97441
0cef3b8
 
 
 
 
 
 
a3dbc1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cef3b8
 
 
 
9c97441
0cef3b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3dbc1f
 
0cef3b8
e72d1f6
a3dbc1f
e72d1f6
 
 
0cef3b8
 
 
a3dbc1f
0cef3b8
 
9c97441
e72d1f6
 
a3dbc1f
e72d1f6
 
 
 
 
 
 
9c97441
e72d1f6
 
 
0cef3b8
 
 
 
 
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import os
import gradio as gr
from groq import Groq
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
import textwrap


# =========================
# Load API Key
# =========================
api_key = os.getenv("GROQ_API_KEY")

if not api_key:
    raise ValueError("❌ GROQ_API_KEY not found. Add it in HF Secrets.")

client = Groq(api_key=api_key)


# =========================
# Generate Roadmap
# =========================
def generate_roadmap(domain, level, time):

    if not domain or not time:
        msg = "❌ Please fill all fields."
        return msg, msg

    prompt = f"""
Create a detailed learning roadmap.
Domain: {domain}
Skill Level: {level}
Time Available: {time}
Include:
- Weekly plan
- Resources
- Projects
- Tips
"""

    try:
        response = client.chat.completions.create(

            model="llama-3.1-8b-instant",

            messages=[
                {"role": "system", "content": "You are an expert mentor."},
                {"role": "user", "content": prompt}
            ],

            max_tokens=3000,
            temperature=0.7
        )

        result = response.choices[0].message.content

        return result, result

    except Exception as e:
        err = f"❌ Error: {e}"
        return err, err


# =========================
# Generate PDF (FULLY FIXED)
# =========================
def create_pdf(text):

    if not text:
        return None

    file_path = "ai_roadmap.pdf"

    c = canvas.Canvas(file_path, pagesize=A4)

    width, height = A4

    margin_x = 40
    margin_y = 40

    max_chars = 90


    def new_text_object():
        t = c.beginText(margin_x, height - margin_y)
        t.setFont("Helvetica", 11)
        return t


    text_obj = new_text_object()


    paragraphs = text.split("\n\n")

    for para in paragraphs:

        for raw_line in para.split("\n"):

            wrapped = textwrap.wrap(raw_line, max_chars)

            if not wrapped:
                wrapped = [""]


            for line in wrapped:

                # Page break check
                if text_obj.getY() < margin_y:

                    # Draw current page FIRST
                    c.drawText(text_obj)
                    c.showPage()

                    # Start new page
                    text_obj = new_text_object()

                text_obj.textLine(line)


        # Space after paragraph
        text_obj.textLine("")


    # Draw last page
    c.drawText(text_obj)


    # Footer
    c.setFont("Helvetica-Bold", 10)
    c.drawCentredString(width / 2, 25, "Made by Kubra 🌻")

    c.save()

    return file_path


# =========================
# Custom CSS
# =========================
custom_css = """
html, body {
    margin: 0;
    padding: 0;
    font-family: Arial, sans-serif;
}

/* Light Mode */
@media (prefers-color-scheme: light) {
    html,
    body,
    .gradio-container,
    .app,
    .wrap,
    .container {
        background: linear-gradient(to right, #dff5e3, #b7e4c7) !important;
        color: #1a1a1a !important;
    }

    input,
    textarea,
    select {
        background: #f6fff8 !important;
        color: #1a1a1a !important;
        border: 1px solid #9dd9b1 !important;
        border-radius: 6px;
    }

    button {
        background: #6fcf97 !important;
        color: #103822 !important;
        border-radius: 8px !important;
        font-weight: 600;
    }

    button:hover {
        background: #5bbd84 !important;
    }
}

/* Dark Mode */
@media (prefers-color-scheme: dark) {
    html,
    body,
    .gradio-container,
    .app,
    .wrap,
    .container {
        background: linear-gradient(to right, #667eea, #764ba2) !important;
        color: #ffffff !important;
    }

    input,
    textarea,
    select {
        background: #1e1e2f !important;
        color: #ffffff !important;
        border: 1px solid #444 !important;
    }

    button {
        background: #7c83fd !important;
        color: white !important;
        border-radius: 8px !important;
        font-weight: 600;
    }

    button:hover {
        background: #6a70e0 !important;
    }
}
"""


# =========================
# UI
# =========================
with gr.Blocks(css=custom_css) as app:

    gr.Markdown(
        """
        # πŸ“˜ AI Learning Roadmap Generator πŸš€
        Generate a personalized learning roadmap in seconds.
        """
    )

    with gr.Row():
        domain = gr.Textbox(label="πŸ“š Domain / Field")
        level = gr.Dropdown(
            ["Beginner", "Intermediate", "Advanced"],
            label="🎯 Skill Level"
        )

    time = gr.Textbox(label="⏳ Time to Learn (e.g. 3 Months)")

    generate_btn = gr.Button("✨ Generate Roadmap")

    output = gr.Markdown()
    hidden_text = gr.Textbox(visible=False)

    pdf_btn = gr.Button("πŸ“₯ Download as PDF")

    pdf_file = gr.File(label="Download PDF")


    generate_btn.click(
        generate_roadmap,
        inputs=[domain, level, time],
        outputs=[output, hidden_text]
    )


    pdf_btn.click(
        create_pdf,
        inputs=hidden_text,
        outputs=pdf_file
    )


    gr.Markdown(
        """
        ---
         **Made by Kubra** 🌻  
        """
    )


# =========================
# Launch
# =========================
app.launch(server_name="0.0.0.0", server_port=7860)