Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -3,42 +3,60 @@ from rag_app import WebRAG
|
|
3 |
import time
|
4 |
import os
|
5 |
|
6 |
-
#
|
7 |
st.set_page_config(
|
8 |
page_title="Web RAG Assistant",
|
9 |
page_icon="π",
|
10 |
layout="wide"
|
11 |
)
|
12 |
|
13 |
-
# Custom CSS
|
14 |
-
st.markdown(
|
|
|
15 |
<style>
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
</style>
|
41 |
-
""",
|
|
|
|
|
42 |
|
43 |
# Initialize session state
|
44 |
if 'rag' not in st.session_state:
|
@@ -55,86 +73,72 @@ def reset_chat_history():
|
|
55 |
st.session_state.chat_history = []
|
56 |
st.session_state.current_url = ""
|
57 |
|
58 |
-
#
|
59 |
-
st.title("π Web RAG Assistant")
|
60 |
-
st.markdown("### Ask questions about any webpage")
|
61 |
-
|
62 |
-
# Sidebar
|
63 |
with st.sidebar:
|
64 |
-
st.header("Settings")
|
65 |
url = st.text_input("Enter webpage URL:")
|
66 |
|
67 |
-
# Add scraping method selection
|
68 |
scraping_method = st.selectbox(
|
69 |
"Select Scraping Method",
|
70 |
["beautifulsoup", "scrapegraph", "crawl4ai"],
|
71 |
help="""
|
72 |
-
BeautifulSoup: Basic HTML parsing, faster but less sophisticated
|
73 |
-
ScrapeGraph: AI-powered scraping, better at understanding content but slower
|
74 |
-
Crawl4ai: Advanced async crawler with good JavaScript support
|
75 |
"""
|
76 |
)
|
77 |
|
78 |
-
if st.button("Process URL"
|
79 |
if url:
|
80 |
-
# Check if URL has changed
|
81 |
if url != st.session_state.current_url:
|
82 |
reset_chat_history()
|
83 |
st.session_state.current_url = url
|
84 |
|
85 |
-
with st.spinner("Processing URL...
|
86 |
try:
|
87 |
st.session_state.rag.crawl_and_process(url, scraping_method)
|
88 |
st.session_state.url_processed = True
|
89 |
-
st.success("URL processed successfully!")
|
90 |
-
st.rerun()
|
91 |
except Exception as e:
|
92 |
-
st.error(f"Error processing URL: {str(e)}")
|
93 |
else:
|
94 |
-
st.warning("Please enter a URL")
|
95 |
|
96 |
st.divider()
|
97 |
-
st.markdown("### How to use")
|
98 |
-
st.markdown(
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
104 |
|
105 |
-
# Main
|
|
|
|
|
106 |
st.divider()
|
107 |
|
108 |
# Display chat messages
|
109 |
for message in st.session_state.chat_history:
|
110 |
if message["role"] == "user":
|
111 |
-
st.markdown(f"""
|
112 |
-
<div class="user-message">
|
113 |
-
{message["content"]}
|
114 |
-
</div>
|
115 |
-
""", unsafe_allow_html=True)
|
116 |
else:
|
117 |
-
st.markdown(f"""
|
118 |
-
<div class="assistant-message">
|
119 |
-
{message["content"]}
|
120 |
-
</div>
|
121 |
-
""", unsafe_allow_html=True)
|
122 |
|
123 |
-
# Chat
|
124 |
if st.session_state.url_processed:
|
125 |
-
question = st.chat_input("Ask a question about the webpage...")
|
126 |
if question:
|
127 |
-
# Add user message to chat history
|
128 |
st.session_state.chat_history.append({"role": "user", "content": question})
|
129 |
-
|
130 |
-
# Get answer from RAG
|
131 |
with st.spinner("Thinking..."):
|
132 |
try:
|
133 |
answer = st.session_state.rag.ask_question(
|
134 |
question,
|
135 |
[(msg["content"], msg["content"]) for msg in st.session_state.chat_history if msg["role"] == "assistant"]
|
136 |
)
|
137 |
-
# Add assistant message to chat history
|
138 |
st.session_state.chat_history.append({"role": "assistant", "content": answer})
|
139 |
st.rerun()
|
140 |
except Exception as e:
|
|
|
3 |
import time
|
4 |
import os
|
5 |
|
6 |
+
# Page Configuration
|
7 |
st.set_page_config(
|
8 |
page_title="Web RAG Assistant",
|
9 |
page_icon="π",
|
10 |
layout="wide"
|
11 |
)
|
12 |
|
13 |
+
# Custom CSS for better aesthetics
|
14 |
+
st.markdown(
|
15 |
+
"""
|
16 |
<style>
|
17 |
+
.stApp {
|
18 |
+
max-width: 1200px;
|
19 |
+
margin: 0 auto;
|
20 |
+
font-family: 'Arial', sans-serif;
|
21 |
+
}
|
22 |
+
.sidebar .sidebar-content {
|
23 |
+
background-color: #1E1E1E;
|
24 |
+
color: white;
|
25 |
+
padding: 20px;
|
26 |
+
}
|
27 |
+
.css-1d391kg {
|
28 |
+
padding: 0;
|
29 |
+
}
|
30 |
+
.stTextInput, .stSelectbox, .stButton, .stChatInput {
|
31 |
+
border-radius: 10px;
|
32 |
+
padding: 10px;
|
33 |
+
}
|
34 |
+
.chat-container {
|
35 |
+
border-radius: 10px;
|
36 |
+
padding: 20px;
|
37 |
+
background-color: #f0f2f6;
|
38 |
+
margin: 10px 0;
|
39 |
+
}
|
40 |
+
.user-message {
|
41 |
+
background-color: #2e7bf3;
|
42 |
+
color: white;
|
43 |
+
padding: 15px;
|
44 |
+
border-radius: 15px;
|
45 |
+
margin: 5px 0;
|
46 |
+
text-align: right;
|
47 |
+
}
|
48 |
+
.assistant-message {
|
49 |
+
background-color: white;
|
50 |
+
padding: 15px;
|
51 |
+
border-radius: 15px;
|
52 |
+
margin: 5px 0;
|
53 |
+
border: 1px solid #e0e0e0;
|
54 |
+
text-align: left;
|
55 |
+
}
|
56 |
</style>
|
57 |
+
""",
|
58 |
+
unsafe_allow_html=True
|
59 |
+
)
|
60 |
|
61 |
# Initialize session state
|
62 |
if 'rag' not in st.session_state:
|
|
|
73 |
st.session_state.chat_history = []
|
74 |
st.session_state.current_url = ""
|
75 |
|
76 |
+
# Sidebar on the extreme left
|
|
|
|
|
|
|
|
|
77 |
with st.sidebar:
|
78 |
+
st.header("βοΈ Settings")
|
79 |
url = st.text_input("Enter webpage URL:")
|
80 |
|
|
|
81 |
scraping_method = st.selectbox(
|
82 |
"Select Scraping Method",
|
83 |
["beautifulsoup", "scrapegraph", "crawl4ai"],
|
84 |
help="""
|
85 |
+
- BeautifulSoup: Basic HTML parsing, faster but less sophisticated
|
86 |
+
- ScrapeGraph: AI-powered scraping, better at understanding content but slower
|
87 |
+
- Crawl4ai: Advanced async crawler with good JavaScript support
|
88 |
"""
|
89 |
)
|
90 |
|
91 |
+
if st.button("π Process URL"):
|
92 |
if url:
|
|
|
93 |
if url != st.session_state.current_url:
|
94 |
reset_chat_history()
|
95 |
st.session_state.current_url = url
|
96 |
|
97 |
+
with st.spinner("Processing URL... Please wait."):
|
98 |
try:
|
99 |
st.session_state.rag.crawl_and_process(url, scraping_method)
|
100 |
st.session_state.url_processed = True
|
101 |
+
st.success("β
URL processed successfully!")
|
102 |
+
st.rerun()
|
103 |
except Exception as e:
|
104 |
+
st.error(f"β Error processing URL: {str(e)}")
|
105 |
else:
|
106 |
+
st.warning("β οΈ Please enter a URL")
|
107 |
|
108 |
st.divider()
|
109 |
+
st.markdown("### π How to use")
|
110 |
+
st.markdown(
|
111 |
+
"""
|
112 |
+
1. Enter a webpage URL
|
113 |
+
2. Click 'Process URL' to analyze the content
|
114 |
+
3. Ask questions about the webpage
|
115 |
+
4. Receive answers
|
116 |
+
"""
|
117 |
+
)
|
118 |
|
119 |
+
# Main Content
|
120 |
+
st.title("π Web RAG Assistant")
|
121 |
+
st.markdown("### Ask questions about any webpage")
|
122 |
st.divider()
|
123 |
|
124 |
# Display chat messages
|
125 |
for message in st.session_state.chat_history:
|
126 |
if message["role"] == "user":
|
127 |
+
st.markdown(f"""<div class='user-message'>{message["content"]}</div>""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
128 |
else:
|
129 |
+
st.markdown(f"""<div class='assistant-message'>{message["content"]}</div>""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
130 |
|
131 |
+
# Chat Input
|
132 |
if st.session_state.url_processed:
|
133 |
+
question = st.chat_input("π¬ Ask a question about the webpage...")
|
134 |
if question:
|
|
|
135 |
st.session_state.chat_history.append({"role": "user", "content": question})
|
|
|
|
|
136 |
with st.spinner("Thinking..."):
|
137 |
try:
|
138 |
answer = st.session_state.rag.ask_question(
|
139 |
question,
|
140 |
[(msg["content"], msg["content"]) for msg in st.session_state.chat_history if msg["role"] == "assistant"]
|
141 |
)
|
|
|
142 |
st.session_state.chat_history.append({"role": "assistant", "content": answer})
|
143 |
st.rerun()
|
144 |
except Exception as e:
|