Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,41 @@ import streamlit as st
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from xhtml2pdf import pisa
|
| 6 |
import io
|
| 7 |
-
from
|
|
|
|
| 8 |
|
| 9 |
# --- Load Model Resources ---
|
| 10 |
def load_resources():
|
| 11 |
load_dotenv()
|
| 12 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 13 |
subprocess.run(["huggingface-cli", "login", "--token", huggingface_token], capture_output=True)
|
| 14 |
-
tokenizer = AutoTokenizer.from_pretrained("istiak101/TinyLlama-1.1B-
|
| 15 |
-
model = AutoModelForCausalLM.from_pretrained("istiak101/TinyLlama-1.1B-
|
| 16 |
return model, tokenizer
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# --- Store model and tokenizer in session state ---
|
| 19 |
if "llama_model" not in st.session_state or "llama_tokenizer" not in st.session_state:
|
| 20 |
model, tokenizer = load_resources()
|
|
@@ -24,13 +48,21 @@ if "llama_model" not in st.session_state or "llama_tokenizer" not in st.session_
|
|
| 24 |
st.set_page_config(page_title="Ask Wikipedia", page_icon="π", layout="wide")
|
| 25 |
|
| 26 |
def get_llama_response(query):
|
| 27 |
-
model = st.session_state.llama_model
|
| 28 |
-
tokenizer = st.session_state.llama_tokenizer
|
| 29 |
|
| 30 |
-
inputs = tokenizer(query, return_tensors="pt")
|
| 31 |
-
outputs = model.generate(**inputs, max_new_tokens=300)
|
| 32 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# --- PDF Generation ---
|
| 36 |
def generate_pdf(convo, topic):
|
|
@@ -159,15 +191,32 @@ if st.session_state.current_conversation:
|
|
| 159 |
with st.container():
|
| 160 |
if msg["role"] == "user":
|
| 161 |
if st.session_state.edit_mode.get(idx, False):
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
col1, col2 = st.columns([1, 1])
|
| 164 |
with col1:
|
| 165 |
if st.button("β
Save", key=f"save_{idx}"):
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
if idx + 1 < len(convo) and convo[idx + 1]["role"] == "assistant":
|
| 169 |
convo[idx + 1]["text"] = new_response
|
| 170 |
st.session_state.edit_mode[idx] = False
|
|
|
|
| 171 |
st.rerun()
|
| 172 |
with col2:
|
| 173 |
if st.button("β Cancel", key=f"cancel_{idx}"):
|
|
@@ -201,26 +250,42 @@ if st.session_state.current_conversation:
|
|
| 201 |
if st.button("π₯ Export Conversation as PDF"):
|
| 202 |
pdf_bytes = generate_pdf(convo, st.session_state.current_conversation)
|
| 203 |
if pdf_bytes:
|
| 204 |
-
st.download_button("Download PDF", pdf_bytes, file_name=
|
| 205 |
else:
|
| 206 |
st.error("β Failed to generate PDF.")
|
| 207 |
|
| 208 |
# --- User Prompt ---
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
# Display assistant response after rerun
|
| 215 |
if st.session_state.current_conversation and len(st.session_state.chat_sessions[st.session_state.current_conversation]) % 2 == 1:
|
| 216 |
convo = st.session_state.chat_sessions[st.session_state.current_conversation]
|
| 217 |
last_user_msg = convo[-1]["text"]
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
with st.spinner("Generating response..."):
|
| 220 |
try:
|
| 221 |
-
assistant_reply = get_llama_response(
|
|
|
|
| 222 |
except Exception as e:
|
| 223 |
assistant_reply = f"β οΈ Failed to generate response"
|
| 224 |
|
|
|
|
| 225 |
convo.append({"role": "assistant", "text": assistant_reply})
|
| 226 |
-
st.
|
|
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from xhtml2pdf import pisa
|
| 6 |
import io
|
| 7 |
+
from textwrap import dedent
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline # for loading llama tokenizer
|
| 9 |
|
| 10 |
# --- Load Model Resources ---
|
| 11 |
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-Finetunedv1.0")
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained("istiak101/TinyLlama-1.1B-Finetunedv1.0")
|
| 17 |
return model, tokenizer
|
| 18 |
|
| 19 |
+
def create_test_prompt(question, context, tokenizer):
|
| 20 |
+
prompt = dedent(
|
| 21 |
+
f"""
|
| 22 |
+
{question}
|
| 23 |
+
|
| 24 |
+
Information:
|
| 25 |
+
|
| 26 |
+
```
|
| 27 |
+
{context}
|
| 28 |
+
```
|
| 29 |
+
"""
|
| 30 |
+
)
|
| 31 |
+
messages = [
|
| 32 |
+
{
|
| 33 |
+
"role": "system",
|
| 34 |
+
"content": "Use only the information to answer the question",
|
| 35 |
+
},
|
| 36 |
+
{"role": "user", "content": prompt},
|
| 37 |
+
]
|
| 38 |
+
return tokenizer.apply_chat_template(
|
| 39 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
# --- Store model and tokenizer in session state ---
|
| 43 |
if "llama_model" not in st.session_state or "llama_tokenizer" not in st.session_state:
|
| 44 |
model, tokenizer = load_resources()
|
|
|
|
| 48 |
st.set_page_config(page_title="Ask Wikipedia", page_icon="π", layout="wide")
|
| 49 |
|
| 50 |
def get_llama_response(query):
|
| 51 |
+
# model = st.session_state.llama_model
|
| 52 |
+
# tokenizer = st.session_state.llama_tokenizer
|
| 53 |
|
| 54 |
+
# inputs = tokenizer(query, return_tensors="pt")
|
| 55 |
+
# outputs = model.generate(**inputs, max_new_tokens=300)
|
| 56 |
+
# response = tokenizer.decode(outputs[0]["generated_text"], skip_special_tokens=True)
|
| 57 |
+
pipe = pipeline(
|
| 58 |
+
task="text-generation",
|
| 59 |
+
model=model,
|
| 60 |
+
tokenizer=tokenizer,
|
| 61 |
+
max_new_tokens=128,
|
| 62 |
+
return_full_text=False,
|
| 63 |
+
)
|
| 64 |
+
outputs = pipe(prompt)
|
| 65 |
+
return outputs[0]["generated_text"]
|
| 66 |
|
| 67 |
# --- PDF Generation ---
|
| 68 |
def generate_pdf(convo, topic):
|
|
|
|
| 191 |
with st.container():
|
| 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: ", "")
|
| 199 |
+
|
| 200 |
+
# Provide separate inputs for the question and context
|
| 201 |
+
new_question = st.text_input("Edit your question:", value=question_input, key=f"edit_question_{idx}")
|
| 202 |
+
new_context = st.text_area("Edit your context:", value=context_input, key=f"edit_context_{idx}")
|
| 203 |
+
|
| 204 |
+
prompt = create_test_prompt(new_question, new_context, st.session_state.llama_tokenizer)
|
| 205 |
col1, col2 = st.columns([1, 1])
|
| 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:
|
| 213 |
+
new_response = get_llama_response(prompt)
|
| 214 |
+
except:
|
| 215 |
+
new_response = "Failed to retrieve summary."
|
| 216 |
if idx + 1 < len(convo) and convo[idx + 1]["role"] == "assistant":
|
| 217 |
convo[idx + 1]["text"] = new_response
|
| 218 |
st.session_state.edit_mode[idx] = False
|
| 219 |
+
st.session_state.chat_sessions[st.session_state.current_conversation] = convo
|
| 220 |
st.rerun()
|
| 221 |
with col2:
|
| 222 |
if st.button("β Cancel", key=f"cancel_{idx}"):
|
|
|
|
| 250 |
if st.button("π₯ Export Conversation as PDF"):
|
| 251 |
pdf_bytes = generate_pdf(convo, st.session_state.current_conversation)
|
| 252 |
if pdf_bytes:
|
| 253 |
+
st.download_button("Download PDF", pdf_bytes, file_name="AskWikipedia_Conversation.pdf", mime="application/pdf")
|
| 254 |
else:
|
| 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>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 |
+
|
| 280 |
+
prompt = create_test_prompt(question_input, context_input, st.session_state.llama_tokenizer)
|
| 281 |
with st.spinner("Generating response..."):
|
| 282 |
try:
|
| 283 |
+
assistant_reply = get_llama_response(prompt)
|
| 284 |
+
|
| 285 |
except Exception as e:
|
| 286 |
assistant_reply = f"β οΈ Failed to generate response"
|
| 287 |
|
| 288 |
+
# Now, append the response after the spinner
|
| 289 |
convo.append({"role": "assistant", "text": assistant_reply})
|
| 290 |
+
st.session_state.chat_sessions[st.session_state.current_conversation] = convo
|
| 291 |
+
st.rerun()
|