omniverse1 commited on
Commit
c03a87b
Β·
verified Β·
1 Parent(s): 91613d5

Deploy from anycoder - streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +189 -0
streamlit_app.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from datetime import datetime
4
+ import tempfile
5
+ from pathlib import Path
6
+
7
+ # Import our modules
8
+ from utils import initialize_rag_system, clear_session_state, format_sources
9
+ from config import MODEL_NAME, EMBEDDING_MODEL
10
+
11
+ # Page config
12
+ st.set_page_config(
13
+ page_title="AI RAG Assistant",
14
+ page_icon="πŸ€–",
15
+ layout="wide",
16
+ initial_sidebar_state="expanded"
17
+ )
18
+
19
+ # Custom CSS
20
+ st.markdown("""
21
+ <style>
22
+ .main-header {font-size: 2.5rem; font-weight: 700; color: #1f77b4;}
23
+ .chat-message {padding: 1rem; border-radius: 1rem; margin: 1rem 0;}
24
+ .user-message {background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;}
25
+ .assistant-message {background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);}
26
+ .source-container {background: #f8f9fa; border-left: 4px solid #007bff; padding: 1rem;}
27
+ </style>
28
+ """, unsafe_allow_html=True)
29
+
30
+ # Header
31
+ st.markdown('<h1 class="main-header">πŸ€– AI RAG Assistant</h1>', unsafe_allow_html=True)
32
+ st.markdown("Upload your documents and chat with your data using advanced RAG powered by Llama-4-Scout")
33
+
34
+ # Footer with attribution
35
+ st.markdown(
36
+ "**Built with** [![anycoder](https://img.shields.io/badge/Built%20with-anycoder-3b82f6?style=for-the-badge&logo=huggingface)](https://huggingface.co/spaces/akhaliq/anycoder)"
37
+ )
38
+
39
+ # Sidebar
40
+ with st.sidebar:
41
+ st.header("βš™οΈ Settings")
42
+
43
+ # Model selection
44
+ model_name = st.selectbox(
45
+ "Response Model",
46
+ [MODEL_NAME],
47
+ help="Llama-4-Scout for powerful reasoning"
48
+ )
49
+
50
+ embedding_model = st.selectbox(
51
+ "Embedding Model",
52
+ [EMBEDDING_MODEL],
53
+ help="bge-m3: State-of-the-art multilingual embeddings"
54
+ )
55
+
56
+ # Similarity threshold
57
+ similarity_threshold = st.slider(
58
+ "Similarity Threshold", 0.5, 0.95, 0.8,
59
+ help="Minimum similarity score for relevant chunks"
60
+ )
61
+
62
+ # Max new tokens
63
+ max_new_tokens = st.slider("Max Tokens", 200, 2000, 1000)
64
+
65
+ st.divider()
66
+ if st.button("πŸ—‘οΈ Clear Chat & Memory", type="secondary"):
67
+ clear_session_state()
68
+ st.rerun()
69
+
70
+ # Initialize session state
71
+ if "messages" not in st.session_state:
72
+ st.session_state.messages = []
73
+ if "rag_system" not in st.session_state:
74
+ st.session_state.rag_system = None
75
+ if "documents_processed" not in st.session_state:
76
+ st.session_state.documents_processed = 0
77
+
78
+ # File upload section
79
+ uploaded_files = st.file_uploader(
80
+ "πŸ“ Upload Documents",
81
+ type=['pdf', 'txt', 'md', 'docx', 'doc', 'pptx', 'ppt'],
82
+ accept_multiple_files=True,
83
+ help="Supports PDF, TXT, MD, DOCX, PPTX and more"
84
+ )
85
+
86
+ # Process uploaded files
87
+ if uploaded_files:
88
+ with st.spinner("Processing documents... This may take a moment."):
89
+ try:
90
+ temp_dir = tempfile.mkdtemp()
91
+ for file in uploaded_files:
92
+ file_path = Path(temp_dir) / file.name
93
+ with open(file_path, "wb") as f:
94
+ f.write(file.getbuffer())
95
+
96
+ # Initialize or update RAG system
97
+ st.session_state.rag_system = initialize_rag_system(
98
+ temp_dir,
99
+ model_name,
100
+ embedding_model,
101
+ similarity_threshold
102
+ )
103
+ st.session_state.documents_processed = len(uploaded_files)
104
+
105
+ st.success(f"βœ… Processed {len(uploaded_files)} documents successfully!")
106
+ st.info(f"πŸ“Š {st.session_state.documents_processed} documents indexed and ready for querying")
107
+
108
+ except Exception as e:
109
+ st.error(f"❌ Error processing documents: {str(e)}")
110
+
111
+ # Status indicator
112
+ if st.session_state.rag_system is not None:
113
+ col1, col2 = st.columns([3, 1])
114
+ with col1:
115
+ st.success(f"βœ… Ready! {st.session_state.documents_processed} documents loaded")
116
+ with col2:
117
+ st.caption(f"Model: {model_name}")
118
+
119
+ # Chat interface
120
+ st.markdown("---")
121
+
122
+ # Display chat messages
123
+ for message in st.session_state.messages:
124
+ with st.chat_message(message["role"]):
125
+ st.markdown(message["content"])
126
+
127
+ # Display sources for assistant messages
128
+ if message["role"] == "assistant" and "sources" in message:
129
+ with st.expander("πŸ“š Sources", expanded=False):
130
+ st.markdown(format_sources(message["sources"]))
131
+
132
+ # Chat input
133
+ if prompt := st.chat_input("Ask a question about your documents..."):
134
+ # Add user message
135
+ st.session_state.messages.append({"role": "user", "content": prompt})
136
+ with st.chat_message("user"):
137
+ st.markdown(prompt)
138
+
139
+ # Generate response
140
+ if st.session_state.rag_system is not None:
141
+ with st.chat_message("assistant"):
142
+ with st.spinner("Thinking..."):
143
+ try:
144
+ # Query RAG system
145
+ response = st.session_state.rag_system.query(prompt)
146
+
147
+ # Display response
148
+ st.markdown(response.response)
149
+
150
+ # Store full response with sources
151
+ full_message = {
152
+ "role": "assistant",
153
+ "content": response.response,
154
+ "sources": response.source_nodes
155
+ }
156
+ st.session_state.messages.append(full_message)
157
+
158
+ except Exception as e:
159
+ st.error(f"Error generating response: {str(e)}")
160
+ else:
161
+ with st.chat_message("assistant"):
162
+ st.warning("πŸ‘† Please upload and process documents first!")
163
+
164
+ # Instructions
165
+ with st.expander("ℹ️ How to use", expanded=False):
166
+ st.markdown("""
167
+ 1. **Upload documents** (PDF, TXT, MD, DOCX, PPTX supported)
168
+ 2. **Wait for processing** (indexing happens automatically)
169
+ 3. **Ask questions** about your documents
170
+ 4. **Click sources** to see exact references
171
+
172
+ **Features:**
173
+ - Multi-document support
174
+ - Advanced semantic search
175
+ - Source citations
176
+ - Adjustable similarity threshold
177
+ - Streaming responses
178
+ """)
179
+
180
+ # Performance metrics
181
+ if st.session_state.rag_system is not None:
182
+ with st.expander("πŸ“ˆ System Info", expanded=False):
183
+ col1, col2, col3 = st.columns(3)
184
+ with col1:
185
+ st.metric("Documents", st.session_state.documents_processed)
186
+ with col2:
187
+ st.metric("Model", MODEL_NAME.split('/')[-1])
188
+ with col3:
189
+ st.metric("Embedding", EMBEDDING_MODEL.split('/')[-1])