File size: 6,799 Bytes
cd1d04b 7d0d187 cd1d04b 1c7f0b8 cd1d04b | 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | import os
import gradio as gr
from groq import Groq
import spaces
from dotenv import load_dotenv
# Load local .env file (useful for local testing)
load_dotenv()
# Safely retrieve the key from environment variables (Hugging Face Secrets)
api_key = os.getenv("Roadmap_Key")
if not api_key:
print("β οΈ Warning: 'Roadmap_Key' environment variable not found. Please set it in Hugging Face Space Secrets.")
else:
print("API Key Loaded Successfully!")
# Initialize the Groq client
client = Groq(api_key=api_key)
@spaces.GPU
def generate_roadmap(domain, level, duration):
# Ensure the client is initialized with an API key before trying to make a call
if not api_key:
return "β Error: API Key missing. Please set 'Roadmap_Key' as a Secret in your Hugging Face Space settings."
if not domain.strip():
return "β Please enter a learning domain."
if not duration.strip():
return "β Please enter the learning duration."
prompt = f"""
You are an expert learning mentor.
Create a detailed roadmap.
Domain: {domain}
Level: {level}
Duration: {duration}
Generate:
# Overview
# Weekly Learning Plan
# Resources
# Practice Websites
# Projects
# Career Advice
"""
try:
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{
"role": "user",
"content": prompt
}
]
)
return response.choices[0].message.content
except Exception as e:
return f"β An error occurred during roadmap generation: {str(e)}"
# Custom CSS styling for the interface
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght=300;400;600;700&display=swap');
/* ---------- Overall Page ---------- */
body{
font-family:'Inter',sans-serif;
background:#0f172a;
}
/* Wider app for desktop */
.gradio-container{
max-width:1500px !important;
width:96% !important;
margin:auto !important;
background:linear-gradient(135deg,#0f172a,#111827,#1e293b);
}
/* ---------- Title ---------- */
.main-title{
text-align:center;
font-size:44px;
font-weight:700;
background:linear-gradient(90deg,#00DBDE,#FC00FF);
-webkit-background-clip:text;
-webkit-text-fill-color:transparent;
margin-bottom:8px;
}
.subtitle{
text-align:center;
color:#cbd5e1;
font-size:18px;
margin-bottom:30px;
}
/* ---------- Glass Cards ---------- */
.glass{
background:rgba(255,255,255,0.08);
backdrop-filter:blur(16px);
border:1px solid rgba(255,255,255,0.12);
border-radius:18px;
padding:22px;
box-shadow:0 8px 30px rgba(0,0,0,.25);
}
/* ---------- Output ---------- */
.output-box{
min-height:650px;
overflow:auto;
}
/* ---------- Buttons ---------- */
button{
border-radius:12px !important;
font-weight:600 !important;
transition:.25s ease;
}
button:hover{
transform:translateY(-2px);
}
.generate-btn{
background:linear-gradient(90deg,#06b6d4,#3b82f6) !important;
color:white !important;
}
.clear-btn{
background:#ef4444 !important;
color:white !important;
}
/* ---------- Examples ---------- */
.examples-table{
width:100% !important;
margin-top:20px;
}
.examples-table table{
width:100% !important;
}
.examples-table td{
white-space:normal !important;
word-break:break-word;
font-size:15px;
padding:12px !important;
}
/* ---------- Inputs ---------- */
textarea,
input{
font-size:16px !important;
}
/* ---------- Markdown Output ---------- */
.prose{
font-size:16px !important;
line-height:1.8 !important;
}
/* ---------- Footer ---------- */
.footer{
text-align:center;
color:#94a3b8;
margin-top:30px;
font-size:14px;
}
/* Hide Gradio Footer */
footer{
visibility:hidden;
}
/* ---------- Desktop ---------- */
@media (min-width:1200px){
.glass{
padding:26px;
}
.output-box{
min-height:700px;
}
}
/* ---------- Mobile ---------- */
@media (max-width:768px){
.main-title{
font-size:34px;
}
.subtitle{
font-size:16px;
}
}
"""
with gr.Blocks(
css=custom_css,
title="AI Learning Roadmap Generator",
theme=gr.themes.Soft()
) as demo:
gr.HTML("""
<div class="main-title">
π AI Learning Roadmap Generator
</div>
<div class="subtitle">
Personalized AI Powered Learning Plans using Groq
</div>
""")
with gr.Row():
with gr.Column(scale=1):
with gr.Group(elem_classes="glass"):
domain = gr.Textbox(
label="π― Learning Domain",
placeholder="Python, AI, Data Science, Flutter..."
)
level = gr.Dropdown(
choices=[
"Beginner",
"Intermediate",
"Advanced"
],
value="Beginner",
label="π Skill Level"
)
duration = gr.Textbox(
label="β³ Learning Duration",
placeholder="Example: 3 Months"
)
with gr.Row():
generate_btn = gr.Button(
"β¨ Generate Roadmap",
elem_classes="generate-btn"
)
clear_btn = gr.ClearButton(
components=[domain, level, duration],
elem_classes="clear-btn"
)
gr.Examples(
examples=[
["Python","Beginner","3 Months"],
["Machine Learning","Intermediate","6 Months"],
["Cyber Security","Beginner","1 Year"],
["Flutter","Beginner","4 Months"],
["Data Science","Advanced","5 Months"]
],
inputs=[domain, level, duration]
)
with gr.Column(scale=2):
with gr.Group(elem_classes=["glass","output-box"]):
roadmap_output = gr.Markdown(
label="π Generated Roadmap"
)
generate_btn.click(
fn=generate_roadmap,
inputs=[
domain,
level,
duration
],
outputs=roadmap_output
)
# Wire up the clear button to reset the output as well
clear_btn.add(roadmap_output)
gr.HTML("""
<div class="footer">
Built with β€οΈ using Python β’ Gradio β’ Groq AI
</div>
""")
# Launching the app on Hugging Face
demo.launch() |