NightPassenger commited on
Commit
a412e15
Β·
verified Β·
1 Parent(s): ec3584e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import streamlit as st
4
+ from qa_loader import load_qa_and_create_vectorstore
5
+ from rag_chain import generate_response
6
+ from dotenv import load_dotenv
7
+
8
+ # πŸ”Ή Load environment variables
9
+ load_dotenv()
10
+
11
+ # πŸ”Ή Streamlit Page Configuration
12
+ st.set_page_config(page_title="Vistula University AI Assistant", layout="centered")
13
+
14
+ # πŸ”Ή Title and Description
15
+ st.title("πŸ“š University AI Assistant")
16
+ st.write("πŸš€ Ask me anything about University!")
17
+
18
+ # πŸ”Ή Retrieve Data (Cached for Performance)
19
+ @st.cache_resource
20
+ def get_retriever():
21
+ return load_qa_and_create_vectorstore()
22
+
23
+ retriever = get_retriever()
24
+
25
+ if isinstance(retriever, tuple):
26
+ retriever = retriever[0]
27
+
28
+ # πŸ”Ή Start or Load Chat History
29
+ if "chat_history" not in st.session_state:
30
+ st.session_state.chat_history = []
31
+
32
+ # πŸ”Ή Display Chat History
33
+ st.write("### πŸ—‚οΈ Chat History")
34
+
35
+ for entry in st.session_state.chat_history:
36
+ with st.chat_message("user"):
37
+ st.write(entry["question"])
38
+ with st.chat_message("assistant"):
39
+ st.write(entry["answer"])
40
+
41
+ # πŸ”Ή User Input
42
+ query = st.chat_input("Ask your question about Vistula University!")
43
+
44
+ # πŸ”Ή Process When User Submits a Question
45
+ if query:
46
+ with st.spinner("πŸ€– Thinking..."):
47
+ response = generate_response(retriever, query)
48
+
49
+ # πŸ”Ή Add to Chat History
50
+ st.session_state.chat_history.append({
51
+ "question": query,
52
+ "answer": response
53
+ })
54
+
55
+ # πŸ”Ή Display User Question and AI Response
56
+ with st.chat_message("user"):
57
+ st.write(query)
58
+ with st.chat_message("assistant"):
59
+ placeholder = st.empty()
60
+ current_text = ""
61
+
62
+ # Typing Effect
63
+ for word in response.split():
64
+ current_text += word + " "
65
+ placeholder.write(current_text)
66
+ time.sleep(0.05)