istiak101 commited on
Commit
4e33689
·
verified ·
1 Parent(s): 9bcb252

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -28
app.py CHANGED
@@ -12,8 +12,8 @@ def load_resources():
12
  load_dotenv()
13
  huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
14
  subprocess.run(["huggingface-cli", "login", "--token", huggingface_token], capture_output=True)
15
- tokenizer = AutoTokenizer.from_pretrained("istiak101/TinyLlama-1.1B-Finetuned")
16
- model = AutoModelForCausalLM.from_pretrained("istiak101/TinyLlama-1.1B-Finetuned")
17
  return model, tokenizer
18
 
19
  def create_test_prompt(question, context, tokenizer):
@@ -169,17 +169,20 @@ else:
169
  st.sidebar.write("No conversations yet. Start one below!")
170
 
171
  # --- New Conversation ---
172
- new_topic = st.sidebar.text_input("New Conversation Name")
173
- if st.sidebar.button("Start New Conversation"):
174
- if new_topic.strip() and new_topic not in st.session_state.chat_sessions:
175
- st.session_state.chat_sessions[new_topic] = []
176
- st.session_state.current_conversation = new_topic
177
- st.sidebar.success(f"Started new conversation: {new_topic}")
178
- st.rerun()
179
- elif not new_topic.strip():
180
- st.sidebar.warning("Please enter a name.")
181
- else:
182
- st.sidebar.warning("Conversation already exists!")
 
 
 
183
 
184
  # --- Main Chat Area ---
185
  if st.session_state.current_conversation:
@@ -192,7 +195,7 @@ if st.session_state.current_conversation:
192
  if msg["role"] == "user":
193
  if st.session_state.edit_mode.get(idx, False):
194
  # Split the message into question and context
195
- question_input, context_input = msg["text"].split("<br>")
196
  # Remove the "Question:" and "Context:" parts from the beginning
197
  question_input = question_input.replace("Question: ", "")
198
  context_input = context_input.replace("Context: ", "")
@@ -206,7 +209,7 @@ if st.session_state.current_conversation:
206
  with col1:
207
  if st.button("✅ Save", key=f"save_{idx}"):
208
  # Combine question and context without the "Question:" and "Context:" labels
209
- new_combined_input = f"{new_question}<br>{new_context}"
210
  msg["text"] = new_combined_input
211
  with st.spinner("Generating response..."):
212
  try:
@@ -255,25 +258,28 @@ if st.session_state.current_conversation:
255
  st.error("❌ Failed to generate PDF.")
256
 
257
  # --- User Prompt ---
258
- question_input = st.text_input("Enter your question:")
259
- context_input = st.text_area("Enter the context:")
260
-
261
- # Button to submit
262
- if st.button("Submit"):
263
- if question_input and context_input:
264
- combined_input = f"Question: {question_input}<br><br>Context: {context_input}"
265
- convo.append({"role": "user", "text": combined_input})
266
-
267
- # Avoid rerunning unnecessarily
268
- st.session_state.chat_sessions[st.session_state.current_conversation] = convo
269
- st.rerun()
 
 
 
270
 
271
  # Display assistant response after rerun
272
  if st.session_state.current_conversation and len(st.session_state.chat_sessions[st.session_state.current_conversation]) % 2 == 1:
273
  convo = st.session_state.chat_sessions[st.session_state.current_conversation]
274
  last_user_msg = convo[-1]["text"]
275
 
276
- question_input, context_input = last_user_msg.split("<br>")
277
  question_input = question_input.replace("Question: ", "")
278
  context_input = context_input.replace("Context: ", "")
279
 
 
12
  load_dotenv()
13
  huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
14
  subprocess.run(["huggingface-cli", "login", "--token", huggingface_token], capture_output=True)
15
+ tokenizer = AutoTokenizer.from_pretrained("istiak101/TinyLlama-1.1B-Chat-v0.6-rag-finetunedv1.0")
16
+ model = AutoModelForCausalLM.from_pretrained("istiak101/TinyLlama-1.1B-Chat-v0.6-rag-finetunedv1.0")
17
  return model, tokenizer
18
 
19
  def create_test_prompt(question, context, tokenizer):
 
169
  st.sidebar.write("No conversations yet. Start one below!")
170
 
171
  # --- New Conversation ---
172
+ with st.sidebar.form(key='new_conversation_form', clear_on_submit=True):
173
+ new_topic = st.text_input("New Conversation Name")
174
+ submit_button = st.form_submit_button("Start New Conversation")
175
+
176
+ if submit_button:
177
+ if new_topic.strip() and new_topic not in st.session_state.chat_sessions:
178
+ st.session_state.chat_sessions[new_topic] = []
179
+ st.session_state.current_conversation = new_topic
180
+ st.sidebar.success(f"Started new conversation: {new_topic}")
181
+ st.rerun()
182
+ elif not new_topic.strip():
183
+ st.sidebar.warning("Please enter a name.")
184
+ else:
185
+ st.sidebar.warning("Conversation already exists!")
186
 
187
  # --- Main Chat Area ---
188
  if st.session_state.current_conversation:
 
195
  if msg["role"] == "user":
196
  if st.session_state.edit_mode.get(idx, False):
197
  # Split the message into question and context
198
+ question_input, context_input = msg["text"].split("<br><br>")
199
  # Remove the "Question:" and "Context:" parts from the beginning
200
  question_input = question_input.replace("Question: ", "")
201
  context_input = context_input.replace("Context: ", "")
 
209
  with col1:
210
  if st.button("✅ Save", key=f"save_{idx}"):
211
  # Combine question and context without the "Question:" and "Context:" labels
212
+ new_combined_input = f"{new_question}<br><br>{new_context}"
213
  msg["text"] = new_combined_input
214
  with st.spinner("Generating response..."):
215
  try:
 
258
  st.error("❌ Failed to generate PDF.")
259
 
260
  # --- User Prompt ---
261
+ with st.form(key="submit_form", clear_on_submit=True):
262
+ question_input = st.text_input("Enter your question:")
263
+ context_input = st.text_area("Enter your context:")
264
+
265
+ # Button to submit the form
266
+ submit_button = st.form_submit_button("Submit")
267
+
268
+ if submit_button:
269
+ if question_input and context_input:
270
+ combined_input = f"Question: {question_input}<br><br>Context: {context_input}"
271
+ convo.append({"role": "user", "text": combined_input})
272
+
273
+ # Avoid rerunning unnecessarily
274
+ st.session_state.chat_sessions[st.session_state.current_conversation] = convo
275
+ st.rerun()
276
 
277
  # Display assistant response after rerun
278
  if st.session_state.current_conversation and len(st.session_state.chat_sessions[st.session_state.current_conversation]) % 2 == 1:
279
  convo = st.session_state.chat_sessions[st.session_state.current_conversation]
280
  last_user_msg = convo[-1]["text"]
281
 
282
+ question_input, context_input = last_user_msg.split("<br><br>")
283
  question_input = question_input.replace("Question: ", "")
284
  context_input = context_input.replace("Context: ", "")
285