Rakshitjan commited on
Commit
08e376b
·
verified ·
1 Parent(s): c0f9405

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -2
app.py CHANGED
@@ -35,8 +35,111 @@ if "max_optimizer_iterations" not in st.session_state:
35
 
36
  # Navigation sidebar setup
37
  st.sidebar.title("JEE Roadmap Planner")
38
- page = st.sidebar.radio("Navigation", ["Home", "Roadmap Manager", "Task Analysis"])
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # Function to load initial data
41
  def load_initial_data():
42
  with st.spinner("Loading roadmap data..."):
@@ -578,4 +681,17 @@ elif page == "Task Analysis":
578
 
579
  with col2:
580
  st.subheader("Task Type Distribution")
581
- st.bar_chart(type_counts)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Navigation sidebar setup
37
  st.sidebar.title("JEE Roadmap Planner")
38
+ page = st.sidebar.radio("Navigation", ["Home", "Roadmap Manager", "Task Analysis","Roadmap Chatbot"])
39
 
40
+
41
+ # For roadmap chatbot
42
+ import sqlite3
43
+
44
+ # Function to convert NL query to SQL
45
+ def generate_sql_from_nl(prompt):
46
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
47
+
48
+ table_struct = """
49
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
50
+ date TEXT,
51
+ subject TEXT,
52
+ chapter_name TEXT,
53
+ task_type TEXT,
54
+ time TEXT,
55
+ subtopic TEXT
56
+ """
57
+
58
+ response = client.chat.completions.create(
59
+ model="gpt-4o-mini",
60
+ messages=[
61
+ {"role": "system", "content": "You are an expert at converting natural language to SQL."},
62
+ {"role": "user", "content": f"""Convert this to an SQL query for the 'roadmap' table: {prompt}
63
+ Keep the table structure in mind: {table_struct},
64
+ Also dont include new line statements in the output of the query and just give me the query in the output.
65
+ Do not include ```sql and any other text."""}
66
+ ]
67
+ )
68
+ return response.choices[0].message.content.strip()
69
+
70
+ # Function to convert SQL output to natural language
71
+ def generate_nl_from_sql_output(prompt):
72
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
73
+
74
+ response = client.chat.completions.create(
75
+ model="gpt-4o-mini",
76
+ messages=[
77
+ {"role": "system", "content": """You are a helpful chatbot for JEE roadmap analysis.
78
+ You will be given SQL-fetched roadmap data and must give a natural and interactive explanation to the user."""},
79
+ {"role": "user", "content": f"""Convert this data into interactive output: {prompt}"""}
80
+ ]
81
+ )
82
+ return response.choices[0].message.content.strip()
83
+
84
+ # Function to fetch data from SQLite
85
+ def fetch_data_from_sql(sql_query):
86
+ conn = sqlite3.connect("jee_roadmap.db")
87
+ cursor = conn.cursor()
88
+ cursor.execute(sql_query)
89
+ rows = cursor.fetchall()
90
+ conn.close()
91
+ return rows
92
+
93
+ # Main function for chatbot
94
+ def answer_user_query(prompt):
95
+ initialize_roadmap_db()
96
+ sql = generate_sql_from_nl(prompt)
97
+ rows = fetch_data_from_sql(sql)
98
+ return generate_nl_from_sql_output(rows)
99
+
100
+ def initialize_roadmap_db():
101
+ if not os.path.exists("jee_roadmap.db"):
102
+ try:
103
+ with open("full_roadmap.json") as f:
104
+ roadmap_data = json.load(f)
105
+
106
+ conn = sqlite3.connect("jee_roadmap.db")
107
+ cursor = conn.cursor()
108
+
109
+ cursor.execute("""
110
+ CREATE TABLE IF NOT EXISTS roadmap (
111
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
112
+ date TEXT,
113
+ subject TEXT,
114
+ chapter_name TEXT,
115
+ task_type TEXT,
116
+ time TEXT,
117
+ subtopic TEXT
118
+ )
119
+ """)
120
+
121
+ for day in roadmap_data["schedule"]:
122
+ date = day["date"]
123
+ for subj in day["subjects"]:
124
+ subject = subj["name"]
125
+ for task in subj["tasks"]:
126
+ cursor.execute("""
127
+ INSERT INTO roadmap (date, subject, chapter_name, task_type, time, subtopic)
128
+ VALUES (?, ?, ?, ?, ?, ?)
129
+ """, (
130
+ date,
131
+ subject,
132
+ task["ChapterName"],
133
+ task["type"],
134
+ task["time"],
135
+ task["subtopic"]
136
+ ))
137
+
138
+ conn.commit()
139
+ conn.close()
140
+ print("✅ Database created and data inserted successfully.")
141
+ except Exception as e:
142
+ print(f"⚠️ Error initializing database: {e}")
143
  # Function to load initial data
144
  def load_initial_data():
145
  with st.spinner("Loading roadmap data..."):
 
681
 
682
  with col2:
683
  st.subheader("Task Type Distribution")
684
+ st.bar_chart(type_counts)
685
+ # ---- ROADMAP CHATBOT PAGE ----
686
+ elif page == "Roadmap Chatbot":
687
+ st.title("🤖 Roadmap Chatbot Assistant")
688
+
689
+ user_query = st.text_input("Ask a question about your roadmap:", placeholder="e.g., What are my tasks on 14 Feb 2025?")
690
+
691
+ if st.button("Ask") and user_query:
692
+ with st.spinner("Thinking..."):
693
+ try:
694
+ response = answer_user_query(user_query)
695
+ st.markdown(response)
696
+ except Exception as e:
697
+ st.error(f"Error: {e}")