Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,23 +3,25 @@ from groq import Groq
|
|
| 3 |
import os
|
| 4 |
from fpdf import FPDF
|
| 5 |
|
| 6 |
-
# =======================
|
| 7 |
# INIT
|
| 8 |
-
# =======================
|
| 9 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 10 |
SYSTEM_PROMPT = "You are a helpful AI mentor."
|
| 11 |
|
| 12 |
-
# =======================
|
| 13 |
-
# ROADMAP
|
| 14 |
-
# =======================
|
| 15 |
def generate(domain, level, hours, months):
|
| 16 |
prompt = f"""
|
| 17 |
Create a structured learning roadmap.
|
| 18 |
|
| 19 |
Domain: {domain}
|
| 20 |
Level: {level}
|
| 21 |
-
Hours
|
| 22 |
Months: {months}
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
|
| 25 |
try:
|
|
@@ -36,16 +38,16 @@ Months: {months}
|
|
| 36 |
return f"Error: {str(e)}"
|
| 37 |
|
| 38 |
|
| 39 |
-
# =======================
|
| 40 |
-
# PDF
|
| 41 |
-
# =======================
|
| 42 |
def download_pdf(text):
|
| 43 |
pdf = FPDF()
|
| 44 |
pdf.add_page()
|
| 45 |
pdf.set_font("Arial", size=12)
|
| 46 |
|
| 47 |
if not text:
|
| 48 |
-
text = "No roadmap."
|
| 49 |
|
| 50 |
clean = text.encode("latin-1", "ignore").decode("latin-1")
|
| 51 |
|
|
@@ -57,19 +59,25 @@ def download_pdf(text):
|
|
| 57 |
return path
|
| 58 |
|
| 59 |
|
| 60 |
-
# =======================
|
| 61 |
-
#
|
| 62 |
-
# =======================
|
| 63 |
def chat(message, history):
|
| 64 |
history = history or []
|
| 65 |
|
|
|
|
| 66 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 67 |
|
| 68 |
-
for
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
messages.append({"role": "user", "content": message})
|
| 72 |
|
|
|
|
| 73 |
history.append({"role": "user", "content": message})
|
| 74 |
history.append({"role": "assistant", "content": ""})
|
| 75 |
|
|
@@ -93,18 +101,18 @@ def chat(message, history):
|
|
| 93 |
yield "", history
|
| 94 |
|
| 95 |
|
| 96 |
-
# =======================
|
| 97 |
# UI
|
| 98 |
-
# =======================
|
| 99 |
with gr.Blocks(title="AI Roadmap Startup") as app:
|
| 100 |
|
| 101 |
-
gr.Markdown("# 🚀 AI Roadmap Generator +
|
| 102 |
|
| 103 |
with gr.Tabs():
|
| 104 |
|
| 105 |
# -------- ROADMAP TAB
|
| 106 |
-
with gr.Tab("Roadmap"):
|
| 107 |
-
domain = gr.Textbox(label="Domain")
|
| 108 |
level = gr.Dropdown(
|
| 109 |
["Beginner", "Intermediate", "Advanced"],
|
| 110 |
value="Beginner",
|
|
@@ -113,22 +121,22 @@ with gr.Blocks(title="AI Roadmap Startup") as app:
|
|
| 113 |
hours = gr.Slider(1, 10, value=2, label="Hours/day")
|
| 114 |
months = gr.Slider(1, 12, value=3, label="Months")
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
pdf_btn = gr.Button("Download PDF")
|
| 120 |
pdf_file = gr.File()
|
| 121 |
|
| 122 |
-
|
| 123 |
-
pdf_btn.click(download_pdf,
|
| 124 |
|
| 125 |
# -------- CHAT TAB
|
| 126 |
-
with gr.Tab("
|
| 127 |
chatbot = gr.Chatbot(height=500)
|
| 128 |
msg = gr.Textbox(placeholder="Ask anything...")
|
| 129 |
|
| 130 |
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
| 131 |
|
| 132 |
-
# =======================
|
| 133 |
if __name__ == "__main__":
|
| 134 |
app.launch(theme=gr.themes.Soft())
|
|
|
|
| 3 |
import os
|
| 4 |
from fpdf import FPDF
|
| 5 |
|
| 6 |
+
# =========================
|
| 7 |
# INIT
|
| 8 |
+
# =========================
|
| 9 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 10 |
SYSTEM_PROMPT = "You are a helpful AI mentor."
|
| 11 |
|
| 12 |
+
# =========================
|
| 13 |
+
# ROADMAP GENERATOR
|
| 14 |
+
# =========================
|
| 15 |
def generate(domain, level, hours, months):
|
| 16 |
prompt = f"""
|
| 17 |
Create a structured learning roadmap.
|
| 18 |
|
| 19 |
Domain: {domain}
|
| 20 |
Level: {level}
|
| 21 |
+
Hours per day: {hours}
|
| 22 |
Months: {months}
|
| 23 |
+
|
| 24 |
+
Make weekly roadmap with projects.
|
| 25 |
"""
|
| 26 |
|
| 27 |
try:
|
|
|
|
| 38 |
return f"Error: {str(e)}"
|
| 39 |
|
| 40 |
|
| 41 |
+
# =========================
|
| 42 |
+
# PDF DOWNLOAD
|
| 43 |
+
# =========================
|
| 44 |
def download_pdf(text):
|
| 45 |
pdf = FPDF()
|
| 46 |
pdf.add_page()
|
| 47 |
pdf.set_font("Arial", size=12)
|
| 48 |
|
| 49 |
if not text:
|
| 50 |
+
text = "No roadmap generated."
|
| 51 |
|
| 52 |
clean = text.encode("latin-1", "ignore").decode("latin-1")
|
| 53 |
|
|
|
|
| 59 |
return path
|
| 60 |
|
| 61 |
|
| 62 |
+
# =========================
|
| 63 |
+
# CHATBOT (METADATA SAFE)
|
| 64 |
+
# =========================
|
| 65 |
def chat(message, history):
|
| 66 |
history = history or []
|
| 67 |
|
| 68 |
+
# ---- Build messages for Groq (NO metadata)
|
| 69 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 70 |
|
| 71 |
+
for item in history:
|
| 72 |
+
if isinstance(item, dict):
|
| 73 |
+
role = item.get("role")
|
| 74 |
+
content = item.get("content")
|
| 75 |
+
if role and content:
|
| 76 |
+
messages.append({"role": role, "content": content})
|
| 77 |
|
| 78 |
messages.append({"role": "user", "content": message})
|
| 79 |
|
| 80 |
+
# ---- Add to UI history
|
| 81 |
history.append({"role": "user", "content": message})
|
| 82 |
history.append({"role": "assistant", "content": ""})
|
| 83 |
|
|
|
|
| 101 |
yield "", history
|
| 102 |
|
| 103 |
|
| 104 |
+
# =========================
|
| 105 |
# UI
|
| 106 |
+
# =========================
|
| 107 |
with gr.Blocks(title="AI Roadmap Startup") as app:
|
| 108 |
|
| 109 |
+
gr.Markdown("# 🚀 AI Roadmap Generator + AI Mentor")
|
| 110 |
|
| 111 |
with gr.Tabs():
|
| 112 |
|
| 113 |
# -------- ROADMAP TAB
|
| 114 |
+
with gr.Tab("Roadmap Generator"):
|
| 115 |
+
domain = gr.Textbox(label="Domain", placeholder="Data Science")
|
| 116 |
level = gr.Dropdown(
|
| 117 |
["Beginner", "Intermediate", "Advanced"],
|
| 118 |
value="Beginner",
|
|
|
|
| 121 |
hours = gr.Slider(1, 10, value=2, label="Hours/day")
|
| 122 |
months = gr.Slider(1, 12, value=3, label="Months")
|
| 123 |
|
| 124 |
+
gen_btn = gr.Button("Generate Roadmap")
|
| 125 |
+
roadmap_output = gr.Textbox(lines=18)
|
| 126 |
|
| 127 |
pdf_btn = gr.Button("Download PDF")
|
| 128 |
pdf_file = gr.File()
|
| 129 |
|
| 130 |
+
gen_btn.click(generate, [domain, level, hours, months], roadmap_output)
|
| 131 |
+
pdf_btn.click(download_pdf, roadmap_output, pdf_file)
|
| 132 |
|
| 133 |
# -------- CHAT TAB
|
| 134 |
+
with gr.Tab("AI Chatbot"):
|
| 135 |
chatbot = gr.Chatbot(height=500)
|
| 136 |
msg = gr.Textbox(placeholder="Ask anything...")
|
| 137 |
|
| 138 |
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
| 139 |
|
| 140 |
+
# =========================
|
| 141 |
if __name__ == "__main__":
|
| 142 |
app.launch(theme=gr.themes.Soft())
|