Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from rag_app import WebRAG
|
3 |
+
import time
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Set page configuration
|
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 |
+
.stApp {
|
17 |
+
max-width: 1200px;
|
18 |
+
margin: 0 auto;
|
19 |
+
}
|
20 |
+
.chat-container {
|
21 |
+
border-radius: 10px;
|
22 |
+
padding: 20px;
|
23 |
+
background-color: #f0f2f6;
|
24 |
+
margin: 10px 0;
|
25 |
+
}
|
26 |
+
.user-message {
|
27 |
+
background-color: #2e7bf3;
|
28 |
+
color: white;
|
29 |
+
padding: 15px;
|
30 |
+
border-radius: 15px;
|
31 |
+
margin: 5px 0;
|
32 |
+
}
|
33 |
+
.assistant-message {
|
34 |
+
background-color: #white;
|
35 |
+
padding: 15px;
|
36 |
+
border-radius: 15px;
|
37 |
+
margin: 5px 0;
|
38 |
+
border: 1px solid #e0e0e0;
|
39 |
+
}
|
40 |
+
</style>
|
41 |
+
""", unsafe_allow_html=True)
|
42 |
+
|
43 |
+
# Initialize session state
|
44 |
+
if 'rag' not in st.session_state:
|
45 |
+
st.session_state.rag = WebRAG()
|
46 |
+
if 'chat_history' not in st.session_state:
|
47 |
+
st.session_state.chat_history = []
|
48 |
+
if 'url_processed' not in st.session_state:
|
49 |
+
st.session_state.url_processed = False
|
50 |
+
if 'current_url' not in st.session_state:
|
51 |
+
st.session_state.current_url = ""
|
52 |
+
|
53 |
+
# Function to reset chat history
|
54 |
+
def reset_chat_history():
|
55 |
+
st.session_state.chat_history = []
|
56 |
+
st.session_state.current_url = ""
|
57 |
+
|
58 |
+
# Header
|
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", type="primary"):
|
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... This may take a moment."):
|
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() # Rerun the app to refresh the chat interface
|
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 |
+
1. Enter a webpage URL in the input field
|
100 |
+
2. Click 'Process URL' to analyze the content
|
101 |
+
3. Ask questions about the webpage content
|
102 |
+
4. Get AI-powered answers based on the content
|
103 |
+
""")
|
104 |
+
|
105 |
+
# Main chat interface
|
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 input
|
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:
|
141 |
+
st.error(f"Error: {str(e)}")
|
142 |
+
else:
|
143 |
+
st.info("π Please process a URL first using the sidebar")
|