MahatirTusher commited on
Commit
c0af503
Β·
verified Β·
1 Parent(s): fa5d820

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -60
app.py CHANGED
@@ -18,13 +18,11 @@ GROQ_API_KEY = "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"
18
  # Custom CSS for glassy, colorful, and shadowy UI
19
  st.markdown("""
20
  <style>
21
- /* Global styles */
22
  body {
23
  background: linear-gradient(135deg, #1e3c72, #2a5298);
24
  color: #ffffff;
25
  font-family: 'Arial', sans-serif;
26
  }
27
- /* Glassy container for sidebar and main content */
28
  .stSidebar, .main .block-container {
29
  background: rgba(255, 255, 255, 0.1);
30
  border-radius: 15px;
@@ -34,7 +32,6 @@ st.markdown("""
34
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
35
  padding: 20px;
36
  }
37
- /* Input fields */
38
  .stTextInput > div > input {
39
  background: rgba(255, 255, 255, 0.15);
40
  color: #ffffff;
@@ -43,7 +40,6 @@ st.markdown("""
43
  padding: 10px;
44
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
45
  }
46
- /* Buttons */
47
  .stButton > button {
48
  background: linear-gradient(45deg, #6b48ff, #00ddeb);
49
  color: #ffffff;
@@ -58,30 +54,25 @@ st.markdown("""
58
  transform: translateY(-2px);
59
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3);
60
  }
61
- /* Headers */
62
  h1, h2, h3 {
63
  color: #ffffff;
64
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
65
  }
66
- /* Status messages */
67
  .stText {
68
  color: #e0e0e0;
69
  font-weight: bold;
70
  }
71
- /* Error messages */
72
  .stAlert {
73
  background: rgba(255, 50, 50, 0.2);
74
  border: 1px solid rgba(255, 50, 50, 0.5);
75
  border-radius: 10px;
76
  color: #ffcccc;
77
  }
78
- /* Warning messages */
79
  .stAlert[role="alert"] > div {
80
  background: rgba(255, 200, 0, 0.2);
81
  border: 1px solid rgba(255, 200, 0, 0.5);
82
  color: #fff5cc;
83
  }
84
- /* Spinner */
85
  .stSpinner > div {
86
  color: #00ddeb;
87
  }
