Spaces:
Sleeping
Sleeping
Commit
Β·
3b5a9d4
1
Parent(s):
c1ac2e5
modified ui
Browse files- __pycache__/main_api.cpython-312.pyc +0 -0
- interface.py +133 -181
__pycache__/main_api.cpython-312.pyc
ADDED
|
Binary file (17.8 kB). View file
|
|
|
interface.py
CHANGED
|
@@ -52,8 +52,8 @@ def check_api_health():
|
|
| 52 |
except:
|
| 53 |
return False
|
| 54 |
|
| 55 |
-
def
|
| 56 |
-
"""
|
| 57 |
if not url or not prompt:
|
| 58 |
return "β Error: Please provide both URL and prompt", "", ""
|
| 59 |
|
|
@@ -62,28 +62,34 @@ def process_web_rag(url, prompt, data_source, progress=gr.Progress()):
|
|
| 62 |
return "β Error: FastAPI service is not available. Please wait a moment and try again.", "", ""
|
| 63 |
|
| 64 |
try:
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
sample_links = links[:5]
|
| 71 |
-
|
| 72 |
-
progress(0.4, desc="π Extracting text from multiple pages...")
|
| 73 |
-
file_path = extract_text(sample_links)
|
| 74 |
-
|
| 75 |
-
status_msg = f"β
Processed {len(sample_links)} pages from {len(links)} total links found"
|
| 76 |
-
else:
|
| 77 |
-
progress(0.3, desc="π Extracting text from homepage...")
|
| 78 |
-
file_path = extract_text([url])
|
| 79 |
-
status_msg = "β
Processed homepage content"
|
| 80 |
|
| 81 |
-
|
| 82 |
-
result = perform_rag(file_path, prompt)
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
response_text = f"**Query:** {result['user_query']}\n\n**Response:** {result['assistant_response']}"
|
| 88 |
sources_text = result['sources']
|
| 89 |
|
|
@@ -92,175 +98,121 @@ def process_web_rag(url, prompt, data_source, progress=gr.Progress()):
|
|
| 92 |
except Exception as e:
|
| 93 |
return f"β Error: {str(e)}", "", ""
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
.
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
.description-text {
|
| 113 |
-
text-align: center;
|
| 114 |
-
color: #666;
|
| 115 |
-
font-size: 1.1em;
|
| 116 |
-
margin-bottom: 2em;
|
| 117 |
-
}
|
| 118 |
-
|
| 119 |
-
.input-group {
|
| 120 |
-
background: #f8f9fa;
|
| 121 |
-
padding: 1.5em;
|
| 122 |
-
border-radius: 12px;
|
| 123 |
-
margin: 1em 0;
|
| 124 |
-
border: 1px solid #e9ecef;
|
| 125 |
-
}
|
| 126 |
-
|
| 127 |
-
.output-group {
|
| 128 |
-
background: #ffffff;
|
| 129 |
-
border-radius: 12px;
|
| 130 |
-
border: 1px solid #dee2e6;
|
| 131 |
-
margin: 1em 0;
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
.status-box {
|
| 135 |
-
padding: 1em;
|
| 136 |
-
border-radius: 8px;
|
| 137 |
-
margin: 0.5em 0;
|
| 138 |
-
}
|
| 139 |
-
|
| 140 |
-
.status-success {
|
| 141 |
-
background-color: #d4edda;
|
| 142 |
-
border-color: #c3e6cb;
|
| 143 |
-
color: #155724;
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
.status-error {
|
| 147 |
-
background-color: #f8d7da;
|
| 148 |
-
border-color: #f5c6cb;
|
| 149 |
-
color: #721c24;
|
| 150 |
-
}
|
| 151 |
-
"""
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
""")
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
with gr.Row():
|
| 164 |
-
with gr.Column(scale=1):
|
| 165 |
-
|
| 166 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
placeholder="What is this website about?",
|
| 177 |
-
lines=3,
|
| 178 |
-
info="Ask any question about the content"
|
| 179 |
-
)
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
label="π Data Source",
|
| 185 |
-
info="Choose how much content to analyze"
|
| 186 |
)
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
with gr.Column(scale=2):
|
| 205 |
-
response_output = gr.Textbox(
|
| 206 |
-
label="π€ AI Response",
|
| 207 |
-
lines=8,
|
| 208 |
-
interactive=False,
|
| 209 |
-
show_label=True
|
| 210 |
-
)
|
| 211 |
-
|
| 212 |
-
with gr.Column(scale=1):
|
| 213 |
-
sources_output = gr.Textbox(
|
| 214 |
-
label="π Sources",
|
| 215 |
-
lines=8,
|
| 216 |
-
interactive=False,
|
| 217 |
-
show_label=True
|
| 218 |
-
)
|
| 219 |
-
|
| 220 |
-
# Example section
|
| 221 |
-
gr.HTML("""
|
| 222 |
-
<div style="margin-top: 2em; padding: 1.5em; background: #f8f9fa; border-radius: 12px; border-left: 4px solid #667eea;">
|
| 223 |
-
<h3 style="margin-top: 0; color: #333;">π‘ Example Usage</h3>
|
| 224 |
-
<p><strong>URL:</strong> https://openai.com</p>
|
| 225 |
-
<p><strong>Question:</strong> What are the main products and services offered?</p>
|
| 226 |
-
<p><strong>Data Source:</strong> Multiple links (first 5)</p>
|
| 227 |
-
</div>
|
| 228 |
-
""")
|
| 229 |
-
|
| 230 |
-
# Add a note about the system status
|
| 231 |
-
gr.HTML("""
|
| 232 |
-
<div style="margin-top: 1em; padding: 1em; background: #e3f2fd; border-radius: 8px; border-left: 4px solid #2196f3;">
|
| 233 |
-
<p style="margin: 0; color: #0d47a1;">
|
| 234 |
-
βΉοΈ <strong>Note:</strong> If you encounter connection errors, please wait a moment for the system to initialize and try again.
|
| 235 |
-
</p>
|
| 236 |
-
</div>
|
| 237 |
-
""")
|
| 238 |
-
|
| 239 |
-
# Connect the function
|
| 240 |
-
process_btn.click(
|
| 241 |
-
fn=process_web_rag,
|
| 242 |
-
inputs=[url_input, prompt_input, data_source],
|
| 243 |
-
outputs=[status_output, response_output, sources_output],
|
| 244 |
-
show_progress=True
|
| 245 |
-
)
|
| 246 |
-
|
| 247 |
-
# Add keyboard shortcut
|
| 248 |
-
url_input.submit(
|
| 249 |
-
fn=process_web_rag,
|
| 250 |
-
inputs=[url_input, prompt_input, data_source],
|
| 251 |
-
outputs=[status_output, response_output, sources_output],
|
| 252 |
-
show_progress=True
|
| 253 |
-
)
|
| 254 |
-
|
| 255 |
-
prompt_input.submit(
|
| 256 |
-
fn=process_web_rag,
|
| 257 |
-
inputs=[url_input, prompt_input, data_source],
|
| 258 |
-
outputs=[status_output, response_output, sources_output],
|
| 259 |
-
show_progress=True
|
| 260 |
-
)
|
| 261 |
-
|
| 262 |
if __name__ == "__main__":
|
| 263 |
-
|
| 264 |
server_name="0.0.0.0",
|
| 265 |
server_port=7860,
|
| 266 |
share=False,
|
|
|
|
| 52 |
except:
|
| 53 |
return False
|
| 54 |
|
| 55 |
+
def process_multiple_links(url, prompt):
|
| 56 |
+
"""Process multiple links from a webpage"""
|
| 57 |
if not url or not prompt:
|
| 58 |
return "β Error: Please provide both URL and prompt", "", ""
|
| 59 |
|
|
|
|
| 62 |
return "β Error: FastAPI service is not available. Please wait a moment and try again.", "", ""
|
| 63 |
|
| 64 |
try:
|
| 65 |
+
links = extract_links(url)
|
| 66 |
+
sample_links = links[:5]
|
| 67 |
+
file_path = extract_text(sample_links)
|
| 68 |
+
result = perform_rag(file_path, prompt)
|
| 69 |
|
| 70 |
+
status_msg = f"β
Processed {len(sample_links)} pages from {len(links)} total links found"
|
| 71 |
+
response_text = f"**Query:** {result['user_query']}\n\n**Response:** {result['assistant_response']}"
|
| 72 |
+
sources_text = result['sources']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
return status_msg, response_text, sources_text
|
|
|
|
| 75 |
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"β Error: {str(e)}", "", ""
|
| 78 |
+
|
| 79 |
+
def process_homepage_only(url, prompt):
|
| 80 |
+
"""Process homepage content only"""
|
| 81 |
+
if not url or not prompt:
|
| 82 |
+
return "β Error: Please provide both URL and prompt", "", ""
|
| 83 |
+
|
| 84 |
+
# Check API health first
|
| 85 |
+
if not check_api_health():
|
| 86 |
+
return "β Error: FastAPI service is not available. Please wait a moment and try again.", "", ""
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
file_path = extract_text([url])
|
| 90 |
+
result = perform_rag(file_path, prompt)
|
| 91 |
|
| 92 |
+
status_msg = "β
Processed homepage content"
|
| 93 |
response_text = f"**Query:** {result['user_query']}\n\n**Response:** {result['assistant_response']}"
|
| 94 |
sources_text = result['sources']
|
| 95 |
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
return f"β Error: {str(e)}", "", ""
|
| 100 |
|
| 101 |
+
def extract_links_only(url):
|
| 102 |
+
"""Extract and display links from a webpage"""
|
| 103 |
+
if not url:
|
| 104 |
+
return "β Error: Please provide a URL"
|
| 105 |
+
|
| 106 |
+
# Check API health first
|
| 107 |
+
if not check_api_health():
|
| 108 |
+
return "β Error: FastAPI service is not available. Please wait a moment and try again."
|
| 109 |
+
|
| 110 |
+
try:
|
| 111 |
+
links = extract_links(url)
|
| 112 |
+
links_text = "\n".join([f"β’ {link}" for link in links])
|
| 113 |
+
return f"β
Found {len(links)} links:\n\n{links_text}"
|
| 114 |
+
|
| 115 |
+
except Exception as e:
|
| 116 |
+
return f"β Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
def extract_text_only(url):
|
| 119 |
+
"""Extract and display text from a webpage"""
|
| 120 |
+
if not url:
|
| 121 |
+
return "β Error: Please provide a URL"
|
| 122 |
+
|
| 123 |
+
# Check API health first
|
| 124 |
+
if not check_api_health():
|
| 125 |
+
return "β Error: FastAPI service is not available. Please wait a moment and try again."
|
|
|
|
| 126 |
|
| 127 |
+
try:
|
| 128 |
+
file_path = extract_text([url])
|
| 129 |
+
return f"β
Text extracted and saved to: {file_path}"
|
| 130 |
+
|
| 131 |
+
except Exception as e:
|
| 132 |
+
return f"β Error: {str(e)}"
|
| 133 |
+
|
| 134 |
+
# Create individual interfaces for each tab
|
| 135 |
+
multiple_links_demo = gr.Interface(
|
| 136 |
+
process_multiple_links,
|
| 137 |
+
inputs=[
|
| 138 |
+
gr.Textbox(label="π Website URL", placeholder="https://example.com"),
|
| 139 |
+
gr.Textbox(label="β Your Question", placeholder="What is this website about?", lines=3)
|
| 140 |
+
],
|
| 141 |
+
outputs=[
|
| 142 |
+
gr.Textbox(label="π Status"),
|
| 143 |
+
gr.Textbox(label="π€ AI Response", lines=8),
|
| 144 |
+
gr.Textbox(label="π Sources", lines=6)
|
| 145 |
+
],
|
| 146 |
+
title=" \nπ Multiple Links Analysis",
|
| 147 |
+
allow_flagging="never"
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
homepage_only_demo = gr.Interface(
|
| 151 |
+
process_homepage_only,
|
| 152 |
+
inputs=[
|
| 153 |
+
gr.Textbox(label="π Website URL", placeholder="https://example.com"),
|
| 154 |
+
gr.Textbox(label="β Your Question", placeholder="What is this website about?", lines=3)
|
| 155 |
+
],
|
| 156 |
+
outputs=[
|
| 157 |
+
gr.Textbox(label="π Status"),
|
| 158 |
+
gr.Textbox(label="π€ AI Response", lines=8),
|
| 159 |
+
gr.Textbox(label="π Sources", lines=6)
|
| 160 |
+
],
|
| 161 |
+
title=" \nπ Homepage Only Analysis",
|
| 162 |
+
allow_flagging="never"
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# Tab names
|
| 166 |
+
tab_names = ["Multiple Links Analysis", "Homepage Only Analysis"]
|
| 167 |
+
|
| 168 |
+
# Main interface with tabs
|
| 169 |
+
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
| 170 |
with gr.Row():
|
| 171 |
+
with gr.Column(scale=1, min_width=1200):
|
| 172 |
+
gr.Markdown("## \n\n<style>h1, h2, h3, h4, h5, h6 { font-size: 40px !important; text-align:center !important; display: block !important;}</style>", label="")
|
| 173 |
+
gr.HTML("""
|
| 174 |
+
<div style="text-align: center; margin: 2em 0;">
|
| 175 |
+
<h1 style="font-size: 3em; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin-bottom: 0.5em;">
|
| 176 |
+
π Web RAG System
|
| 177 |
+
</h1>
|
| 178 |
+
<p style="font-size: 1.2em; color: #666; margin-bottom: 1em;">
|
| 179 |
+
Extract content from web pages and ask questions using AI-powered retrieval
|
| 180 |
+
</p>
|
| 181 |
+
</div>
|
| 182 |
+
""")
|
| 183 |
|
| 184 |
+
# Add custom CSS for increasing font size of text areas and toggles
|
| 185 |
+
gr.HTML("""
|
| 186 |
+
<style>
|
| 187 |
+
textarea, input[type="text"], .gr-button {
|
| 188 |
+
font-size: 1.2em !important;
|
| 189 |
+
}
|
| 190 |
+
</style>
|
| 191 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
+
tabbed_interface = gr.TabbedInterface(
|
| 194 |
+
[multiple_links_demo, homepage_only_demo],
|
| 195 |
+
tab_names
|
|
|
|
|
|
|
| 196 |
)
|
| 197 |
|
| 198 |
+
# Add example and notes at the bottom
|
| 199 |
+
gr.HTML("""
|
| 200 |
+
<div style="margin-top: 2em; padding: 1.5em; background: #f8f9fa; border-radius: 12px; border-left: 4px solid #667eea;">
|
| 201 |
+
<h3 style="margin-top: 0; color: #333;">π‘ Example Usage</h3>
|
| 202 |
+
<p><strong>URL:</strong> https://openai.com</p>
|
| 203 |
+
<p><strong>Question:</strong> What are the main products and services offered?</p>
|
| 204 |
+
</div>
|
| 205 |
+
|
| 206 |
+
<div style="margin-top: 1em; padding: 1em; background: #e3f2fd; border-radius: 8px; border-left: 4px solid #2196f3;">
|
| 207 |
+
<p style="margin: 0; color: #0d47a1;">
|
| 208 |
+
βΉοΈ <strong>Note:</strong> If you encounter connection errors, please wait a moment for the system to initialize and try again.
|
| 209 |
+
</p>
|
| 210 |
+
</div>
|
| 211 |
+
""")
|
| 212 |
+
|
| 213 |
+
# Launch the interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
if __name__ == "__main__":
|
| 215 |
+
interface.launch(
|
| 216 |
server_name="0.0.0.0",
|
| 217 |
server_port=7860,
|
| 218 |
share=False,
|