Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,93 +3,130 @@ from groq import Groq
|
|
| 3 |
import os
|
| 4 |
from fpdf import FPDF
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
| 7 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
-
# ----------
|
|
|
|
|
|
|
| 10 |
def generate(domain, level, hours, months):
|
| 11 |
prompt = f"""
|
| 12 |
Create a structured learning roadmap.
|
|
|
|
| 13 |
Domain: {domain}
|
| 14 |
Level: {level}
|
| 15 |
Hours per day: {hours}
|
| 16 |
Months: {months}
|
| 17 |
|
| 18 |
-
Make a weekly roadmap with projects.
|
| 19 |
"""
|
|
|
|
| 20 |
try:
|
| 21 |
res = client.chat.completions.create(
|
| 22 |
model="llama-3.3-70b-versatile",
|
| 23 |
-
messages=[{"role": "user", "content": prompt}]
|
|
|
|
| 24 |
)
|
| 25 |
return res.choices[0].message.content
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
return f"Error: {str(e)}"
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
def download_pdf(text):
|
| 31 |
if not text or text.startswith("Error"):
|
| 32 |
-
text = "No roadmap generated
|
| 33 |
|
| 34 |
pdf = FPDF()
|
| 35 |
pdf.add_page()
|
| 36 |
pdf.set_font("Arial", size=12)
|
| 37 |
|
| 38 |
-
clean_text = text.encode(
|
|
|
|
| 39 |
for line in clean_text.split("\n"):
|
| 40 |
-
pdf.multi_cell(0,
|
| 41 |
|
| 42 |
path = "roadmap.pdf"
|
| 43 |
pdf.output(path)
|
| 44 |
return path
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
def chat(message, history):
|
| 48 |
if history is None:
|
| 49 |
history = []
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
messages =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
messages.append({"role": "user", "content": message})
|
| 54 |
|
| 55 |
try:
|
| 56 |
res = client.chat.completions.create(
|
| 57 |
model="llama-3.3-70b-versatile",
|
| 58 |
-
messages=messages
|
|
|
|
| 59 |
)
|
| 60 |
reply = res.choices[0].message.content
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
reply = f"Error: {str(e)}"
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
|
|
|
| 75 |
domain = gr.Textbox(label="Domain", placeholder="e.g. Data Science")
|
| 76 |
-
level = gr.Dropdown(
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
months = gr.Slider(1, 12, value=3, label="Months")
|
| 79 |
|
| 80 |
-
btn = gr.Button("Generate")
|
| 81 |
-
output = gr.Textbox(lines=
|
|
|
|
| 82 |
pdf_btn = gr.Button("Download PDF")
|
| 83 |
-
pdf_file = gr.File(
|
| 84 |
|
| 85 |
btn.click(generate, [domain, level, hours, months], output)
|
| 86 |
pdf_btn.click(download_pdf, output, pdf_file)
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
| 93 |
|
|
|
|
|
|
|
| 94 |
if __name__ == "__main__":
|
| 95 |
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 |
+
# -------------------------------
|
| 12 |
+
# ROADMAP GENERATOR
|
| 13 |
+
# -------------------------------
|
| 14 |
def generate(domain, level, hours, months):
|
| 15 |
prompt = f"""
|
| 16 |
Create a structured learning roadmap.
|
| 17 |
+
|
| 18 |
Domain: {domain}
|
| 19 |
Level: {level}
|
| 20 |
Hours per day: {hours}
|
| 21 |
Months: {months}
|
| 22 |
|
| 23 |
+
Make a weekly roadmap with projects and milestones.
|
| 24 |
"""
|
| 25 |
+
|
| 26 |
try:
|
| 27 |
res = client.chat.completions.create(
|
| 28 |
model="llama-3.3-70b-versatile",
|
| 29 |
+
messages=[{"role": "user", "content": prompt}],
|
| 30 |
+
temperature=0.7
|
| 31 |
)
|
| 32 |
return res.choices[0].message.content
|
| 33 |
+
|
| 34 |
except Exception as e:
|
| 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."
|
| 44 |
|
| 45 |
pdf = FPDF()
|
| 46 |
pdf.add_page()
|
| 47 |
pdf.set_font("Arial", size=12)
|
| 48 |
|
| 49 |
+
clean_text = text.encode("latin-1", "ignore").decode("latin-1")
|
| 50 |
+
|
| 51 |
for line in clean_text.split("\n"):
|
| 52 |
+
pdf.multi_cell(0, 8, line)
|
| 53 |
|
| 54 |
path = "roadmap.pdf"
|
| 55 |
pdf.output(path)
|
| 56 |
return path
|
| 57 |
|
| 58 |
+
|
| 59 |
+
# -------------------------------
|
| 60 |
+
# CHATBOT (FIXED)
|
| 61 |
+
# -------------------------------
|
| 62 |
def chat(message, history):
|
| 63 |
if history is None:
|
| 64 |
history = []
|
| 65 |
|
| 66 |
+
# Convert Gradio history → Groq format
|
| 67 |
+
messages = []
|
| 68 |
+
|
| 69 |
+
for user_msg, bot_msg in history:
|
| 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 |
res = client.chat.completions.create(
|
| 77 |
model="llama-3.3-70b-versatile",
|
| 78 |
+
messages=messages,
|
| 79 |
+
temperature=0.7
|
| 80 |
)
|
| 81 |
reply = res.choices[0].message.content
|
| 82 |
+
|
| 83 |
except Exception as e:
|
| 84 |
reply = f"Error: {str(e)}"
|
| 85 |
|
| 86 |
+
history.append((message, reply))
|
| 87 |
+
return "", history
|
| 88 |
+
|
| 89 |
|
| 90 |
+
# -------------------------------
|
| 91 |
+
# UI
|
| 92 |
+
# -------------------------------
|
| 93 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 94 |
|
| 95 |
+
gr.Markdown(
|
| 96 |
+
"""
|
| 97 |
+
# 🚀 AI Roadmap Startup
|
| 98 |
+
Generate learning roadmaps + chat with AI mentor
|
| 99 |
+
"""
|
| 100 |
+
)
|
| 101 |
|
| 102 |
+
# ------------------- TAB 1
|
| 103 |
+
with gr.Tab("📚 Roadmap Generator"):
|
| 104 |
domain = gr.Textbox(label="Domain", placeholder="e.g. Data Science")
|
| 105 |
+
level = gr.Dropdown(
|
| 106 |
+
["Beginner", "Intermediate", "Advanced"],
|
| 107 |
+
value="Beginner",
|
| 108 |
+
label="Level"
|
| 109 |
+
)
|
| 110 |
+
hours = gr.Slider(1, 10, value=2, label="Hours per day")
|
| 111 |
months = gr.Slider(1, 12, value=3, label="Months")
|
| 112 |
|
| 113 |
+
btn = gr.Button("Generate Roadmap")
|
| 114 |
+
output = gr.Textbox(lines=18, label="Your Roadmap")
|
| 115 |
+
|
| 116 |
pdf_btn = gr.Button("Download PDF")
|
| 117 |
+
pdf_file = gr.File()
|
| 118 |
|
| 119 |
btn.click(generate, [domain, level, hours, months], output)
|
| 120 |
pdf_btn.click(download_pdf, output, pdf_file)
|
| 121 |
|
| 122 |
+
# ------------------- TAB 2
|
| 123 |
+
with gr.Tab("🤖 AI Mentor Chat"):
|
| 124 |
+
chatbot = gr.Chatbot(height=450)
|
| 125 |
+
msg = gr.Textbox remembered=False, placeholder="Ask anything..."
|
| 126 |
|
| 127 |
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
| 128 |
|
| 129 |
+
|
| 130 |
+
# -------------------------------
|
| 131 |
if __name__ == "__main__":
|
| 132 |
app.launch()
|