-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
253 lines (163 loc) · 5.94 KB
/
Copy pathapp.py
File metadata and controls
253 lines (163 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""
Main Streamlit application for the AI Study Assistant.
"""
import streamlit as st
from src.constants import (
PAGE_ICON,
PAGE_TITLE,
LAYOUT,
PROJECT_NAME,
)
from src.logger import get_logger
from src.pipeline.notes_pipeline import notes_pipeline
from src.pipeline.parallel_pipeline import parallel_pipeline
from src.pipeline.sentiment_pipeline import sentiment_pipeline
from src.pipeline.conditional_pipeline import conditional_pipeline
logger = get_logger(__name__)
# ==========================================================
# Streamlit Configuration
# ==========================================================
st.set_page_config(
page_title=PAGE_TITLE,
page_icon=PAGE_ICON,
layout=LAYOUT,
)
# ==========================================================
# Session State Initialization
# ==========================================================
if "notes" not in st.session_state:
st.session_state.notes = ""
if "questions" not in st.session_state:
st.session_state.questions = ""
if "answers" not in st.session_state:
st.session_state.answers = ""
if "feedback" not in st.session_state:
st.session_state.feedback = ""
if "sentiment" not in st.session_state:
st.session_state.sentiment = ""
if "response" not in st.session_state:
st.session_state.response = ""
# ==========================================================
# Sidebar
# ==========================================================
with st.sidebar:
st.title(" AI Study Assistant")
st.markdown("---")
st.markdown(
"""
### About
This AI Study Assistant uses
- LangChain
- Google Gemini
- RunnableParallel
- RunnableBranch
- Streamlit
### Features
Generate Notes
Generate Questions
Generate Answers
Sentiment Analysis
AI Feedback Response
"""
)
st.markdown("---")
st.info("Built using LangChain + Gemini")
# ==========================================================
# Main Page
# ==========================================================
st.title("📖 AI Study Assistant using LangChain")
st.write(
"Generate notes, interview questions, answers, and receive AI feedback."
)
st.markdown("---")
topic = st.text_input(
"Enter Study Topic",
placeholder="Example: Machine Learning",
)
# ==========================================================
# Generate Button
# ==========================================================
if st.button("🚀 Generate Study Material", use_container_width=True):
if topic.strip() == "":
st.warning("Please enter a study topic.")
st.stop()
try:
logger.info("Topic entered: %s", topic)
with st.spinner("Generating Notes..."):
notes = notes_pipeline.generate_notes(topic)
st.session_state.notes = notes
with st.spinner("Generating Questions & Answers..."):
result = parallel_pipeline.generate_questions_answers(
notes
)
st.session_state.questions = result["questions"]
st.session_state.answers = result["answers"]
st.success("Study material generated successfully!")
except Exception as e:
logger.exception("Generation failed.")
st.error(str(e))
# ==========================================================
# Display Notes
# ==========================================================
if st.session_state.notes:
st.header("📝 Generated Notes")
st.markdown(st.session_state.notes)
# ==========================================================
# Questions
# ==========================================================
if st.session_state.questions:
st.header("❓ Interview / Exam Questions")
st.markdown(st.session_state.questions)
# ==========================================================
# Answers
# ==========================================================
if st.session_state.answers:
st.header("✅ Answers")
st.markdown(st.session_state.answers)
# ==========================================================
# Feedback
# ==========================================================
if st.session_state.notes:
st.markdown("---")
st.header("💬 Feedback")
feedback = st.text_area(
"How were the generated notes?",
placeholder="Example: The notes were very helpful.",
)
if st.button("Submit Feedback"):
if feedback.strip() == "":
st.warning("Please enter feedback.")
st.stop()
try:
st.session_state.feedback = feedback
with st.spinner("Analyzing feedback..."):
sentiment = (
sentiment_pipeline.analyze_sentiment(
feedback
)
)
st.session_state.sentiment = sentiment
with st.spinner("Generating response..."):
response = (
conditional_pipeline.generate_response(
feedback,
sentiment,
)
)
st.session_state.response = response
st.success("Feedback processed successfully!")
except Exception as e:
logger.exception("Feedback processing failed.")
st.error(str(e))
# ==========================================================
# Sentiment
# ==========================================================
if st.session_state.sentiment:
st.header("📊 Sentiment")
st.success(st.session_state.sentiment.capitalize())
# ==========================================================
# AI Response
# ==========================================================
if st.session_state.response:
st.header("🤖 AI Response")
st.info(st.session_state.response)