Update app.py
Browse files
app.py
CHANGED
|
@@ -45,8 +45,19 @@ faiss_index = None
|
|
| 45 |
bookmarks = []
|
| 46 |
fetch_cache = {}
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Define the categories
|
| 52 |
CATEGORIES = [
|
|
@@ -83,10 +94,34 @@ if not GROQ_API_KEY:
|
|
| 83 |
openai.api_key = GROQ_API_KEY
|
| 84 |
openai.api_base = "https://api.groq.com/openai/v1"
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def extract_main_content(soup):
|
| 91 |
"""
|
| 92 |
Extract the main content from a webpage while filtering out boilerplate content.
|
|
@@ -155,186 +190,14 @@ def get_page_metadata(soup):
|
|
| 155 |
metadata['title'] = og_title.get('content', '').strip()
|
| 156 |
|
| 157 |
return metadata
|
| 158 |
-
def generate_summary_and_assign_category(bookmark):
|
| 159 |
-
"""
|
| 160 |
-
Generate a concise summary and assign a category using a single LLM call.
|
| 161 |
-
"""
|
| 162 |
-
logger.info(f"Generating summary and assigning category for bookmark: {bookmark.get('url')}")
|
| 163 |
-
|
| 164 |
-
max_retries = 3
|
| 165 |
-
retry_count = 0
|
| 166 |
-
base_wait = 5 # Increased base wait time to 5 seconds
|
| 167 |
-
|
| 168 |
-
while retry_count < max_retries:
|
| 169 |
-
try:
|
| 170 |
-
# Rate Limiting Logic - Modified
|
| 171 |
-
with api_lock:
|
| 172 |
-
global last_api_call_time
|
| 173 |
-
current_time = time.time()
|
| 174 |
-
elapsed = current_time - last_api_call_time
|
| 175 |
-
if elapsed < base_wait:
|
| 176 |
-
sleep_duration = base_wait - elapsed
|
| 177 |
-
logger.info(f"Rate limiting: Waiting for {sleep_duration:.2f} seconds...")
|
| 178 |
-
time.sleep(sleep_duration)
|
| 179 |
-
last_api_call_time = time.time()
|
| 180 |
-
|
| 181 |
-
html_content = bookmark.get('html_content', '')
|
| 182 |
-
soup = BeautifulSoup(html_content, 'html.parser')
|
| 183 |
-
metadata = get_page_metadata(soup)
|
| 184 |
-
main_content = extract_main_content(soup)
|
| 185 |
-
|
| 186 |
-
# Prepare content for the prompt
|
| 187 |
-
content_parts = []
|
| 188 |
-
if metadata['title']:
|
| 189 |
-
content_parts.append(f"Title: {metadata['title']}")
|
| 190 |
-
if metadata['description']:
|
| 191 |
-
content_parts.append(f"Description: {metadata['description']}")
|
| 192 |
-
if metadata['keywords']:
|
| 193 |
-
content_parts.append(f"Keywords: {metadata['keywords']}")
|
| 194 |
-
if main_content:
|
| 195 |
-
content_parts.append(f"Main Content: {main_content}")
|
| 196 |
-
|
| 197 |
-
content_text = '\n'.join(content_parts)
|
| 198 |
-
|
| 199 |
-
# Detect insufficient or erroneous content
|
| 200 |
-
error_keywords = ['Access Denied', 'Security Check', 'Cloudflare', 'captcha', 'unusual traffic']
|
| 201 |
-
if not content_text or len(content_text.split()) < 50:
|
| 202 |
-
use_prior_knowledge = True
|
| 203 |
-
logger.info(f"Content for {bookmark.get('url')} is insufficient. Instructing LLM to use prior knowledge.")
|
| 204 |
-
elif any(keyword.lower() in content_text.lower() for keyword in error_keywords):
|
| 205 |
-
use_prior_knowledge = True
|
| 206 |
-
logger.info(f"Content for {bookmark.get('url')} contains error messages. Instructing LLM to use prior knowledge.")
|
| 207 |
-
else:
|
| 208 |
-
use_prior_knowledge = False
|
| 209 |
-
|
| 210 |
-
if use_prior_knowledge:
|
| 211 |
-
prompt = f"""
|
| 212 |
-
You are a knowledgeable assistant with up-to-date information as of 2023.
|
| 213 |
-
URL: {bookmark.get('url')}
|
| 214 |
-
Provide:
|
| 215 |
-
1. A concise summary (max two sentences) about this website.
|
| 216 |
-
2. Assign the most appropriate category from the list below.
|
| 217 |
-
Categories:
|
| 218 |
-
{', '.join([f'"{cat}"' for cat in CATEGORIES])}
|
| 219 |
-
Format:
|
| 220 |
-
Summary: [Your summary]
|
| 221 |
-
Category: [One category]
|
| 222 |
-
"""
|
| 223 |
-
else:
|
| 224 |
-
prompt = f"""
|
| 225 |
-
You are an assistant that creates concise webpage summaries and assigns categories.
|
| 226 |
-
Content:
|
| 227 |
-
{content_text}
|
| 228 |
-
Provide:
|
| 229 |
-
1. A concise summary (max two sentences) focusing on the main topic.
|
| 230 |
-
2. Assign the most appropriate category from the list below.
|
| 231 |
-
Categories:
|
| 232 |
-
{', '.join([f'"{cat}"' for cat in CATEGORIES])}
|
| 233 |
-
Format:
|
| 234 |
-
Summary: [Your summary]
|
| 235 |
-
Category: [One category]
|
| 236 |
-
"""
|
| 237 |
-
|
| 238 |
-
def estimate_tokens(text):
|
| 239 |
-
return len(text) / 4
|
| 240 |
-
|
| 241 |
-
prompt_tokens = estimate_tokens(prompt)
|
| 242 |
-
max_tokens = 150
|
| 243 |
-
total_tokens = prompt_tokens + max_tokens
|
| 244 |
-
|
| 245 |
-
tokens_per_minute = 40000
|
| 246 |
-
tokens_per_second = tokens_per_minute / 60
|
| 247 |
-
required_delay = total_tokens / tokens_per_second
|
| 248 |
-
sleep_time = max(required_delay, base_wait) # Use at least base_wait seconds
|
| 249 |
-
|
| 250 |
-
response = openai.ChatCompletion.create(
|
| 251 |
-
model='llama-3.1-70b-versatile',
|
| 252 |
-
messages=[
|
| 253 |
-
{"role": "user", "content": prompt}
|
| 254 |
-
],
|
| 255 |
-
max_tokens=int(max_tokens),
|
| 256 |
-
temperature=0.5,
|
| 257 |
-
)
|
| 258 |
-
|
| 259 |
-
content = response['choices'][0]['message']['content'].strip()
|
| 260 |
-
if not content:
|
| 261 |
-
raise ValueError("Empty response received from the model.")
|
| 262 |
-
|
| 263 |
-
summary_match = re.search(r"Summary:\s*(.*)", content)
|
| 264 |
-
category_match = re.search(r"Category:\s*(.*)", content)
|
| 265 |
-
|
| 266 |
-
if summary_match:
|
| 267 |
-
bookmark['summary'] = summary_match.group(1).strip()
|
| 268 |
-
else:
|
| 269 |
-
bookmark['summary'] = 'No summary available.'
|
| 270 |
-
|
| 271 |
-
if category_match:
|
| 272 |
-
category = category_match.group(1).strip().strip('"')
|
| 273 |
-
if category in CATEGORIES:
|
| 274 |
-
bookmark['category'] = category
|
| 275 |
-
else:
|
| 276 |
-
bookmark['category'] = 'Uncategorized'
|
| 277 |
-
else:
|
| 278 |
-
bookmark['category'] = 'Uncategorized'
|
| 279 |
-
|
| 280 |
-
# Simple keyword-based validation
|
| 281 |
-
summary_lower = bookmark['summary'].lower()
|
| 282 |
-
url_lower = bookmark['url'].lower()
|
| 283 |
-
if 'social media' in summary_lower or 'twitter' in summary_lower or 'x.com' in url_lower:
|
| 284 |
-
bookmark['category'] = 'Social Media'
|
| 285 |
-
elif 'wikipedia' in url_lower:
|
| 286 |
-
bookmark['category'] = 'Reference and Knowledge Bases'
|
| 287 |
-
|
| 288 |
-
logger.info("Successfully generated summary and assigned category")
|
| 289 |
-
|
| 290 |
-
# Add consistent delay after successful processing
|
| 291 |
-
time.sleep(sleep_time)
|
| 292 |
-
break
|
| 293 |
-
|
| 294 |
-
except openai.error.RateLimitError as e:
|
| 295 |
-
retry_count += 1
|
| 296 |
-
# Use exponential backoff with a maximum wait time
|
| 297 |
-
wait_time = min(base_wait * (2 ** retry_count), 30) # Cap at 30 seconds
|
| 298 |
-
logger.warning(f"Rate limit reached. Waiting for {wait_time} seconds before retrying... (Attempt {retry_count}/{max_retries})")
|
| 299 |
-
time.sleep(wait_time)
|
| 300 |
-
if retry_count == max_retries:
|
| 301 |
-
bookmark['summary'] = 'Summary generation failed due to rate limits.'
|
| 302 |
-
bookmark['category'] = 'Uncategorized'
|
| 303 |
-
break
|
| 304 |
-
except Exception as e:
|
| 305 |
-
logger.error(f"Error generating summary and assigning category: {e}", exc_info=True)
|
| 306 |
-
bookmark['summary'] = 'No summary available.'
|
| 307 |
-
bookmark['category'] = 'Uncategorized'
|
| 308 |
-
break
|
| 309 |
|
| 310 |
-
def parse_bookmarks(file_content):
|
| 311 |
-
"""
|
| 312 |
-
Parse bookmarks from HTML file.
|
| 313 |
-
"""
|
| 314 |
-
logger.info("Parsing bookmarks")
|
| 315 |
-
try:
|
| 316 |
-
soup = BeautifulSoup(file_content, 'html.parser')
|
| 317 |
-
extracted_bookmarks = []
|
| 318 |
-
for link in soup.find_all('a'):
|
| 319 |
-
url = link.get('href')
|
| 320 |
-
title = link.text.strip()
|
| 321 |
-
if url and title:
|
| 322 |
-
if url.startswith('http://') or url.startswith('https://'):
|
| 323 |
-
extracted_bookmarks.append({'url': url, 'title': title})
|
| 324 |
-
else:
|
| 325 |
-
logger.info(f"Skipping non-http/https URL: {url}")
|
| 326 |
-
logger.info(f"Extracted {len(extracted_bookmarks)} bookmarks")
|
| 327 |
-
return extracted_bookmarks
|
| 328 |
-
except Exception as e:
|
| 329 |
-
logger.error("Error parsing bookmarks: %s", e, exc_info=True)
|
| 330 |
-
raise
|
| 331 |
def fetch_url_info(bookmark):
|
| 332 |
"""
|
| 333 |
Fetch information about a URL.
|
| 334 |
"""
|
| 335 |
url = bookmark['url']
|
| 336 |
if url in fetch_cache:
|
| 337 |
-
with
|
| 338 |
bookmark.update(fetch_cache[url])
|
| 339 |
return
|
| 340 |
|
|
@@ -378,7 +241,7 @@ def fetch_url_info(bookmark):
|
|
| 378 |
bookmark['html_content'] = ''
|
| 379 |
logger.error(f"Error fetching URL info for {url}: {e}", exc_info=True)
|
| 380 |
finally:
|
| 381 |
-
with
|
| 382 |
fetch_cache[url] = {
|
| 383 |
'etag': bookmark.get('etag'),
|
| 384 |
'status_code': bookmark.get('status_code'),
|
|
@@ -388,6 +251,87 @@ def fetch_url_info(bookmark):
|
|
| 388 |
'slow_link': bookmark.get('slow_link', False),
|
| 389 |
}
|
| 390 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
def vectorize_and_index(bookmarks_list):
|
| 392 |
"""
|
| 393 |
Create vector embeddings for bookmarks and build FAISS index with ID mapping.
|
|
@@ -459,7 +403,7 @@ def display_bookmarks():
|
|
| 459 |
|
| 460 |
def process_uploaded_file(file, state_bookmarks):
|
| 461 |
"""
|
| 462 |
-
Process
|
| 463 |
"""
|
| 464 |
global bookmarks, faiss_index
|
| 465 |
logger.info("Processing uploaded file")
|
|
@@ -470,52 +414,63 @@ def process_uploaded_file(file, state_bookmarks):
|
|
| 470 |
|
| 471 |
try:
|
| 472 |
file_content = file.decode('utf-8')
|
| 473 |
-
except UnicodeDecodeError as e:
|
| 474 |
-
logger.error(f"Error decoding the file: {e}", exc_info=True)
|
| 475 |
-
return "Error decoding the file. Please ensure it's a valid HTML file.", '', state_bookmarks, display_bookmarks(), gr.update(choices=[])
|
| 476 |
-
|
| 477 |
-
try:
|
| 478 |
bookmarks = parse_bookmarks(file_content)
|
| 479 |
-
except Exception as e:
|
| 480 |
-
logger.error(f"Error parsing bookmarks: {e}", exc_info=True)
|
| 481 |
-
return "Error parsing the bookmarks HTML file.", '', state_bookmarks, display_bookmarks(), gr.update(choices=[])
|
| 482 |
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
return "No bookmarks found in the uploaded file.", '', state_bookmarks, display_bookmarks(), gr.update(choices=[])
|
| 486 |
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
executor.map(fetch_url_info, bookmarks)
|
| 495 |
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
|
|
|
|
|
|
| 500 |
|
| 501 |
-
|
| 502 |
faiss_index = vectorize_and_index(bookmarks)
|
| 503 |
-
except Exception as e:
|
| 504 |
-
logger.error(f"Error building FAISS index: {e}", exc_info=True)
|
| 505 |
-
return "Error building search index.", '', state_bookmarks, display_bookmarks(), gr.update(choices=[])
|
| 506 |
|
| 507 |
-
|
| 508 |
-
|
|
|
|
|
|
|
|
|
|
| 509 |
|
| 510 |
-
|
| 511 |
-
bookmark_html = display_bookmarks()
|
| 512 |
-
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
| 513 |
-
for i, bookmark in enumerate(bookmarks)]
|
| 514 |
|
| 515 |
-
|
| 516 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
-
return message, bookmark_html, state_bookmarks, bookmark_html, gr.update(choices=choices)
|
| 519 |
def delete_selected_bookmarks(selected_indices, state_bookmarks):
|
| 520 |
"""
|
| 521 |
Delete selected bookmarks and remove their vectors from the FAISS index.
|
|
@@ -578,7 +533,6 @@ def edit_selected_bookmarks_category(selected_indices, new_category, state_bookm
|
|
| 578 |
state_bookmarks = bookmarks.copy()
|
| 579 |
|
| 580 |
return message, gr.update(choices=choices), display_bookmarks(), state_bookmarks
|
| 581 |
-
|
| 582 |
def export_bookmarks():
|
| 583 |
"""
|
| 584 |
Export bookmarks to an HTML file.
|
|
@@ -622,81 +576,82 @@ def chatbot_response(user_query, chat_history):
|
|
| 622 |
try:
|
| 623 |
chat_history.append({"role": "user", "content": user_query})
|
| 624 |
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
base_wait = 5 # Increased base wait time to 5 seconds
|
| 628 |
-
for attempt in range(max_retries):
|
| 629 |
-
try:
|
| 630 |
with api_lock:
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 651 |
chat_history.append({"role": "assistant", "content": answer})
|
| 652 |
return chat_history
|
| 653 |
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
Bookmarks:
|
| 663 |
-
{bookmarks_info}
|
| 664 |
-
Provide a concise and helpful response.
|
| 665 |
-
"""
|
| 666 |
-
|
| 667 |
-
response = openai.ChatCompletion.create(
|
| 668 |
-
model='llama-3.1-70b-versatile',
|
| 669 |
-
messages=[
|
| 670 |
-
{"role": "user", "content": prompt}
|
| 671 |
-
],
|
| 672 |
-
max_tokens=300,
|
| 673 |
-
temperature=0.7,
|
| 674 |
-
)
|
| 675 |
-
|
| 676 |
-
answer = response['choices'][0]['message']['content'].strip()
|
| 677 |
-
logger.info("Chatbot response generated")
|
| 678 |
-
|
| 679 |
-
# Add a small delay between successful requests
|
| 680 |
-
time.sleep(base_wait)
|
| 681 |
-
|
| 682 |
-
chat_history.append({"role": "assistant", "content": answer})
|
| 683 |
-
return chat_history
|
| 684 |
-
|
| 685 |
-
except openai.error.RateLimitError as e:
|
| 686 |
-
wait_time = min(base_wait * (2 ** attempt), 30) # Cap maximum wait time at 30 seconds
|
| 687 |
-
logger.warning(f"Rate limit reached. Attempt {attempt + 1}/{max_retries}. Waiting for {wait_time} seconds...")
|
| 688 |
-
time.sleep(wait_time)
|
| 689 |
-
if attempt == max_retries - 1:
|
| 690 |
-
error_message = "⚠️ The service is currently experiencing high demand. Please try again in a few moments."
|
| 691 |
chat_history.append({"role": "assistant", "content": error_message})
|
| 692 |
return chat_history
|
| 693 |
-
continue
|
| 694 |
|
| 695 |
except Exception as e:
|
| 696 |
error_message = f"⚠️ Error processing your query: {str(e)}"
|
| 697 |
logger.error(error_message, exc_info=True)
|
| 698 |
chat_history.append({"role": "assistant", "content": error_message})
|
| 699 |
return chat_history
|
|
|
|
| 700 |
def build_app():
|
| 701 |
"""
|
| 702 |
Build and launch the Gradio app.
|
|
|
|
| 45 |
bookmarks = []
|
| 46 |
fetch_cache = {}
|
| 47 |
|
| 48 |
+
# Groq API Rate Limits
|
| 49 |
+
GROQ_RPM = 30 # requests per minute
|
| 50 |
+
GROQ_TPM = 40000 # tokens per minute
|
| 51 |
+
SECONDS_PER_MINUTE = 60
|
| 52 |
+
MIN_TIME_BETWEEN_CALLS = SECONDS_PER_MINUTE / GROQ_RPM # 2 seconds between calls
|
| 53 |
+
MAX_CONCURRENT_CALLS = 3 # Keep concurrent calls limited to prevent rate limits
|
| 54 |
+
TOKEN_BUFFER = 0.9 # Use 90% of token limit to be safe
|
| 55 |
+
|
| 56 |
+
# Rate limiting tools
|
| 57 |
+
api_lock = threading.Lock()
|
| 58 |
+
request_times = [] # Track request timestamps
|
| 59 |
+
token_usage = [] # Track token usage
|
| 60 |
+
LLM_SEMAPHORE = threading.Semaphore(MAX_CONCURRENT_CALLS)
|
| 61 |
|
| 62 |
# Define the categories
|
| 63 |
CATEGORIES = [
|
|
|
|
| 94 |
openai.api_key = GROQ_API_KEY
|
| 95 |
openai.api_base = "https://api.groq.com/openai/v1"
|
| 96 |
|
| 97 |
+
def manage_rate_limits():
|
| 98 |
+
"""
|
| 99 |
+
Manage both request and token rate limits.
|
| 100 |
+
Returns the time to wait (if any) before making next request.
|
| 101 |
+
"""
|
| 102 |
+
current_time = time.time()
|
| 103 |
+
minute_ago = current_time - SECONDS_PER_MINUTE
|
| 104 |
+
|
| 105 |
+
# Clean up old entries
|
| 106 |
+
global request_times, token_usage
|
| 107 |
+
request_times = [t for t in request_times if t > minute_ago]
|
| 108 |
+
token_usage = [t for t, _ in token_usage if t > minute_ago]
|
| 109 |
|
| 110 |
+
# Check request rate
|
| 111 |
+
if len(request_times) >= GROQ_RPM:
|
| 112 |
+
oldest_request = request_times[0]
|
| 113 |
+
return max(0, SECONDS_PER_MINUTE - (current_time - oldest_request))
|
| 114 |
+
|
| 115 |
+
# Check token rate
|
| 116 |
+
total_tokens = sum(tokens for _, tokens in token_usage)
|
| 117 |
+
if total_tokens >= GROQ_TPM * TOKEN_BUFFER:
|
| 118 |
+
return 1.0 # Wait a second if near token limit
|
| 119 |
+
|
| 120 |
+
return 0
|
| 121 |
+
|
| 122 |
+
def estimate_tokens(text):
|
| 123 |
+
"""Estimate tokens in text using GPT-3 tokenizer approximation"""
|
| 124 |
+
return len(text.split()) * 1.3 # Rough estimate: 1.3 tokens per word
|
| 125 |
def extract_main_content(soup):
|
| 126 |
"""
|
| 127 |
Extract the main content from a webpage while filtering out boilerplate content.
|
|
|
|
| 190 |
metadata['title'] = og_title.get('content', '').strip()
|
| 191 |
|
| 192 |
return metadata
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
def fetch_url_info(bookmark):
|
| 195 |
"""
|
| 196 |
Fetch information about a URL.
|
| 197 |
"""
|
| 198 |
url = bookmark['url']
|
| 199 |
if url in fetch_cache:
|
| 200 |
+
with api_lock:
|
| 201 |
bookmark.update(fetch_cache[url])
|
| 202 |
return
|
| 203 |
|
|
|
|
| 241 |
bookmark['html_content'] = ''
|
| 242 |
logger.error(f"Error fetching URL info for {url}: {e}", exc_info=True)
|
| 243 |
finally:
|
| 244 |
+
with api_lock:
|
| 245 |
fetch_cache[url] = {
|
| 246 |
'etag': bookmark.get('etag'),
|
| 247 |
'status_code': bookmark.get('status_code'),
|
|
|
|
| 251 |
'slow_link': bookmark.get('slow_link', False),
|
| 252 |
}
|
| 253 |
|
| 254 |
+
def process_bookmarks_batch(bookmarks_batch):
|
| 255 |
+
"""Process a batch of bookmarks with controlled rate limiting"""
|
| 256 |
+
for bookmark in bookmarks_batch:
|
| 257 |
+
with LLM_SEMAPHORE:
|
| 258 |
+
while True:
|
| 259 |
+
with api_lock:
|
| 260 |
+
wait_time = manage_rate_limits()
|
| 261 |
+
if wait_time <= 0:
|
| 262 |
+
break
|
| 263 |
+
logger.info(f"Rate limiting: Waiting for {wait_time:.2f} seconds...")
|
| 264 |
+
time.sleep(wait_time)
|
| 265 |
+
|
| 266 |
+
try:
|
| 267 |
+
html_content = bookmark.get('html_content', '')
|
| 268 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
| 269 |
+
metadata = get_page_metadata(soup)
|
| 270 |
+
main_content = extract_main_content(soup)
|
| 271 |
+
|
| 272 |
+
# Prepare shortened prompt to reduce tokens
|
| 273 |
+
content = f"Title: {metadata['title']}\nURL: {bookmark['url']}"
|
| 274 |
+
if len(main_content) > 1000: # Limit content length
|
| 275 |
+
main_content = main_content[:1000] + "..."
|
| 276 |
+
|
| 277 |
+
prompt = f"""Analyze this webpage:
|
| 278 |
+
{content}
|
| 279 |
+
Content: {main_content}
|
| 280 |
+
Provide in format:
|
| 281 |
+
Summary: [2 sentences max]
|
| 282 |
+
Category: [{', '.join(CATEGORIES)}]"""
|
| 283 |
+
|
| 284 |
+
# Estimate tokens
|
| 285 |
+
input_tokens = estimate_tokens(prompt)
|
| 286 |
+
max_tokens = 150
|
| 287 |
+
total_tokens = input_tokens + max_tokens
|
| 288 |
+
|
| 289 |
+
# Make API call
|
| 290 |
+
response = openai.ChatCompletion.create(
|
| 291 |
+
model='llama-3.1-70b-versatile',
|
| 292 |
+
messages=[{"role": "user", "content": prompt}],
|
| 293 |
+
max_tokens=max_tokens,
|
| 294 |
+
temperature=0.5,
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
# Track rate limits
|
| 298 |
+
with api_lock:
|
| 299 |
+
current_time = time.time()
|
| 300 |
+
request_times.append(current_time)
|
| 301 |
+
token_usage.append((current_time, total_tokens))
|
| 302 |
+
|
| 303 |
+
content = response['choices'][0]['message']['content'].strip()
|
| 304 |
+
|
| 305 |
+
# Process response
|
| 306 |
+
summary_match = re.search(r"Summary:\s*(.*?)(?:\n|$)", content)
|
| 307 |
+
category_match = re.search(r"Category:\s*(.*?)(?:\n|$)", content)
|
| 308 |
+
|
| 309 |
+
bookmark['summary'] = summary_match.group(1).strip() if summary_match else 'No summary available.'
|
| 310 |
+
|
| 311 |
+
if category_match:
|
| 312 |
+
category = category_match.group(1).strip().strip('"')
|
| 313 |
+
bookmark['category'] = category if category in CATEGORIES else 'Uncategorized'
|
| 314 |
+
else:
|
| 315 |
+
bookmark['category'] = 'Uncategorized'
|
| 316 |
+
|
| 317 |
+
# Quick category validation
|
| 318 |
+
if 'social media' in bookmark['url'].lower() or 'twitter' in bookmark['url'].lower() or 'x.com' in bookmark['url'].lower():
|
| 319 |
+
bookmark['category'] = 'Social Media'
|
| 320 |
+
elif 'wikipedia' in bookmark['url'].lower():
|
| 321 |
+
bookmark['category'] = 'Reference and Knowledge Bases'
|
| 322 |
+
|
| 323 |
+
logger.info(f"Successfully processed bookmark: {bookmark['url']}")
|
| 324 |
+
break
|
| 325 |
+
|
| 326 |
+
except openai.error.RateLimitError as e:
|
| 327 |
+
wait_time = int(e.headers.get('Retry-After', 5))
|
| 328 |
+
logger.warning(f"Rate limit hit, waiting {wait_time} seconds...")
|
| 329 |
+
time.sleep(wait_time)
|
| 330 |
+
except Exception as e:
|
| 331 |
+
logger.error(f"Error processing bookmark: {e}")
|
| 332 |
+
bookmark['summary'] = 'Processing failed.'
|
| 333 |
+
bookmark['category'] = 'Uncategorized'
|
| 334 |
+
break
|
| 335 |
def vectorize_and_index(bookmarks_list):
|
| 336 |
"""
|
| 337 |
Create vector embeddings for bookmarks and build FAISS index with ID mapping.
|
|
|
|
| 403 |
|
| 404 |
def process_uploaded_file(file, state_bookmarks):
|
| 405 |
"""
|
| 406 |
+
Process uploaded file with optimized batch processing
|
| 407 |
"""
|
| 408 |
global bookmarks, faiss_index
|
| 409 |
logger.info("Processing uploaded file")
|
|
|
|
| 414 |
|
| 415 |
try:
|
| 416 |
file_content = file.decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
bookmarks = parse_bookmarks(file_content)
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
+
if not bookmarks:
|
| 420 |
+
return "No bookmarks found in the file.", '', state_bookmarks, display_bookmarks(), gr.update(choices=[])
|
|
|
|
| 421 |
|
| 422 |
+
# Assign IDs
|
| 423 |
+
for idx, bookmark in enumerate(bookmarks):
|
| 424 |
+
bookmark['id'] = idx
|
| 425 |
|
| 426 |
+
# First fetch all URLs concurrently
|
| 427 |
+
with ThreadPoolExecutor(max_workers=10) as executor:
|
| 428 |
+
executor.map(fetch_url_info, bookmarks)
|
|
|
|
| 429 |
|
| 430 |
+
# Process bookmarks in parallel with controlled concurrency
|
| 431 |
+
batch_size = min(MAX_CONCURRENT_CALLS, len(bookmarks))
|
| 432 |
+
batches = [bookmarks[i:i + batch_size] for i in range(0, len(bookmarks), batch_size)]
|
| 433 |
+
|
| 434 |
+
with ThreadPoolExecutor(max_workers=MAX_CONCURRENT_CALLS) as executor:
|
| 435 |
+
executor.map(process_bookmarks_batch, batches)
|
| 436 |
|
| 437 |
+
# Build FAISS index
|
| 438 |
faiss_index = vectorize_and_index(bookmarks)
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
+
# Update display and state
|
| 441 |
+
bookmark_html = display_bookmarks()
|
| 442 |
+
choices = [f"{i+1}. {bookmark['title']} (Category: {bookmark['category']})"
|
| 443 |
+
for i, bookmark in enumerate(bookmarks)]
|
| 444 |
+
state_bookmarks = bookmarks.copy()
|
| 445 |
|
| 446 |
+
return "✅ Processing complete!", bookmark_html, state_bookmarks, bookmark_html, gr.update(choices=choices)
|
|
|
|
|
|
|
|
|
|
| 447 |
|
| 448 |
+
except Exception as e:
|
| 449 |
+
logger.error(f"Error processing file: {e}")
|
| 450 |
+
return f"Error processing file: {str(e)}", '', state_bookmarks, display_bookmarks(), gr.update(choices=[])
|
| 451 |
+
|
| 452 |
+
def parse_bookmarks(file_content):
|
| 453 |
+
"""
|
| 454 |
+
Parse bookmarks from HTML file.
|
| 455 |
+
"""
|
| 456 |
+
logger.info("Parsing bookmarks")
|
| 457 |
+
try:
|
| 458 |
+
soup = BeautifulSoup(file_content, 'html.parser')
|
| 459 |
+
extracted_bookmarks = []
|
| 460 |
+
for link in soup.find_all('a'):
|
| 461 |
+
url = link.get('href')
|
| 462 |
+
title = link.text.strip()
|
| 463 |
+
if url and title:
|
| 464 |
+
if url.startswith('http://') or url.startswith('https://'):
|
| 465 |
+
extracted_bookmarks.append({'url': url, 'title': title})
|
| 466 |
+
else:
|
| 467 |
+
logger.info(f"Skipping non-http/https URL: {url}")
|
| 468 |
+
logger.info(f"Extracted {len(extracted_bookmarks)} bookmarks")
|
| 469 |
+
return extracted_bookmarks
|
| 470 |
+
except Exception as e:
|
| 471 |
+
logger.error("Error parsing bookmarks: %s", e, exc_info=True)
|
| 472 |
+
raise
|
| 473 |
|
|
|
|
| 474 |
def delete_selected_bookmarks(selected_indices, state_bookmarks):
|
| 475 |
"""
|
| 476 |
Delete selected bookmarks and remove their vectors from the FAISS index.
|
|
|
|
| 533 |
state_bookmarks = bookmarks.copy()
|
| 534 |
|
| 535 |
return message, gr.update(choices=choices), display_bookmarks(), state_bookmarks
|
|
|
|
| 536 |
def export_bookmarks():
|
| 537 |
"""
|
| 538 |
Export bookmarks to an HTML file.
|
|
|
|
| 576 |
try:
|
| 577 |
chat_history.append({"role": "user", "content": user_query})
|
| 578 |
|
| 579 |
+
with LLM_SEMAPHORE:
|
| 580 |
+
while True:
|
|
|
|
|
|
|
|
|
|
| 581 |
with api_lock:
|
| 582 |
+
wait_time = manage_rate_limits()
|
| 583 |
+
if wait_time <= 0:
|
| 584 |
+
break
|
| 585 |
+
logger.info(f"Rate limiting: Waiting for {wait_time:.2f} seconds...")
|
| 586 |
+
time.sleep(wait_time)
|
| 587 |
+
|
| 588 |
+
try:
|
| 589 |
+
# Search for relevant bookmarks
|
| 590 |
+
query_vector = embedding_model.encode([user_query]).astype('float32')
|
| 591 |
+
k = 5
|
| 592 |
+
distances, ids = faiss_index.search(query_vector, k)
|
| 593 |
+
ids = ids.flatten()
|
| 594 |
+
|
| 595 |
+
id_to_bookmark = {bookmark['id']: bookmark for bookmark in bookmarks}
|
| 596 |
+
matching_bookmarks = [id_to_bookmark.get(id) for id in ids if id in id_to_bookmark]
|
| 597 |
+
|
| 598 |
+
if not matching_bookmarks:
|
| 599 |
+
answer = "No relevant bookmarks found for your query."
|
| 600 |
+
chat_history.append({"role": "assistant", "content": answer})
|
| 601 |
+
return chat_history
|
| 602 |
+
|
| 603 |
+
# Prepare concise prompt
|
| 604 |
+
bookmarks_info = "\n".join([
|
| 605 |
+
f"Title: {bookmark['title']}\nURL: {bookmark['url']}\nSummary: {bookmark['summary']}"
|
| 606 |
+
for bookmark in matching_bookmarks
|
| 607 |
+
])
|
| 608 |
+
|
| 609 |
+
prompt = f"""User Query: "{user_query}"
|
| 610 |
+
Found Bookmarks:
|
| 611 |
+
{bookmarks_info}
|
| 612 |
+
Provide a helpful, concise response."""
|
| 613 |
+
|
| 614 |
+
# Estimate tokens and make API call
|
| 615 |
+
input_tokens = estimate_tokens(prompt)
|
| 616 |
+
max_tokens = 300
|
| 617 |
+
total_tokens = input_tokens + max_tokens
|
| 618 |
+
|
| 619 |
+
response = openai.ChatCompletion.create(
|
| 620 |
+
model='llama-3.1-70b-versatile',
|
| 621 |
+
messages=[{"role": "user", "content": prompt}],
|
| 622 |
+
max_tokens=max_tokens,
|
| 623 |
+
temperature=0.7,
|
| 624 |
+
)
|
| 625 |
+
|
| 626 |
+
# Track rate limits
|
| 627 |
+
with api_lock:
|
| 628 |
+
current_time = time.time()
|
| 629 |
+
request_times.append(current_time)
|
| 630 |
+
token_usage.append((current_time, total_tokens))
|
| 631 |
+
|
| 632 |
+
answer = response['choices'][0]['message']['content'].strip()
|
| 633 |
+
logger.info("Chatbot response generated")
|
| 634 |
+
|
| 635 |
chat_history.append({"role": "assistant", "content": answer})
|
| 636 |
return chat_history
|
| 637 |
|
| 638 |
+
except openai.error.RateLimitError as e:
|
| 639 |
+
wait_time = int(e.headers.get('Retry-After', 5))
|
| 640 |
+
logger.warning(f"Rate limit hit, waiting {wait_time} seconds...")
|
| 641 |
+
time.sleep(wait_time)
|
| 642 |
+
continue
|
| 643 |
+
except Exception as e:
|
| 644 |
+
error_message = f"⚠️ Error processing your query: {str(e)}"
|
| 645 |
+
logger.error(error_message, exc_info=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 646 |
chat_history.append({"role": "assistant", "content": error_message})
|
| 647 |
return chat_history
|
|
|
|
| 648 |
|
| 649 |
except Exception as e:
|
| 650 |
error_message = f"⚠️ Error processing your query: {str(e)}"
|
| 651 |
logger.error(error_message, exc_info=True)
|
| 652 |
chat_history.append({"role": "assistant", "content": error_message})
|
| 653 |
return chat_history
|
| 654 |
+
|
| 655 |
def build_app():
|
| 656 |
"""
|
| 657 |
Build and launch the Gradio app.
|