AbuSaleh28 commited on
Commit
cd1d04b
Β·
verified Β·
1 Parent(s): 29d1200

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +318 -0
app.py ADDED
@@ -0,0 +1,318 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from groq import Groq
4
+ from dotenv import load_dotenv
5
+
6
+ # Load local .env file (useful for local testing)
7
+ load_dotenv()
8
+
9
+ # Safely retrieve the key from environment variables (Hugging Face Secrets)
10
+ api_key = os.getenv("Roadmap_Key")
11
+
12
+ if not api_key:
13
+ print("⚠️ Warning: 'Roadmap_Key' environment variable not found. Please set it in Hugging Face Space Secrets.")
14
+ else:
15
+ print("API Key Loaded Successfully!")
16
+
17
+ # Initialize the Groq client
18
+ client = Groq(api_key=api_key)
19
+
20
+ def generate_roadmap(domain, level, duration):
21
+ # Ensure the client is initialized with an API key before trying to make a call
22
+ if not api_key:
23
+ return "❌ Error: API Key missing. Please set 'Roadmap_Key' as a Secret in your Hugging Face Space settings."
24
+
25
+ if not domain.strip():
26
+ return "❌ Please enter a learning domain."
27
+
28
+ if not duration.strip():
29
+ return "❌ Please enter the learning duration."
30
+
31
+ prompt = f"""
32
+ You are an expert learning mentor.
33
+
34
+ Create a detailed roadmap.
35
+
36
+ Domain: {domain}
37
+
38
+ Level: {level}
39
+
40
+ Duration: {duration}
41
+
42
+ Generate:
43
+
44
+ # Overview
45
+
46
+ # Weekly Learning Plan
47
+
48
+ # Resources
49
+
50
+ # Practice Websites
51
+
52
+ # Projects
53
+
54
+ # Career Advice
55
+ """
56
+
57
+ try:
58
+ response = client.chat.completions.create(
59
+ model="llama-3.3-70b-versatile",
60
+ messages=[
61
+ {
62
+ "role": "user",
63
+ "content": prompt
64
+ }
65
+ ]
66
+ )
67
+ return response.choices[0].message.content
68
+ except Exception as e:
69
+ return f"❌ An error occurred during roadmap generation: {str(e)}"
70
+
71
+ # Custom CSS styling for the interface
72
+ custom_css = """
73
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght=300;400;600;700&display=swap');
74
+
75
+ /* ---------- Overall Page ---------- */
76
+
77
+ body{
78
+ font-family:'Inter',sans-serif;
79
+ background:#0f172a;
80
+ }
81
+
82
+ /* Wider app for desktop */
83
+ .gradio-container{
84
+ max-width:1500px !important;
85
+ width:96% !important;
86
+ margin:auto !important;
87
+ background:linear-gradient(135deg,#0f172a,#111827,#1e293b);
88
+ }
89
+
90
+ /* ---------- Title ---------- */
91
+
92
+ .main-title{
93
+ text-align:center;
94
+ font-size:44px;
95
+ font-weight:700;
96
+ background:linear-gradient(90deg,#00DBDE,#FC00FF);
97
+ -webkit-background-clip:text;
98
+ -webkit-text-fill-color:transparent;
99
+ margin-bottom:8px;
100
+ }
101
+
102
+ .subtitle{
103
+ text-align:center;
104
+ color:#cbd5e1;
105
+ font-size:18px;
106
+ margin-bottom:30px;
107
+ }
108
+
109
+ /* ---------- Glass Cards ---------- */
110
+
111
+ .glass{
112
+ background:rgba(255,255,255,0.08);
113
+ backdrop-filter:blur(16px);
114
+ border:1px solid rgba(255,255,255,0.12);
115
+ border-radius:18px;
116
+ padding:22px;
117
+ box-shadow:0 8px 30px rgba(0,0,0,.25);
118
+ }
119
+
120
+ /* ---------- Output ---------- */
121
+
122
+ .output-box{
123
+ min-height:650px;
124
+ overflow:auto;
125
+ }
126
+
127
+ /* ---------- Buttons ---------- */
128
+
129
+ button{
130
+ border-radius:12px !important;
131
+ font-weight:600 !important;
132
+ transition:.25s ease;
133
+ }
134
+
135
+ button:hover{
136
+ transform:translateY(-2px);
137
+ }
138
+
139
+ .generate-btn{
140
+ background:linear-gradient(90deg,#06b6d4,#3b82f6) !important;
141
+ color:white !important;
142
+ }
143
+
144
+ .clear-btn{
145
+ background:#ef4444 !important;
146
+ color:white !important;
147
+ }
148
+
149
+ /* ---------- Examples ---------- */
150
+
151
+ .examples-table{
152
+ width:100% !important;
153
+ margin-top:20px;
154
+ }
155
+
156
+ .examples-table table{
157
+ width:100% !important;
158
+ }
159
+
160
+ .examples-table td{
161
+ white-space:normal !important;
162
+ word-break:break-word;
163
+ font-size:15px;
164
+ padding:12px !important;
165
+ }
166
+
167
+ /* ---------- Inputs ---------- */
168
+
169
+ textarea,
170
+ input{
171
+ font-size:16px !important;
172
+ }
173
+
174
+ /* ---------- Markdown Output ---------- */
175
+
176
+ .prose{
177
+ font-size:16px !important;
178
+ line-height:1.8 !important;
179
+ }
180
+
181
+ /* ---------- Footer ---------- */
182
+
183
+ .footer{
184
+ text-align:center;
185
+ color:#94a3b8;
186
+ margin-top:30px;
187
+ font-size:14px;
188
+ }
189
+
190
+ /* Hide Gradio Footer */
191
+
192
+ footer{
193
+ visibility:hidden;
194
+ }
195
+
196
+ /* ---------- Desktop ---------- */
197
+
198
+ @media (min-width:1200px){
199
+
200
+ .glass{
201
+ padding:26px;
202
+ }
203
+
204
+ .output-box{
205
+ min-height:700px;
206
+ }
207
+
208
+ }
209
+
210
+ /* ---------- Mobile ---------- */
211
+
212
+ @media (max-width:768px){
213
+
214
+ .main-title{
215
+ font-size:34px;
216
+ }
217
+
218
+ .subtitle{
219
+ font-size:16px;
220
+ }
221
+
222
+ }
223
+ """
224
+
225
+ with gr.Blocks(
226
+ css=custom_css,
227
+ title="AI Learning Roadmap Generator",
228
+ theme=gr.themes.Soft()
229
+ ) as demo:
230
+
231
+ gr.HTML("""
232
+ <div class="main-title">
233
+ πŸš€ AI Learning Roadmap Generator
234
+ </div>
235
+
236
+ <div class="subtitle">
237
+ Personalized AI Powered Learning Plans using Groq
238
+ </div>
239
+ """)
240
+
241
+ with gr.Row():
242
+
243
+ with gr.Column(scale=1):
244
+
245
+ with gr.Group(elem_classes="glass"):
246
+
247
+ domain = gr.Textbox(
248
+ label="🎯 Learning Domain",
249
+ placeholder="Python, AI, Data Science, Flutter..."
250
+ )
251
+
252
+ level = gr.Dropdown(
253
+ choices=[
254
+ "Beginner",
255
+ "Intermediate",
256
+ "Advanced"
257
+ ],
258
+ value="Beginner",
259
+ label="πŸ“š Skill Level"
260
+ )
261
+
262
+ duration = gr.Textbox(
263
+ label="⏳ Learning Duration",
264
+ placeholder="Example: 3 Months"
265
+ )
266
+
267
+ with gr.Row():
268
+
269
+ generate_btn = gr.Button(
270
+ "✨ Generate Roadmap",
271
+ elem_classes="generate-btn"
272
+ )
273
+
274
+ clear_btn = gr.ClearButton(
275
+ components=[domain, level, duration],
276
+ elem_classes="clear-btn"
277
+ )
278
+
279
+ gr.Examples(
280
+ examples=[
281
+ ["Python","Beginner","3 Months"],
282
+ ["Machine Learning","Intermediate","6 Months"],
283
+ ["Cyber Security","Beginner","1 Year"],
284
+ ["Flutter","Beginner","4 Months"],
285
+ ["Data Science","Advanced","5 Months"]
286
+ ],
287
+ inputs=[domain, level, duration]
288
+ )
289
+
290
+ with gr.Column(scale=2):
291
+
292
+ with gr.Group(elem_classes=["glass","output-box"]):
293
+
294
+ roadmap_output = gr.Markdown(
295
+ label="πŸ“„ Generated Roadmap"
296
+ )
297
+
298
+ generate_btn.click(
299
+ fn=generate_roadmap,
300
+ inputs=[
301
+ domain,
302
+ level,
303
+ duration
304
+ ],
305
+ outputs=roadmap_output
306
+ )
307
+
308
+ # Wire up the clear button to reset the output as well
309
+ clear_btn.add(roadmap_output)
310
+
311
+ gr.HTML("""
312
+ <div class="footer">
313
+ Built with ❀️ using Python β€’ Gradio β€’ Groq AI
314
+ </div>
315
+ """)
316
+
317
+ # Launching the app on Hugging Face
318
+ demo.launch()