Spaces:
Runtime error
Runtime error
File size: 3,863 Bytes
baf6b49 0a524f0 d5357d5 0a524f0 d528546 dee1dbd d528546 d5357d5 dee1dbd baf6b49 d528546 d5357d5 baf6b49 fad33ad 6db79a4 d5357d5 baf6b49 d528546 d5357d5 d528546 d5357d5 6db79a4 d5357d5 972671b 2144cc9 d5357d5 402e6fd 6db79a4 d5357d5 baf6b49 6db79a4 d528546 d5357d5 baf6b49 dee1dbd d528546 dee1dbd 2144cc9 6db79a4 2144cc9 6db79a4 baf6b49 d5357d5 0a524f0 6db79a4 d528546 d5357d5 dee1dbd ece39b7 d528546 2144cc9 dee1dbd d528546 6db79a4 b89596a d528546 2144cc9 d5357d5 2144cc9 972671b 6db79a4 2144cc9 4b6a735 6db79a4 2144cc9 4b6a735 2144cc9 6db79a4 ece39b7 d528546 6db79a4 d528546 dee1dbd 2144cc9 d528546 2144cc9 dee1dbd d528546 2144cc9 baf6b49 d528546 6db79a4 2144cc9 4b6a735 d528546 ece39b7 dee1dbd d528546 dee1dbd 2144cc9 baf6b49 2144cc9 6db79a4 d528546 402e6fd dee1dbd | 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 | import gradio as gr
from groq import Groq
import os
from fpdf import FPDF
# =========================
# INIT
# =========================
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
SYSTEM_PROMPT = "You are a helpful AI mentor."
# =========================
# ROADMAP GENERATOR
# =========================
def generate(domain, level, hours, months):
prompt = f"""
Create a structured learning roadmap.
Domain: {domain}
Level: {level}
Hours per day: {hours}
Months: {months}
Make weekly roadmap with projects.
"""
try:
res = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
],
)
return res.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# =========================
# PDF DOWNLOAD
# =========================
def download_pdf(text):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
if not text:
text = "No roadmap generated."
clean = text.encode("latin-1", "ignore").decode("latin-1")
for line in clean.split("\n"):
pdf.multi_cell(0, 8, line)
path = "roadmap.pdf"
pdf.output(path)
return path
# =========================
# CHATBOT (METADATA SAFE)
# =========================
def chat(message, history):
history = history or []
# ---- Build messages for Groq (NO metadata)
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
for item in history:
if isinstance(item, dict):
role = item.get("role")
content = item.get("content")
if role and content:
messages.append({"role": role, "content": content})
messages.append({"role": "user", "content": message})
# ---- Add to UI history
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": ""})
try:
stream = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=messages,
stream=True,
)
reply = ""
for chunk in stream:
if chunk.choices[0].delta.content:
token = chunk.choices[0].delta.content
reply += token
history[-1]["content"] = reply
yield "", history
except Exception as e:
history[-1]["content"] = f"Error: {str(e)}"
yield "", history
# =========================
# UI
# =========================
with gr.Blocks(title="AI Roadmap Startup") as app:
gr.Markdown("# 🚀 AI Roadmap Generator + AI Mentor")
with gr.Tabs():
# -------- ROADMAP TAB
with gr.Tab("Roadmap Generator"):
domain = gr.Textbox(label="Domain", placeholder="Data Science")
level = gr.Dropdown(
["Beginner", "Intermediate", "Advanced"],
value="Beginner",
label="Level",
)
hours = gr.Slider(1, 10, value=2, label="Hours/day")
months = gr.Slider(1, 12, value=3, label="Months")
gen_btn = gr.Button("Generate Roadmap")
roadmap_output = gr.Textbox(lines=18)
pdf_btn = gr.Button("Download PDF")
pdf_file = gr.File()
gen_btn.click(generate, [domain, level, hours, months], roadmap_output)
pdf_btn.click(download_pdf, roadmap_output, pdf_file)
# -------- CHAT TAB
with gr.Tab("AI Chatbot"):
chatbot = gr.Chatbot(height=500)
msg = gr.Textbox(placeholder="Ask anything...")
msg.submit(chat, [msg, chatbot], [msg, chatbot])
# =========================
if __name__ == "__main__":
app.launch(theme=gr.themes.Soft())
|