MahatirTusher commited on
Commit
da146d9
Β·
verified Β·
1 Parent(s): 294e045

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -21
app.py CHANGED
@@ -15,24 +15,92 @@ load_dotenv()
15
  # Hardcoded Groq API key (NOT RECOMMENDED for production)
16
  GROQ_API_KEY = "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"
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # Set Streamlit app title
19
- st.title("News Research Tool πŸ“ˆ")
20
- st.sidebar.title("News Article URLs")
21
 
22
  # Initialize session state for FAISS index
23
  if "index_created" not in st.session_state:
24
  st.session_state.index_created = False
25
 
26
- # Get URLs from user input
27
- urls = []
28
- for i in range(3):
29
- url = st.sidebar.text_input(f"URL {i+1}")
30
- if url:
31
- urls.append(url)
32
-
33
- # Button to process URLs
34
- process_url_clicked = st.sidebar.button("Process URLs")
35
- faiss_index_path = "faiss_index"
36
 
37
  # Placeholder for main content
38
  main_placeholder = st.empty()
@@ -51,16 +119,20 @@ def load_faiss_index(path, embeddings):
51
 
52
  if process_url_clicked:
53
  if not urls:
54
- main_placeholder.error("Please provide at least one valid URL.")
55
  else:
56
  try:
57
  main_placeholder.text("Data Loading...Started...βœ…βœ…βœ…")
58
- loader = WebBaseLoader(urls)
 
 
 
 
59
  data = loader.load()
60
 
61
  # Check loaded data
62
  if not data or all(len(doc.page_content.strip()) == 0 for doc in data):
63
- main_placeholder.error("No content loaded from URLs. Try different URLs (e.g., https://www.bbc.com/news/science-environment-67299122).")
64
  st.stop()
65
 
66
  main_placeholder.text("Text Splitter...Started...βœ…βœ…βœ…")
@@ -72,7 +144,7 @@ if process_url_clicked:
72
 
73
  # Check document count
74
  if not docs:
75
- main_placeholder.error("No document chunks created. Try different URLs.")
76
  st.stop()
77
  main_placeholder.text(f"Split into {len(docs)} document chunks.")
78
 
@@ -86,12 +158,17 @@ if process_url_clicked:
86
  time.sleep(2)
87
  main_placeholder.empty()
88
  except Exception as e:
89
- main_placeholder.error(f"Error processing URLs: {str(e)}")
 
 
 
 
 
 
90
 
91
- query = main_placeholder.text_input("Question: ")
92
- if query:
93
  if not st.session_state.index_created or not os.path.exists(faiss_index_path):
94
- main_placeholder.error("No FAISS index found. Please process URLs first.")
95
  else:
96
  with st.spinner("Processing your question..."):
97
  try:
@@ -101,7 +178,7 @@ if query:
101
  result = chain({"question": query}, return_only_outputs=True)
102
 
103
  if not result.get("answer"):
104
- main_placeholder.warning("No answer generated. Try a different question or URLs.")
105
  st.stop()
106
 
107
  st.header("Answer")
 
15
  # Hardcoded Groq API key (NOT RECOMMENDED for production)
16
  GROQ_API_KEY = "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"
17
 
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;
31
+ backdrop-filter: blur(10px);
32
+ -webkit-backdrop-filter: blur(10px);
33
+ border: 1px solid rgba(255, 255, 255, 0.2);
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;
41
+ border: 1px solid rgba(255, 255, 255, 0.3);
42
+ border-radius: 10px;
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;
50
+ border: none;
51
+ border-radius: 10px;
52
+ padding: 10px 20px;
53
+ font-weight: bold;
54
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
55
+ transition: transform 0.2s;
56
+ }
57
+ .stButton > button:hover {
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
+ }
88
+ </style>
89
+ """, unsafe_allow_html=True)
90
+
91
  # Set Streamlit app title
92
+ st.title("WebChatter πŸ’¬")
 
93
 
94
  # Initialize session state for FAISS index
95
  if "index_created" not in st.session_state:
96
  st.session_state.index_created = False
97
 
98
+ # Sidebar for URL input
99
+ with st.sidebar:
100
+ st.header("Enter Web URL")
101
+ url = st.text_input("URL", placeholder="e.g., https://www.bbc.com/news/science-environment-67299122")
102
+ urls = [url] if url else []
103
+ process_url_clicked = st.button("Process URL")
 
 
 
 
104
 
105
  # Placeholder for main content
106
  main_placeholder = st.empty()
 
119
 
120
  if process_url_clicked:
121
  if not urls:
122
+ main_placeholder.error("Please provide a valid URL.")
123
  else:
124
  try:
125
  main_placeholder.text("Data Loading...Started...βœ…βœ…βœ…")
126
+ loader = WebBaseLoader(
127
+ web_paths=urls,
128
+ bs_kwargs={"parse_only": ["title", "p", "h1", "h2", "h3"]},
129
+ requests_kwargs={"headers": {"User-Agent": "Mozilla/5.0"}}
130
+ )
131
  data = loader.load()
132
 
133
  # Check loaded data
134
  if not data or all(len(doc.page_content.strip()) == 0 for doc in data):
135
+ main_placeholder.error("No content loaded from URL. Try a different URL (e.g., https://www.bbc.com/news/science-environment-67299122).")
136
  st.stop()
137
 
138
  main_placeholder.text("Text Splitter...Started...βœ…βœ…βœ…")
 
144
 
145
  # Check document count
146
  if not docs:
147
+ main_placeholder.error("No document chunks created. Try a different URL.")
148
  st.stop()
149
  main_placeholder.text(f"Split into {len(docs)} document chunks.")
150
 
 
158
  time.sleep(2)
159
  main_placeholder.empty()
160
  except Exception as e:
161
+ main_placeholder.error(f"Error processing URL: {str(e)}")
162
+
163
+ # Query input with Ask button
164
+ with main_placeholder.container():
165
+ st.header("Ask a Question")
166
+ query = st.text_input("Question", placeholder="e.g., What is the article about?")
167
+ ask_clicked = st.button("Ask")
168
 
169
+ if ask_clicked and query:
 
170
  if not st.session_state.index_created or not os.path.exists(faiss_index_path):
171
+ main_placeholder.error("No FAISS index found. Please process a URL first.")
172
  else:
173
  with st.spinner("Processing your question..."):
174
  try:
 
178
  result = chain({"question": query}, return_only_outputs=True)
179
 
180
  if not result.get("answer"):
181
+ main_placeholder.warning("No answer generated. Try a different question or URL.")
182
  st.stop()
183
 
184
  st.header("Answer")