import gradio as gr import requests import os from docx import Document from datetime import datetime # ✅ Load API key from environment GROQ_API_KEY = os.getenv("GROQ_API_KEY") # ✅ Groq API URL and Headers GROQ_URL = "https://api.groq.com/openai/v1/chat/completions" HEADERS = { "Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json" } # ✅ Prompt builder def build_prompt(topic): return ( f"Create a detailed step-by-step learning roadmap to become a {topic}.\n" "Structure it into 3 levels: Beginner, Intermediate, Advanced.\n" "For each level, list 4–6 essential skills.\n" "For each skill, include a short explanation and 1–2 trusted learning resources " "(e.g. websites, books, YouTube links)." ) # ✅ Call Groq API def generate_roadmap(topic): prompt = build_prompt(topic) payload = { "model": "llama3-70b-8192", "messages": [ {"role": "system", "content": "You are an expert career roadmap advisor."}, {"role": "user", "content": prompt} ], "temperature": 0.7 } response = requests.post(GROQ_URL, headers=HEADERS, json=payload) if response.status_code == 200: result = response.json() content = result["choices"][0]["message"]["content"] return content else: return f"❌ Error: {response.status_code} - {response.text}" # ✅ Save output to Word document def save_as_docx(text, topic): doc = Document() doc.add_heading(f"{topic} Learning Roadmap", 0) for line in text.split('\n'): doc.add_paragraph(line) filename = f"roadmap_{topic.lower().replace(' ', '_')}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx" downloads_dir = "downloads" os.makedirs(downloads_dir, exist_ok=True) filepath = os.path.join(downloads_dir, filename) doc.save(filepath) return filepath # ✅ Main function combining roadmap + file def generate_and_download(topic): roadmap = generate_roadmap(topic) filepath = save_as_docx(roadmap, topic) return roadmap, filepath # ✅ Gradio UI with gr.Blocks() as demo: gr.Markdown("## 🗺️ AI-Powered Custom Roadmap Generator") gr.Markdown("👨‍💻 Developed by **Najaf Ali**") gr.Markdown("Enter any career goal or skill (e.g. 'Data Scientist', 'Full Stack Developer') and get a full learning roadmap powered by LLaMA3 on Groq.") with gr.Row(): topic_input = gr.Textbox(label="📝 Enter your desired skill or career path", placeholder="e.g. Cybersecurity Expert") with gr.Row(): generate_button = gr.Button("🚀 Generate Roadmap") with gr.Row(): roadmap_output = gr.Markdown() with gr.Row(): download_output = gr.File(label="⬇️ Download .docx Roadmap") generate_button.click(fn=generate_and_download, inputs=topic_input, outputs=[roadmap_output, download_output]) demo.launch()