Spaces:
Sleeping
Sleeping
File size: 2,951 Bytes
b599f70 14e09f9 b599f70 14e09f9 b599f70 14e09f9 b599f70 14e09f9 b599f70 14e09f9 b599f70 14e09f9 b599f70 f4364f1 b599f70 14e09f9 b599f70 14e09f9 b599f70 f4364f1 b599f70 f4364f1 b599f70 14e09f9 b599f70 14e09f9 f4364f1 14e09f9 b599f70 14e09f9 b599f70 14e09f9 b599f70 14e09f9 | 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 | 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()
|