@@ -91,11 +82,9 @@ st.markdown("""
91
  # Set Streamlit app title
92
  st.title("WebChatter πŸ’¬")
93
 
94
- # Initialize session state for FAISS index and processing status
95
  if "index_created" not in st.session_state:
96
  st.session_state.index_created = False
97
- if "processing_status" not in st.session_state:
98
- st.session_state.processing_status = ""
99
 
100
  # Sidebar for URL input
101
  with st.sidebar:
@@ -124,54 +113,43 @@ if process_url_clicked:
124
  if not url.strip():
125
  st.error("Please provide a valid URL.")
126
  else:
127
- try:
128
- st.session_state.processing_status = "Data Loading...Started...βœ…βœ…βœ…"
129
- st.text(st.session_state.processing_status)
130
- loader = WebBaseLoader(
131
- web_path=url.strip(),
132
- bs_kwargs={"parse_only": ["title", "p", "h1", "h2", "h3"]},
133
- requests_kwargs={"headers": {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}}
134
- )
135
- data = loader.load()
136
-
137
- # Check loaded data
138
- if not data or all(len(doc.page_content.strip()) == 0 for doc in data):
139
- st.error("No content loaded from URL. Try a different URL (e.g., https://www.bbc.com/news/science-environment-67299122).")
140
- st.session_state.processing_status = ""
141
- st.stop()
142
-
143
- st.session_state.processing_status = "Text Splitter...Started...βœ…βœ…βœ…"
144
- st.text(st.session_state.processing_status)
145
- text_splitter = RecursiveCharacterTextSplitter(
146
- separators=['\n\n', '\n', '.', ','],
147
- chunk_size=1000
148
- )
149
- docs = text_splitter.split_documents(data)
150
-
151
- # Check document count
152
- if not docs:
153
- st.error("No document chunks created. Try a different URL.")
154
- st.session_state.processing_status = ""
155
- st.stop()
156
- st.session_state.processing_status = f"Split into {len(docs)} document chunks."
157
- st.text(st.session_state.processing_status)
158
-
159
- st.session_state.processing_status = "Embedding Vector Started Building...βœ…βœ…βœ…"
160
- st.text(st.session_state.processing_status)
161
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
162
- vectorstore_openai = FAISS.from_documents(docs, embeddings)
163
-
164
- faiss_index_path = "faiss_index"
165
- save_faiss_index(vectorstore_openai, faiss_index_path)
166
- st.session_state.index_created = True
167
- st.session_state.processing_status = "FAISS index saved successfully! βœ…βœ…βœ…"
168
- st.text(st.session_state.processing_status)
169
- time.sleep(2)
170
- st.session_state.processing_status = ""
171
- st.experimental_rerun() # Refresh to clear status messages
172
- except Exception as e:
173
- st.error(f"Error processing URL: {str(e)}")
174
- st.session_state.processing_status = ""
175
 
176
  # Query input with Ask button
177
  with main_container:
 
18
  # Custom CSS for glassy, colorful, and shadowy UI
19
  st.markdown("""
20
  <style>
 
21
  body {
22
  background: linear-gradient(135deg, #1e3c72, #2a5298);
23
  color: #ffffff;
24
  font-family: 'Arial', sans-serif;
25
  }
 
26
  .stSidebar, .main .block-container {
27
  background: rgba(255, 255, 255, 0.1);
28
  border-radius: 15px;
 
32
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
33
  padding: 20px;
34
  }
 
35
  .stTextInput > div > input {
36
  background: rgba(255, 255, 255, 0.15);
37
  color: #ffffff;
 
40
  padding: 10px;
41
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
42
  }
 
43
  .stButton > button {
44
  background: linear-gradient(45deg, #6b48ff, #00ddeb);
45
  color: #ffffff;
 
54
  transform: translateY(-2px);
55
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3);
56
  }
 
57
  h1, h2, h3 {
58
  color: #ffffff;
59
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
60
  }
 
61
  .stText {
62
  color: #e0e0e0;
63
  font-weight: bold;
64
  }
 
65
  .stAlert {
66
  background: rgba(255, 50, 50, 0.2);
67
  border: 1px solid rgba(255, 50, 50, 0.5);
68
  border-radius: 10px;
69
  color: #ffcccc;
70
  }
 
71
  .stAlert[role="alert"] > div {
72
  background: rgba(255, 200, 0, 0.2);
73
  border: 1px solid rgba(255, 200, 0, 0.5);
74
  color: #fff5cc;
75
  }
 
76
  .stSpinner > div {
77
  color: #00ddeb;
78
  }
 
82
  # Set Streamlit app title
83
  st.title("WebChatter πŸ’¬")
84
 
85
+ # Initialize session state
86
  if "index_created" not in st.session_state:
87
  st.session_state.index_created = False
 
 
88
 
89
  # Sidebar for URL input
90
  with st.sidebar:
 
113
  if not url.strip():
114
  st.error("Please provide a valid URL.")
115
  else:
116
+ with st.spinner("Processing URL..."):
117
+ try:
118
+ st.text("Data Loading...Started...βœ…βœ…βœ…")
119
+ loader = WebBaseLoader(
120
+ web_path=url.strip(),
121
+ bs_kwargs={"parse_only": ["title", "p", "h1", "h2", "h3"]},
122
+ requests_kwargs={"headers": {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}}
123
+ )
124
+ data = loader.load()
125
+
126
+ if not data or all(len(doc.page_content.strip()) == 0 for doc in data):
127
+ st.error("No content loaded from URL. Try a different URL (e.g., https://www.bbc.com/news/science-environment-67299122).")
128
+ st.stop()
129
+
130
+ st.text("Text Splitter...Started...βœ…βœ…βœ…")
131
+ text_splitter = RecursiveCharacterTextSplitter(
132
+ separators=['\n\n', '\n', '.', ','],
133
+ chunk_size=1000
134
+ )
135
+ docs = text_splitter.split_documents(data)
136
+
137
+ if not docs:
138
+ st.error("No document chunks created. Try a different URL.")
139
+ st.stop()
140
+ st.text(f"Split into {len(docs)} document chunks.")
141
+
142
+ st.text("Embedding Vector Started Building...βœ…βœ…βœ…")
143
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
144
+ vectorstore_openai = FAISS.from_documents(docs, embeddings)
145
+
146
+ faiss_index_path = "faiss_index"
147
+ save_faiss_index(vectorstore_openai, faiss_index_path)
148
+ st.session_state.index_created = True
149
+ st.text("FAISS index saved successfully! βœ…βœ…βœ…")
150
+ time.sleep(2)
151
+ except Exception as e:
152
+ st.error(f"Error processing URL: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
153
 
154
  # Query input with Ask button
155
  with main_container: