purajith commited on
Commit
d0114ab
·
verified ·
1 Parent(s): 6ab4526

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py CHANGED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import shutil
4
+ from hybrid_search import multimodelrag
5
+ import warnings
6
+
7
+ warnings.filterwarnings("ignore")
8
+
9
+ # Streamlit UI
10
+ st.set_page_config(layout="wide")
11
+ st.title("AI Document Processor with Conversational RAG")
12
+
13
+ # Initialize conversation history in session state
14
+ if "conversation_history" not in st.session_state:
15
+ st.session_state.conversation_history = []
16
+
17
+ # Sidebar for file upload and settings
18
+ with st.sidebar:
19
+ uploaded_files = st.file_uploader(
20
+ "Upload multiple files (PDF, DOCX, Excel, CSV)",
21
+ type=["pdf", "docx", "xlsx", "csv"],
22
+ accept_multiple_files=True
23
+ )
24
+
25
+ embeding = st.selectbox("Select Memory Mode", ["open_source", "openai"], index=0)
26
+ conversation = st.selectbox("Number of conversation", [2, 4, 6], index=0)
27
+ llm_option = st.selectbox("Select LLM Model", ["GPT-4o", "GPT-4o-mini"], index=1)
28
+
29
+ temp_dir = "temp_uploaded_files"
30
+
31
+ # Clear the previous uploads when new files are uploaded
32
+ if uploaded_files:
33
+ if os.path.exists(temp_dir):
34
+ shutil.rmtree(temp_dir) # Delete the old directory and its contents
35
+ os.makedirs(temp_dir) # Create a fresh directory
36
+
37
+ file_paths = [] # List to store saved file paths
38
+ for file in uploaded_files:
39
+ file_path = os.path.join(temp_dir, file.name)
40
+ with open(file_path, "wb") as f:
41
+ f.write(file.read()) # Save file locally
42
+ file_paths.append(file_path)
43
+ st.write(f"✅ Saved: {file.name}")
44
+
45
+ # Chat interface
46
+ st.write("### Chat Interface")
47
+
48
+ chat_display = "\n".join(st.session_state.conversation_history)
49
+ # st.text_area("Conversation History", chat_display, height=300, disabled=True)
50
+
51
+ # Input for user question
52
+ user_input = st.text_input("Ask a question:")
53
+ llm_model = llm_option
54
+
55
+ if st.button("Retrieve and Answer"):
56
+ if user_input or uploaded_files:
57
+ answer = multimodelrag(user_input, file_paths, embeding, llm_model,conversation)
58
+
59
+ # Update conversation history
60
+ st.session_state.conversation_history.append(f"User: {user_input}")
61
+ st.session_state.conversation_history.append(f"AI: {answer}")
62
+
63
+ # Refresh chat display
64
+ chat_display = "\n".join(st.session_state.conversation_history)
65
+ st.text_area("Conversation History", chat_display, height=400, disabled=True)
66
+
67
+ st.write("### Answer:")
68
+ st.write(answer)
69
+
70
+