| import os |
| import gradio as gr |
| from groq import Groq |
| from reportlab.lib.pagesizes import A4 |
| from reportlab.pdfgen import canvas |
| import textwrap |
|
|
|
|
| |
| |
| |
| 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) |
|
|
|
|
| |
| |
| |
| 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 |
|
|
|
|
| |
| |
| |
| 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: |
|
|
| |
| if text_obj.getY() < margin_y: |
|
|
| |
| c.drawText(text_obj) |
| c.showPage() |
|
|
| |
| text_obj = new_text_object() |
|
|
| text_obj.textLine(line) |
|
|
|
|
| |
| text_obj.textLine("") |
|
|
|
|
| |
| c.drawText(text_obj) |
|
|
|
|
| |
| c.setFont("Helvetica-Bold", 10) |
| c.drawCentredString(width / 2, 25, "Made by Kubra π»") |
|
|
| c.save() |
|
|
| return file_path |
|
|
|
|
| |
| |
| |
| 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; |
| } |
| } |
| """ |
|
|
|
|
| |
| |
| |
| 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** π» |
| """ |
| ) |
|
|
|
|
| |
| |
| |
| app.launch(server_name="0.0.0.0", server_port=7860) |
|
|