-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
200 lines (158 loc) · 9.29 KB
/
Copy pathapp.py
File metadata and controls
200 lines (158 loc) · 9.29 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
import streamlit as st
import tempfile
from mylib.helper import load_pdf, extract_questions, get_model_questions, get_docx, get_chunks, store_in_db, get_relevant_chunks, explain_concept, get_search_results
from langchain.agents import create_agent
from langchain_google_genai import ChatGoogleGenerativeAI
# Introductory content
st.markdown("""
# ResQ's here to Rescue! 🫡
## What can ResQ do for You :
📜 Generate a predictive model Question Paper based on previous question papers
🧑🏻🏫 Explain concepts from the context of your textbook
🎯 Solve the assignments by learning from textbook
""")
st.write("\n\n")
# Defining streamlit tabs
model_qp, concepts, assignments, resources = st.tabs(["Generate Model QP", "Explain Concepts", "Solve Assignments", "Get Resources"])
# ----------------------------------------------------------------------------------------------------------------------------
# Model Question Paper Generator
# ----------------------------------------------------------------------------------------------------------------------------
with model_qp:
st.write("How many Question Papers are there ? ")
no_of_qp = st.number_input("Input number here..", value=1, min_value=1, max_value=5)
st.session_state.qp_content = ""
for i in range(no_of_qp):
file = st.file_uploader(f"Upload the {i}{"st" if i == 1 else "th"} QP :")
if file is not None:
with st.spinner("Loading the contents...") :
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp :
tmp.write(file.getbuffer())
temp_pdf_path = tmp.name
st.session_state.qp_content += load_pdf(temp_pdf_path)
st.success("Loaded contents")
loaded_qp_content = st.session_state.qp_content
if loaded_qp_content :
with st.spinner("Starting Prediction..."):
st.session_state.agent = create_agent(
model=ChatGoogleGenerativeAI(
model = "gemini-2.5-flash",
google_api_key = st.secrets["GEMINI_API_KEY"]
),
tools = [extract_questions, get_model_questions, get_docx],
system_prompt="You are a helpful assistant for students"
)
agent = st.session_state.agent
st.session_state.response = agent.invoke({
"messages" : [{
"role" : "user",
"content" : f"Please predict the model question paper for the question papers content : {loaded_qp_content}. first extract the clear questions from the content using a tool, then use that extracted questions for analysis and prediction for model question paper. Ultimately i need the file path of the word document that contains the model question papers. Just give me the final file path in string format. Do not add unnecessary words in output, the output string should wholly represent the file path only."
}]
})
file_path = st.session_state.response["messages"][-1].content[0]['text']
with open(file_path, "rb") as file:
st.download_button(
label="Download Saved File",
data=file.read(), # Read the file's bytes into memory here
file_name="Model_Questions.docx",
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
# ----------------------------------------------------------------------------------------------------------------------------
# Concepts Explaining Assistant
# ----------------------------------------------------------------------------------------------------------------------------
with concepts:
st.write("Please upload your Textbook : ")
file = st.file_uploader("Upload here...", type=".pdf")
if file is not None :
if "file" not in st.session_state:
with st.spinner("Loading contents will take time as the file is very large...") :
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp :
tmp.write(file.getbuffer())
temp_pdf_path = tmp.name
st.session_state.text = load_pdf(temp_pdf_path)
st.session_state.file = temp_pdf_path
st.success("Loaded content successfully...")
with st.spinner("Adding to knowledge base..."):
text = st.session_state.text
if text :
chunks = get_chunks(text)
store_in_db(chunks)
st.success("Stored succesfully")
st.session_state.stored = True
if "llm" not in st.session_state:
st.session_state.llm = ChatGoogleGenerativeAI(
model = "gemini-2.5-flash",
google_api_key = st.secrets["GEMINI_API_KEY"]
)
if "agent" not in st.session_state:
st.session_state.agent = create_agent(
model = st.session_state.llm,
tools=[get_relevant_chunks, explain_concept],
system_prompt="You are a helpful student assistant and a master explainer who gives suitable real world examples just by taking the contexts from textbook. Just ensure the final output is in markdown format. Make good use of markdown visual elements."
)
st.session_state.query = st.text_input("What concept would you like to learn ?")
if st.session_state.query :
with st.spinner("Generating response...") :
agent = st.session_state.agent
query = st.session_state.query
prompt = f"You are a helpful study assistant. You are given a user query and you have to first collect the relevant chunks from the vector store and then based on the relevant chunks, you have to explain the concepts mentioned in the query. give the final output in string of markdown format. If you do not get any relevant chunks you can answer based on your own knowledge about the subject.\nquery : {query}"
response = agent.invoke(
{
"messages" : [
{
"role" : "user",
"content" : prompt
}
]
}
)
st.markdown(response["messages"][-1].content[0]['text'])
# ----------------------------------------------------------------------------------------------------------------------------
# Assignments Helper
# ----------------------------------------------------------------------------------------------------------------------------
with assignments :
st.write("\n")
st.session_state.selected = st.selectbox("What is the file-type of assignments ?", ["PDF", "Plain Text"])
if st.session_state.selected == "PDF" :
file = st.file_uploader("Upload the PDF here...", type = ".pdf")
if file is not None:
if "file" not in st.session_state:
with st.spinner("Loading contents will take time as the file is very large...") :
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp :
tmp.write(file.getbuffer())
temp_pdf_path = tmp.name
st.session_state.text = load_pdf(temp_pdf_path)
st.session_state.file = temp_pdf_path
st.success("Loaded content successfully...")
elif (st.session_state.selected == "Plain Text") :
st.session_state.text = st.text_area("Paste the text Here...")
if "text" not in st.session_state or st.session_state.text.strip() == "":
st.warning("Please Insert text")
if "text" in st.session_state :
st.session_state.words = st.slider("Please set the approximate number of words for the size of the answers", min_value=50, max_value=500, step=50,)
if "words" in st.session_state :
with st.spinner("Processing...") :
if "llm" not in st.session_state:
st.session_state.llm = ChatGoogleGenerativeAI(
model = "gemini-2.5-flash",
google_api_key = st.secrets["GEMINI_API_KEY"]
)
response = st.session_state.llm.invoke(
f"""
You are a helpful study assistant. Please solve all the given questions in standard answer formats each in about {st.session_state.words} words.
Directly start from 1st question.
Do not repeat the qeustoins in response.
Just give the sequence number of question and then immediately the answer.
Questions : {st.session_state.text}
"""
)
st.markdown(response.content)
with resources:
st.session_state.query = st.text_input("What do you want to search for ?")
with st.spinner("Getting the results for you... ") :
if "query" in st.session_state and st.session_state.query.strip() != "":
response = get_search_results(st.session_state.query)
for result in response["results"] :
st.markdown(f"""
{result['title']}
```{result['url']}```
""")