Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,209 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
import torch
|
| 4 |
import os
|
| 5 |
-
import
|
| 6 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# Run the huggingface-cli login command from the Python script using subprocess
|
| 19 |
-
subprocess.run(["huggingface-cli", "login", "--token", huggingface_token])
|
| 20 |
-
# Load the pre-trained Llama3 model (or your fine-tuned model)
|
| 21 |
-
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B")
|
| 22 |
-
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B")
|
| 23 |
-
print("Model and tokenizer loaded.")
|
| 24 |
-
return model, tokenizer
|
| 25 |
-
|
| 26 |
-
# Load resources only once and cache them
|
| 27 |
-
if "model" not in st.session_state:
|
| 28 |
-
with st.spinner("Loading the model... Please wait!"):
|
| 29 |
-
model, tokenizer = load_resources()
|
| 30 |
-
st.session_state.model = model
|
| 31 |
-
st.session_state.tokenizer = tokenizer
|
| 32 |
-
|
| 33 |
-
# Streamlit UI Setup
|
| 34 |
-
st.title("Llama3-based Chatbot")
|
| 35 |
-
st.write("Ask a question, and I will provide an answer based on the model.")
|
| 36 |
-
|
| 37 |
-
# User input for the query
|
| 38 |
-
user_query = st.text_input("Your Query:", "")
|
| 39 |
-
|
| 40 |
-
if user_query:
|
| 41 |
-
with st.spinner("Generating response..."):
|
| 42 |
-
# Retrieve the tokenizer from session state
|
| 43 |
-
tokenizer = st.session_state.tokenizer
|
| 44 |
-
model = st.session_state.model
|
| 45 |
-
|
| 46 |
-
# Tokenize the input query
|
| 47 |
-
inputs = tokenizer(user_query, return_tensors="pt")
|
| 48 |
-
|
| 49 |
-
# Generate a response from the model
|
| 50 |
-
with torch.no_grad():
|
| 51 |
-
outputs = model.generate(inputs["input_ids"], max_length=150, num_return_sequences=1)
|
| 52 |
-
|
| 53 |
-
# Decode the response
|
| 54 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 55 |
-
|
| 56 |
-
# Display the response
|
| 57 |
-
st.write(f"Model Response: {response}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
import wikipedia
|
| 5 |
+
from xhtml2pdf import pisa
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
st.set_page_config(page_title="Ask Wikipedia", page_icon="π", layout="wide")
|
| 12 |
+
|
| 13 |
+
# --- Wikipedia Summary ---
|
| 14 |
+
def get_wikipedia_summary(query):
|
| 15 |
+
try:
|
| 16 |
+
return wikipedia.summary(query, sentences=2)
|
| 17 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 18 |
+
return f"Your query is ambiguous, here are some options: {e.options}"
|
| 19 |
+
except wikipedia.exceptions.HTTPTimeoutError:
|
| 20 |
+
return "Request timed out. Please try again later."
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"An error occurred: {e}"
|
| 23 |
+
|
| 24 |
+
# --- PDF Generation ---
|
| 25 |
+
def generate_pdf(convo, topic):
|
| 26 |
+
html = f"<h2>{topic}</h2><hr>"
|
| 27 |
+
for msg in convo:
|
| 28 |
+
if msg["role"] == "user":
|
| 29 |
+
html += f"<p><strong>π€ You:</strong> {msg['text']}</p>"
|
| 30 |
+
elif msg["role"] == "assistant":
|
| 31 |
+
html += f"<p><strong>π Wikipedia:</strong> {msg['text']}</p>"
|
| 32 |
+
|
| 33 |
+
result = io.BytesIO()
|
| 34 |
+
pisa_status = pisa.CreatePDF(io.StringIO(html), dest=result)
|
| 35 |
+
if pisa_status.err:
|
| 36 |
+
return None
|
| 37 |
+
return result
|
| 38 |
+
|
| 39 |
+
# --- Session Init ---
|
| 40 |
+
if "chat_sessions" not in st.session_state:
|
| 41 |
+
st.session_state.chat_sessions = {}
|
| 42 |
+
if "current_conversation" not in st.session_state:
|
| 43 |
+
st.session_state.current_conversation = None
|
| 44 |
+
if "edit_mode" not in st.session_state:
|
| 45 |
+
st.session_state.edit_mode = {}
|
| 46 |
+
|
| 47 |
+
# --- App Title ---
|
| 48 |
+
st.title("π¬ Ask Wikipedia - Your Knowledge Companion")
|
| 49 |
+
|
| 50 |
+
# --- Custom CSS ---
|
| 51 |
+
st.markdown("""
|
| 52 |
+
<style>
|
| 53 |
+
.chat-wrapper {
|
| 54 |
+
display: flex;
|
| 55 |
+
flex-direction: column;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
.message-row {
|
| 59 |
+
display: flex;
|
| 60 |
+
align-items: flex-end;
|
| 61 |
+
justify-content: flex-end;
|
| 62 |
+
margin-top: 10px;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
.user-bubble {
|
| 66 |
+
background-color: rgba(0, 200, 83, 0.1);
|
| 67 |
+
color: var(--text-color);
|
| 68 |
+
padding: 15px;
|
| 69 |
+
border-radius: 15px;
|
| 70 |
+
max-width: 80%;
|
| 71 |
+
border: 1px solid rgba(0, 200, 83, 0.4);
|
| 72 |
+
position: relative;
|
| 73 |
+
margin-bottom: 20px;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
.assistant-bubble {
|
| 77 |
+
background-color: rgba(3, 169, 244, 0.1);
|
| 78 |
+
color: var(--text-color);
|
| 79 |
+
padding: 15px;
|
| 80 |
+
border-radius: 15px;
|
| 81 |
+
max-width: 80%;
|
| 82 |
+
align-self: flex-start;
|
| 83 |
+
margin-right: auto;
|
| 84 |
+
border: 1px solid rgba(3, 169, 244, 0.3);
|
| 85 |
+
margin-top: 10px;
|
| 86 |
+
margin-bottom: 40px;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
.bubble-header {
|
| 90 |
+
font-size: 14px;
|
| 91 |
+
font-weight: bold;
|
| 92 |
+
margin-bottom: 5px;
|
| 93 |
+
display: flex;
|
| 94 |
+
align-items: center;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
.bubble-header span {
|
| 98 |
+
margin-left: 5px;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
.icon-col {
|
| 102 |
+
display: flex;
|
| 103 |
+
flex-direction: column;
|
| 104 |
+
gap: 5px;
|
| 105 |
+
align-items: center;
|
| 106 |
+
}
|
| 107 |
+
</style>
|
| 108 |
+
""", unsafe_allow_html=True)
|
| 109 |
+
|
| 110 |
+
# --- Sidebar: Conversations ---
|
| 111 |
+
st.sidebar.title("Conversations")
|
| 112 |
+
titles = list(st.session_state.chat_sessions.keys())
|
| 113 |
+
|
| 114 |
+
if titles:
|
| 115 |
+
for topic in titles:
|
| 116 |
+
col1, col2 = st.sidebar.columns([0.8, 0.2])
|
| 117 |
+
if col1.button(f"π¨ {topic}", key=f"select_{topic}"):
|
| 118 |
+
st.session_state.current_conversation = topic
|
| 119 |
+
st.rerun()
|
| 120 |
+
if col2.button("π", key=f"delete_{topic}"):
|
| 121 |
+
del st.session_state.chat_sessions[topic]
|
| 122 |
+
if st.session_state.current_conversation == topic:
|
| 123 |
+
st.session_state.current_conversation = None
|
| 124 |
+
st.rerun()
|
| 125 |
+
else:
|
| 126 |
+
st.sidebar.write("No conversations yet. Start one below!")
|
| 127 |
+
|
| 128 |
+
# --- New Conversation ---
|
| 129 |
+
new_topic = st.sidebar.text_input("New Conversation Name")
|
| 130 |
+
if st.sidebar.button("Start New Conversation"):
|
| 131 |
+
if new_topic.strip() and new_topic not in st.session_state.chat_sessions:
|
| 132 |
+
st.session_state.chat_sessions[new_topic] = []
|
| 133 |
+
st.session_state.current_conversation = new_topic
|
| 134 |
+
st.sidebar.success(f"Started new conversation: {new_topic}")
|
| 135 |
+
st.rerun()
|
| 136 |
+
elif not new_topic.strip():
|
| 137 |
+
st.sidebar.warning("Please enter a name.")
|
| 138 |
+
else:
|
| 139 |
+
st.sidebar.warning("Conversation already exists!")
|
| 140 |
+
|
| 141 |
+
# --- Main Chat Area ---
|
| 142 |
+
if st.session_state.current_conversation:
|
| 143 |
+
convo = st.session_state.chat_sessions[st.session_state.current_conversation]
|
| 144 |
+
|
| 145 |
+
st.markdown('<div class="chat-wrapper">', unsafe_allow_html=True)
|
| 146 |
+
|
| 147 |
+
for idx, msg in enumerate(convo):
|
| 148 |
+
with st.container():
|
| 149 |
+
if msg["role"] == "user":
|
| 150 |
+
if st.session_state.edit_mode.get(idx, False):
|
| 151 |
+
new_text = st.text_input("Edit your message:", value=msg["text"], key=f"edit_input_{idx}")
|
| 152 |
+
col1, col2 = st.columns([1, 1])
|
| 153 |
+
with col1:
|
| 154 |
+
if st.button("β
Save", key=f"save_{idx}"):
|
| 155 |
+
msg["text"] = new_text
|
| 156 |
+
try:
|
| 157 |
+
new_response = get_wikipedia_summary(new_text)
|
| 158 |
+
except:
|
| 159 |
+
new_response = "Failed to retrieve summary."
|
| 160 |
+
if idx + 1 < len(convo) and convo[idx + 1]["role"] == "assistant":
|
| 161 |
+
convo[idx + 1]["text"] = new_response
|
| 162 |
+
st.session_state.edit_mode[idx] = False
|
| 163 |
+
st.rerun()
|
| 164 |
+
with col2:
|
| 165 |
+
if st.button("β Cancel", key=f"cancel_{idx}"):
|
| 166 |
+
st.session_state.edit_mode[idx] = False
|
| 167 |
+
st.rerun()
|
| 168 |
+
else:
|
| 169 |
+
col1, col2 = st.columns([0.1, 0.9])
|
| 170 |
+
with col1:
|
| 171 |
+
if st.button("βοΈ", key=f"edit_btn_{idx}"):
|
| 172 |
+
st.session_state.edit_mode[idx] = True
|
| 173 |
+
st.rerun()
|
| 174 |
+
with col2:
|
| 175 |
+
st.markdown(f'''
|
| 176 |
+
<div class="user-bubble">
|
| 177 |
+
<div class="bubble-header">π€ <span>User</span></div>
|
| 178 |
+
{msg["text"]}
|
| 179 |
+
</div>
|
| 180 |
+
''', unsafe_allow_html=True)
|
| 181 |
+
|
| 182 |
+
elif msg["role"] == "assistant":
|
| 183 |
+
st.markdown(f'''
|
| 184 |
+
<div class="assistant-bubble">
|
| 185 |
+
<div class="bubble-header">π <span>Wikipedia</span></div>
|
| 186 |
+
{msg["text"]}
|
| 187 |
+
</div>
|
| 188 |
+
''', unsafe_allow_html=True)
|
| 189 |
+
|
| 190 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 191 |
+
|
| 192 |
+
# --- Export PDF ---
|
| 193 |
+
if st.button("π₯ Export Conversation as PDF"):
|
| 194 |
+
pdf_bytes = generate_pdf(convo,st.session_state.current_conversation)
|
| 195 |
+
if pdf_bytes:
|
| 196 |
+
st.download_button("Download PDF", pdf_bytes, file_name="AskWikipedia_Conversation.pdf", mime="application/pdf")
|
| 197 |
+
else:
|
| 198 |
+
st.error("β Failed to generate PDF.")
|
| 199 |
|
| 200 |
+
# --- User Prompt ---
|
| 201 |
+
user_input = st.chat_input("Ask Wikipedia...")
|
| 202 |
+
if user_input:
|
| 203 |
+
convo.append({"role": "user", "text": user_input})
|
| 204 |
+
try:
|
| 205 |
+
reply = get_wikipedia_summary(user_input)
|
| 206 |
+
except:
|
| 207 |
+
reply = "Could not fetch response."
|
| 208 |
+
convo.append({"role": "assistant", "text": reply})
|
| 209 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|