Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,14 @@ from groq import Groq
|
|
| 3 |
import os
|
| 4 |
from fpdf import FPDF
|
| 5 |
|
| 6 |
-
# Initialize Groq client
|
|
|
|
| 7 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
# ---------- ROADMAP GENERATOR ----------
|
| 10 |
def generate(domain, level, hours, months):
|
| 11 |
prompt = f"""
|
| 12 |
Create a structured learning roadmap.
|
| 13 |
-
|
| 14 |
Domain: {domain}
|
| 15 |
Level: {level}
|
| 16 |
Hours per day: {hours}
|
|
@@ -18,28 +18,29 @@ Months: {months}
|
|
| 18 |
|
| 19 |
Make a weekly roadmap with projects.
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
try:
|
| 23 |
res = client.chat.completions.create(
|
| 24 |
model="llama-3.3-70b-versatile",
|
| 25 |
messages=[{"role": "user", "content": prompt}]
|
| 26 |
)
|
| 27 |
-
|
| 28 |
-
return text
|
| 29 |
except Exception as e:
|
| 30 |
return f"Error: {str(e)}"
|
| 31 |
|
| 32 |
# ---------- PDF DOWNLOAD ----------
|
| 33 |
def download_pdf(text):
|
| 34 |
-
if not text:
|
| 35 |
-
text = "No roadmap generated"
|
| 36 |
|
| 37 |
pdf = FPDF()
|
| 38 |
pdf.add_page()
|
| 39 |
pdf.set_font("Arial", size=12)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
path = "roadmap.pdf"
|
| 45 |
pdf.output(path)
|
|
@@ -47,11 +48,14 @@ def download_pdf(text):
|
|
| 47 |
|
| 48 |
# ---------- AI CHATBOT ----------
|
| 49 |
def chat(message, history):
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
messages = []
|
| 52 |
-
for
|
| 53 |
-
messages.append({"role": "user", "content":
|
| 54 |
-
messages.append({"role": "assistant", "content":
|
| 55 |
|
| 56 |
# Add current user message
|
| 57 |
messages.append({"role": "user", "content": message})
|
|
@@ -63,38 +67,36 @@ def chat(message, history):
|
|
| 63 |
)
|
| 64 |
reply = res.choices[0].message.content
|
| 65 |
except Exception as e:
|
| 66 |
-
reply = str(e)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
history.append((message, reply))
|
| 70 |
-
return
|
| 71 |
|
| 72 |
# ---------- GRADIO UI ----------
|
| 73 |
with gr.Blocks() as app:
|
| 74 |
gr.Markdown("# 🚀 AI Roadmap Startup")
|
| 75 |
|
| 76 |
-
# ----- Roadmap Generator Tab -----
|
| 77 |
with gr.Tab("Roadmap Generator"):
|
| 78 |
-
domain = gr.Textbox(label="Domain")
|
| 79 |
-
level = gr.Dropdown(["Beginner","Intermediate","Advanced"], label="Level")
|
| 80 |
hours = gr.Slider(1, 10, value=2, label="Hours/day")
|
| 81 |
months = gr.Slider(1, 12, value=3, label="Months")
|
| 82 |
|
| 83 |
btn = gr.Button("Generate")
|
| 84 |
-
output = gr.Textbox(lines=
|
| 85 |
pdf_btn = gr.Button("Download PDF")
|
| 86 |
pdf_file = gr.File(label="PDF File")
|
| 87 |
|
| 88 |
btn.click(generate, [domain, level, hours, months], output)
|
| 89 |
pdf_btn.click(download_pdf, output, pdf_file)
|
| 90 |
|
| 91 |
-
# ----- AI Chatbot Tab -----
|
| 92 |
with gr.Tab("AI Chatbot"):
|
| 93 |
chatbot = gr.Chatbot()
|
| 94 |
-
msg = gr.Textbox(label="Your Message")
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
msg.submit(chat, [msg,
|
| 98 |
|
| 99 |
-
|
| 100 |
-
app.launch()
|
|
|
|
| 3 |
import os
|
| 4 |
from fpdf import FPDF
|
| 5 |
|
| 6 |
+
# Initialize Groq client
|
| 7 |
+
# Ensure your environment variable is set!
|
| 8 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 9 |
|
| 10 |
# ---------- ROADMAP GENERATOR ----------
|
| 11 |
def generate(domain, level, hours, months):
|
| 12 |
prompt = f"""
|
| 13 |
Create a structured learning roadmap.
|
|
|
|
| 14 |
Domain: {domain}
|
| 15 |
Level: {level}
|
| 16 |
Hours per day: {hours}
|
|
|
|
| 18 |
|
| 19 |
Make a weekly roadmap with projects.
|
| 20 |
"""
|
|
|
|
| 21 |
try:
|
| 22 |
res = client.chat.completions.create(
|
| 23 |
model="llama-3.3-70b-versatile",
|
| 24 |
messages=[{"role": "user", "content": prompt}]
|
| 25 |
)
|
| 26 |
+
return res.choices[0].message.content
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
return f"Error: {str(e)}"
|
| 29 |
|
| 30 |
# ---------- PDF DOWNLOAD ----------
|
| 31 |
def download_pdf(text):
|
| 32 |
+
if not text or text.startswith("Error"):
|
| 33 |
+
text = "No roadmap generated or error occurred."
|
| 34 |
|
| 35 |
pdf = FPDF()
|
| 36 |
pdf.add_page()
|
| 37 |
pdf.set_font("Arial", size=12)
|
| 38 |
|
| 39 |
+
# Clean text to avoid encoding issues with FPDF
|
| 40 |
+
clean_text = text.encode('latin-1', 'ignore').decode('latin-1')
|
| 41 |
+
|
| 42 |
+
for line in clean_text.split("\n"):
|
| 43 |
+
pdf.multi_cell(0, 10, line)
|
| 44 |
|
| 45 |
path = "roadmap.pdf"
|
| 46 |
pdf.output(path)
|
|
|
|
| 48 |
|
| 49 |
# ---------- AI CHATBOT ----------
|
| 50 |
def chat(message, history):
|
| 51 |
+
"""
|
| 52 |
+
Gradio history is now often a list of dicts: [{"role": "user", "content": "..."}, ...]
|
| 53 |
+
"""
|
| 54 |
+
# Build the message list for Groq API
|
| 55 |
messages = []
|
| 56 |
+
for entry in history:
|
| 57 |
+
messages.append({"role": "user", "content": entry[0]})
|
| 58 |
+
messages.append({"role": "assistant", "content": entry[1]})
|
| 59 |
|
| 60 |
# Add current user message
|
| 61 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 67 |
)
|
| 68 |
reply = res.choices[0].message.content
|
| 69 |
except Exception as e:
|
| 70 |
+
reply = f"Error: {str(e)}"
|
| 71 |
|
| 72 |
+
# Update history: Gradio Chatbot expects a list of tuples/lists [(user, bot)]
|
| 73 |
history.append((message, reply))
|
| 74 |
+
return "", history # Clear the input box and return updated history
|
| 75 |
|
| 76 |
# ---------- GRADIO UI ----------
|
| 77 |
with gr.Blocks() as app:
|
| 78 |
gr.Markdown("# 🚀 AI Roadmap Startup")
|
| 79 |
|
|
|
|
| 80 |
with gr.Tab("Roadmap Generator"):
|
| 81 |
+
domain = gr.Textbox(label="Domain", placeholder="e.g. Data Science")
|
| 82 |
+
level = gr.Dropdown(["Beginner","Intermediate","Advanced"], label="Level", value="Beginner")
|
| 83 |
hours = gr.Slider(1, 10, value=2, label="Hours/day")
|
| 84 |
months = gr.Slider(1, 12, value=3, label="Months")
|
| 85 |
|
| 86 |
btn = gr.Button("Generate")
|
| 87 |
+
output = gr.Textbox(lines=15, label="Generated Roadmap")
|
| 88 |
pdf_btn = gr.Button("Download PDF")
|
| 89 |
pdf_file = gr.File(label="PDF File")
|
| 90 |
|
| 91 |
btn.click(generate, [domain, level, hours, months], output)
|
| 92 |
pdf_btn.click(download_pdf, output, pdf_file)
|
| 93 |
|
|
|
|
| 94 |
with gr.Tab("AI Chatbot"):
|
| 95 |
chatbot = gr.Chatbot()
|
| 96 |
+
msg = gr.Textbox(label="Your Message", placeholder="Type and press Enter...")
|
| 97 |
+
|
| 98 |
+
# We use the chatbot component itself to store history
|
| 99 |
+
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
| 100 |
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
app.launch()
|