shanzaejaz commited on
Commit
fad33ad
·
verified ·
1 Parent(s): 4b6a735

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -96
app.py CHANGED
@@ -1,129 +1,90 @@
1
  import gradio as gr
2
- import os
3
  from groq import Groq
4
- from reportlab.lib.pagesizes import letter
5
- from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
6
- from reportlab.lib.styles import getSampleStyleSheet
7
-
8
- client = Groq(api_key=os.getenv("GROQ_API_KEY"))
9
- memory = {"roadmap": ""}
10
-
11
- # ---------------- PDF ----------------
12
- def create_pdf(text):
13
- file_path = "roadmap.pdf"
14
-
15
- doc = SimpleDocTemplate(file_path, pagesize=letter)
16
- styles = getSampleStyleSheet()
17
- story = []
18
-
19
- for line in text.split("\n"):
20
- story.append(Paragraph(line, styles["Normal"]))
21
- story.append(Spacer(1, 8))
22
-
23
- doc.build(story)
24
- return file_path
25
 
26
- # ---------------- GENERATE ----------------
27
- def generate(field, hours, months, level):
28
 
 
 
29
  prompt = f"""
30
- Create a professional learning roadmap.
31
 
32
- Field: {field}
33
  Level: {level}
34
- Hours/day: {hours}
35
  Months: {months}
36
 
37
- Make it structured with:
38
- - monthly plan
39
- - weekly goals
40
- - projects
41
- - tools
42
- - final outcome
43
  """
44
 
45
- res = client.chat.completions.create(
46
- model="llama-3.3-70b-versatile",
47
- messages=[{"role": "user", "content": prompt}]
48
- )
 
 
 
 
 
49
 
50
- roadmap = res.choices[0].message.content
51
- memory["roadmap"] = roadmap
52
 
53
- pdf = create_pdf(roadmap)
54
- return roadmap, pdf
 
 
55
 
56
- # ---------------- CHAT ----------------
57
- def chat_with_roadmap(message, history):
 
58
 
59
- if memory["roadmap"] == "":
60
- reply = "Generate a roadmap first."
61
- else:
62
- prompt = f"""
63
- You are helping with this roadmap:
64
 
65
- {memory['roadmap']}
 
 
66
 
67
- User: {message}
68
- """
69
 
 
 
 
70
  res = client.chat.completions.create(
71
- model="llama-3.3-70b-versatile",
72
- messages=[{"role":"user","content":prompt}]
73
  )
74
-
75
  reply = res.choices[0].message.content
 
 
76
 
77
- history = history + [
78
- {"role": "user", "content": message},
79
- {"role": "assistant", "content": reply}
80
- ]
81
-
82
  return history, history
83
 
84
- # ---------------- UI ----------------
85
- with gr.Blocks(theme=gr.themes.Soft(), css="""
86
- #title {text-align:center;font-size:32px;font-weight:700}
87
- """) as app:
88
-
89
- gr.Markdown("<div id='title'>🚀 Roadmap AI</div>")
90
-
91
- with gr.Row():
92
-
93
- # -------- SIDEBAR --------
94
- with gr.Column(scale=1):
95
- gr.Markdown("### ⚙️ Setup")
96
-
97
- field = gr.Textbox(label="Field")
98
- hours = gr.Textbox(label="Hours per day")
99
- months = gr.Textbox(label="Months")
100
- level = gr.Dropdown(["Beginner","Intermediate","Advanced"])
101
-
102
- generate_btn = gr.Button("Generate Roadmap", size="lg")
103
 
104
- pdf_file = gr.File(label="📥 Download PDF")
 
 
105
 
106
- # -------- MAIN PANEL --------
107
- with gr.Column(scale=2):
 
 
 
108
 
109
- roadmap_output = gr.Markdown()
110
 
111
- gr.Markdown("### 💬 Chat with your roadmap")
 
 
112
 
113
- chatbot = gr.Chatbot(type="messages", height=400)
114
- msg = gr.Textbox(placeholder="Ask anything about your roadmap...")
115
- send = gr.Button("Send")
116
 
117
- generate_btn.click(
118
- generate,
119
- inputs=[field, hours, months, level],
120
- outputs=[roadmap_output, pdf_file]
121
- )
122
 
123
- send.click(
124
- chat_with_roadmap,
125
- inputs=[msg, chatbot],
126
- outputs=[chatbot, chatbot]
127
- )
128
 
129
  app.launch()
 
1
  import gradio as gr
 
2
  from groq import Groq
3
+ import os
4
+ from fpdf import FPDF
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
7
 
8
+ # ---------- ROADMAP ----------
9
+ def generate(domain, level, hours, months):
10
  prompt = f"""
11
+ Create a structured learning roadmap.
12
 
13
+ Domain: {domain}
14
  Level: {level}
15
+ Hours per day: {hours}
16
  Months: {months}
17
 
18
+ Make weekly roadmap with projects.
 
 
 
 
 
19
  """
20
 
21
+ try:
22
+ res = client.chat.completions.create(
23
+ model="llama3-70b-8192",
24
+ messages=[{"role": "user", "content": prompt}]
25
+ )
26
+ text = res.choices[0].message.content
27
+ return text
28
+ except Exception as e:
29
+ return f"Error: {str(e)}"
30
 
 
 
31
 
32
+ # ---------- PDF ----------
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
+ for line in text.split("\n"):
42
+ pdf.multi_cell(0, 8, line)
 
 
 
43
 
44
+ path = "roadmap.pdf"
45
+ pdf.output(path)
46
+ return path
47
 
 
 
48
 
49
+ # ---------- CHATBOT ----------
50
+ def chat(message, history):
51
+ try:
52
  res = client.chat.completions.create(
53
+ model="llama3-70b-8192",
54
+ messages=[{"role": "user", "content": message}]
55
  )
 
56
  reply = res.choices[0].message.content
57
+ except Exception as e:
58
+ reply = str(e)
59
 
60
+ history.append((message, reply))
 
 
 
 
61
  return history, history
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
+ # ---------- UI ----------
65
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
66
+ gr.Markdown("# 🚀 AI Roadmap Startup")
67
 
68
+ with gr.Tab("Roadmap Generator"):
69
+ domain = gr.Textbox(label="Domain")
70
+ level = gr.Dropdown(["Beginner","Intermediate","Advanced"])
71
+ hours = gr.Slider(1,10,value=2,label="Hours/day")
72
+ months = gr.Slider(1,12,value=3,label="Months")
73
 
74
+ btn = gr.Button("Generate")
75
 
76
+ output = gr.Textbox(lines=20)
77
+ pdf_btn = gr.Button("Download PDF")
78
+ pdf_file = gr.File()
79
 
80
+ btn.click(generate, [domain,level,hours,months], output)
81
+ pdf_btn.click(download_pdf, output, pdf_file)
 
82
 
83
+ with gr.Tab("AI Chatbot"):
84
+ chatbot = gr.Chatbot()
85
+ msg = gr.Textbox()
86
+ state = gr.State([])
 
87
 
88
+ msg.submit(chat, [msg,state], [chatbot,state])
 
 
 
 
89
 
90
  app.launch()