Kubratech3's picture
Update app.py
9c97441 verified
Raw
History Blame Contribute Delete
5.31 kB
import os
import gradio as gr
from groq import Groq
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
import textwrap
# =========================
# Load API Key
# =========================
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)
# =========================
# Generate Roadmap
# =========================
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
# =========================
# Generate PDF (FULLY FIXED)
# =========================
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:
# Page break check
if text_obj.getY() < margin_y:
# Draw current page FIRST
c.drawText(text_obj)
c.showPage()
# Start new page
text_obj = new_text_object()
text_obj.textLine(line)
# Space after paragraph
text_obj.textLine("")
# Draw last page
c.drawText(text_obj)
# Footer
c.setFont("Helvetica-Bold", 10)
c.drawCentredString(width / 2, 25, "Made by Kubra 🌻")
c.save()
return file_path
# =========================
# Custom CSS
# =========================
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;
}
}
"""
# =========================
# UI
# =========================
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** 🌻
"""
)
# =========================
# Launch
# =========================
app.launch(server_name="0.0.0.0", server_port=7860)