Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,16 @@ from groq import Groq
|
|
| 3 |
import os
|
| 4 |
from fpdf import FPDF
|
| 5 |
|
| 6 |
-
#
|
| 7 |
# INIT GROQ
|
| 8 |
-
#
|
| 9 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
# ROADMAP GENERATOR
|
| 13 |
-
#
|
| 14 |
def generate(domain, level, hours, months):
|
| 15 |
prompt = f"""
|
| 16 |
Create a structured learning roadmap.
|
|
@@ -20,14 +22,17 @@ Level: {level}
|
|
| 20 |
Hours per day: {hours}
|
| 21 |
Months: {months}
|
| 22 |
|
| 23 |
-
Make
|
| 24 |
"""
|
| 25 |
|
| 26 |
try:
|
| 27 |
res = client.chat.completions.create(
|
| 28 |
model="llama-3.3-70b-versatile",
|
| 29 |
-
messages=[
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
return res.choices[0].message.content
|
| 33 |
|
|
@@ -35,9 +40,9 @@ Make a weekly roadmap with projects and milestones.
|
|
| 35 |
return f"Error: {str(e)}"
|
| 36 |
|
| 37 |
|
| 38 |
-
#
|
| 39 |
# PDF DOWNLOAD
|
| 40 |
-
#
|
| 41 |
def download_pdf(text):
|
| 42 |
if not text or text.startswith("Error"):
|
| 43 |
text = "No roadmap generated."
|
|
@@ -46,9 +51,9 @@ def download_pdf(text):
|
|
| 46 |
pdf.add_page()
|
| 47 |
pdf.set_font("Arial", size=12)
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
-
for line in
|
| 52 |
pdf.multi_cell(0, 8, line)
|
| 53 |
|
| 54 |
path = "roadmap.pdf"
|
|
@@ -56,77 +61,91 @@ def download_pdf(text):
|
|
| 56 |
return path
|
| 57 |
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
# CHATBOT (
|
| 61 |
-
#
|
| 62 |
def chat(message, history):
|
| 63 |
if history is None:
|
| 64 |
history = []
|
| 65 |
|
| 66 |
-
# Convert
|
| 67 |
-
messages = []
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
messages.append({"role": "user", "content": user_msg})
|
| 71 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 72 |
|
| 73 |
messages.append({"role": "user", "content": message})
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
try:
|
| 76 |
-
|
| 77 |
model="llama-3.3-70b-versatile",
|
| 78 |
messages=messages,
|
| 79 |
-
|
|
|
|
| 80 |
)
|
| 81 |
-
reply = res.choices[0].message.content
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
-
#
|
| 91 |
# UI
|
| 92 |
-
#
|
| 93 |
-
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 94 |
-
|
| 95 |
-
gr.Markdown(
|
| 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 |
-
with gr.Tab("🤖 AI Mentor Chat"):
|
| 124 |
-
chatbot = gr.Chatbot(height=450)
|
| 125 |
-
msg = gr.Textbox remembered=False, placeholder="Ask anything..."
|
| 126 |
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
|
|
|
| 129 |
|
| 130 |
-
#
|
| 131 |
if __name__ == "__main__":
|
| 132 |
app.launch()
|
|
|
|
| 3 |
import os
|
| 4 |
from fpdf import FPDF
|
| 5 |
|
| 6 |
+
# =========================
|
| 7 |
# INIT GROQ
|
| 8 |
+
# =========================
|
| 9 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 10 |
|
| 11 |
+
SYSTEM_PROMPT = "You are a helpful AI mentor who helps students learn skills and careers."
|
| 12 |
+
|
| 13 |
+
# =========================
|
| 14 |
# ROADMAP GENERATOR
|
| 15 |
+
# =========================
|
| 16 |
def generate(domain, level, hours, months):
|
| 17 |
prompt = f"""
|
| 18 |
Create a structured learning roadmap.
|
|
|
|
| 22 |
Hours per day: {hours}
|
| 23 |
Months: {months}
|
| 24 |
|
| 25 |
+
Make weekly roadmap with projects and milestones.
|
| 26 |
"""
|
| 27 |
|
| 28 |
try:
|
| 29 |
res = client.chat.completions.create(
|
| 30 |
model="llama-3.3-70b-versatile",
|
| 31 |
+
messages=[
|
| 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 |
return f"Error: {str(e)}"
|
| 41 |
|
| 42 |
|
| 43 |
+
# =========================
|
| 44 |
# PDF DOWNLOAD
|
| 45 |
+
# =========================
|
| 46 |
def download_pdf(text):
|
| 47 |
if not text or text.startswith("Error"):
|
| 48 |
text = "No roadmap generated."
|
|
|
|
| 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"):
|
| 57 |
pdf.multi_cell(0, 8, line)
|
| 58 |
|
| 59 |
path = "roadmap.pdf"
|
|
|
|
| 61 |
return path
|
| 62 |
|
| 63 |
|
| 64 |
+
# =========================
|
| 65 |
+
# CHATBOT (GRADIO 6 SAFE)
|
| 66 |
+
# =========================
|
| 67 |
def chat(message, history):
|
| 68 |
if history is None:
|
| 69 |
history = []
|
| 70 |
|
| 71 |
+
# Convert history into Groq format
|
| 72 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 73 |
+
for item in history:
|
| 74 |
+
messages.append(item)
|
|
|
|
|
|
|
| 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 |
+
|
| 82 |
try:
|
| 83 |
+
stream = client.chat.completions.create(
|
| 84 |
model="llama-3.3-70b-versatile",
|
| 85 |
messages=messages,
|
| 86 |
+
stream=True,
|
| 87 |
+
temperature=0.7,
|
| 88 |
)
|
|
|
|
| 89 |
|
| 90 |
+
reply = ""
|
| 91 |
+
for chunk in stream:
|
| 92 |
+
if chunk.choices[0].delta.content:
|
| 93 |
+
token = chunk.choices[0].delta.content
|
| 94 |
+
reply += token
|
| 95 |
+
history[-1]["content"] = reply
|
| 96 |
+
yield "", history
|
| 97 |
|
| 98 |
+
except Exception as e:
|
| 99 |
+
history[-1]["content"] = f"Error: {str(e)}"
|
| 100 |
+
yield "", history
|
| 101 |
|
| 102 |
|
| 103 |
+
# =========================
|
| 104 |
# UI
|
| 105 |
+
# =========================
|
| 106 |
+
with gr.Blocks(title="AI Roadmap Startup", theme=gr.themes.Soft()) as app:
|
| 107 |
+
|
| 108 |
+
gr.Markdown("# 🚀 AI Roadmap Generator + Mentor Chat")
|
| 109 |
+
|
| 110 |
+
with gr.Tabs():
|
| 111 |
+
|
| 112 |
+
# =====================
|
| 113 |
+
# ROADMAP TAB
|
| 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",
|
| 121 |
+
label="Level",
|
| 122 |
+
)
|
| 123 |
+
hours = gr.Slider(1, 10, value=2, label="Hours/day")
|
| 124 |
+
months = gr.Slider(1, 12, value=3, label="Months")
|
| 125 |
|
| 126 |
+
gen_btn = gr.Button("Generate Roadmap")
|
| 127 |
+
roadmap_output = gr.Textbox(lines=18)
|
| 128 |
|
| 129 |
+
pdf_btn = gr.Button("Download PDF")
|
| 130 |
+
pdf_file = gr.File()
|
| 131 |
|
| 132 |
+
gen_btn.click(
|
| 133 |
+
generate,
|
| 134 |
+
[domain, level, hours, months],
|
| 135 |
+
roadmap_output,
|
| 136 |
+
)
|
| 137 |
|
| 138 |
+
pdf_btn.click(download_pdf, roadmap_output, pdf_file)
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
# =====================
|
| 141 |
+
# CHAT TAB
|
| 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()
|