Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,26 +3,23 @@ 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 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
# ROADMAP GENERATOR
|
| 15 |
-
# =========================
|
| 16 |
def generate(domain, level, hours, months):
|
| 17 |
prompt = f"""
|
| 18 |
Create a structured learning roadmap.
|
| 19 |
|
| 20 |
Domain: {domain}
|
| 21 |
Level: {level}
|
| 22 |
-
Hours
|
| 23 |
Months: {months}
|
| 24 |
-
|
| 25 |
-
Make weekly roadmap with projects and milestones.
|
| 26 |
"""
|
| 27 |
|
| 28 |
try:
|
|
@@ -32,7 +29,6 @@ Make weekly roadmap with projects and milestones.
|
|
| 32 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 33 |
{"role": "user", "content": prompt},
|
| 34 |
],
|
| 35 |
-
temperature=0.7,
|
| 36 |
)
|
| 37 |
return res.choices[0].message.content
|
| 38 |
|
|
@@ -40,17 +36,17 @@ Make weekly roadmap with projects and milestones.
|
|
| 40 |
return f"Error: {str(e)}"
|
| 41 |
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
# PDF
|
| 45 |
-
#
|
| 46 |
def download_pdf(text):
|
| 47 |
-
if not text or text.startswith("Error"):
|
| 48 |
-
text = "No roadmap generated."
|
| 49 |
-
|
| 50 |
pdf = FPDF()
|
| 51 |
pdf.add_page()
|
| 52 |
pdf.set_font("Arial", size=12)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
clean = text.encode("latin-1", "ignore").decode("latin-1")
|
| 55 |
|
| 56 |
for line in clean.split("\n"):
|
|
@@ -61,21 +57,19 @@ def download_pdf(text):
|
|
| 61 |
return path
|
| 62 |
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
#
|
| 66 |
-
#
|
| 67 |
def chat(message, history):
|
| 68 |
-
|
| 69 |
-
history = []
|
| 70 |
|
| 71 |
-
# Convert history into Groq format
|
| 72 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
messages.append({"role": "user", "content": message})
|
| 77 |
|
| 78 |
-
# Add user msg to UI history
|
| 79 |
history.append({"role": "user", "content": message})
|
| 80 |
history.append({"role": "assistant", "content": ""})
|
| 81 |
|
|
@@ -84,7 +78,6 @@ def chat(message, history):
|
|
| 84 |
model="llama-3.3-70b-versatile",
|
| 85 |
messages=messages,
|
| 86 |
stream=True,
|
| 87 |
-
temperature=0.7,
|
| 88 |
)
|
| 89 |
|
| 90 |
reply = ""
|
|
@@ -100,21 +93,18 @@ def chat(message, history):
|
|
| 100 |
yield "", history
|
| 101 |
|
| 102 |
|
| 103 |
-
#
|
| 104 |
# UI
|
| 105 |
-
#
|
| 106 |
-
with gr.Blocks(title="AI Roadmap Startup"
|
| 107 |
|
| 108 |
-
gr.Markdown("# 🚀 AI Roadmap Generator +
|
| 109 |
|
| 110 |
with gr.Tabs():
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
with gr.Tab("Roadmap Generator"):
|
| 116 |
-
|
| 117 |
-
domain = gr.Textbox(label="Domain", placeholder="Data Science")
|
| 118 |
level = gr.Dropdown(
|
| 119 |
["Beginner", "Intermediate", "Advanced"],
|
| 120 |
value="Beginner",
|
|
@@ -123,29 +113,22 @@ with gr.Blocks(title="AI Roadmap Startup", theme=gr.themes.Soft()) as app:
|
|
| 123 |
hours = gr.Slider(1, 10, value=2, label="Hours/day")
|
| 124 |
months = gr.Slider(1, 12, value=3, label="Months")
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
|
| 129 |
pdf_btn = gr.Button("Download PDF")
|
| 130 |
pdf_file = gr.File()
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
[domain, level, hours, months],
|
| 135 |
-
roadmap_output,
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
pdf_btn.click(download_pdf, roadmap_output, pdf_file)
|
| 139 |
|
| 140 |
-
#
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
with gr.Tab("AI Mentor Chat"):
|
| 144 |
-
chatbot = gr.Chatbot(type="messages", height=500)
|
| 145 |
msg = gr.Textbox(placeholder="Ask anything...")
|
| 146 |
|
| 147 |
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
| 148 |
|
| 149 |
-
#
|
| 150 |
if __name__ == "__main__":
|
| 151 |
-
app.launch()
|
|
|
|
| 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/day: {hours}
|
| 22 |
Months: {months}
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
|
| 25 |
try:
|
|
|
|
| 29 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 30 |
{"role": "user", "content": prompt},
|
| 31 |
],
|
|
|
|
| 32 |
)
|
| 33 |
return res.choices[0].message.content
|
| 34 |
|
|
|
|
| 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 |
|
| 52 |
for line in clean.split("\n"):
|
|
|
|
| 57 |
return path
|
| 58 |
|
| 59 |
|
| 60 |
+
# =======================
|
| 61 |
+
# CHAT (GRADIO 6 SAFE)
|
| 62 |
+
# =======================
|
| 63 |
def chat(message, history):
|
| 64 |
+
history = history or []
|
|
|
|
| 65 |
|
|
|
|
| 66 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 67 |
+
|
| 68 |
+
for h in history:
|
| 69 |
+
messages.append(h)
|
| 70 |
|
| 71 |
messages.append({"role": "user", "content": message})
|
| 72 |
|
|
|
|
| 73 |
history.append({"role": "user", "content": message})
|
| 74 |
history.append({"role": "assistant", "content": ""})
|
| 75 |
|
|
|
|
| 78 |
model="llama-3.3-70b-versatile",
|
| 79 |
messages=messages,
|
| 80 |
stream=True,
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
reply = ""
|
|
|
|
| 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 + Chat")
|
| 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 |
hours = gr.Slider(1, 10, value=2, label="Hours/day")
|
| 114 |
months = gr.Slider(1, 12, value=3, label="Months")
|
| 115 |
|
| 116 |
+
btn = gr.Button("Generate")
|
| 117 |
+
output = gr.Textbox(lines=18)
|
| 118 |
|
| 119 |
pdf_btn = gr.Button("Download PDF")
|
| 120 |
pdf_file = gr.File()
|
| 121 |
|
| 122 |
+
btn.click(generate, [domain, level, hours, months], output)
|
| 123 |
+
pdf_btn.click(download_pdf, output, pdf_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# -------- CHAT TAB
|
| 126 |
+
with gr.Tab("Chat"):
|
| 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())
|