Spaces:
Running
Running
# app.py | |
import gradio as gr | |
from bs4 import BeautifulSoup | |
import requests | |
from transformers import pipeline | |
from sentence_transformers import SentenceTransformer | |
import faiss | |
import numpy as np | |
import pandas as pd | |
# Initialize models and variables | |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") | |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2') | |
faiss_index = None | |
bookmarks = [] | |
fetch_cache = {} | |
def parse_bookmarks(file_content): | |
# [Same as before] | |
def fetch_url_info(bookmark): | |
# [Same as before] | |
def generate_summary(bookmark): | |
# [Same as before] | |
def vectorize_and_index(bookmarks): | |
# [Same as before] | |
def display_bookmarks(): | |
data = [] | |
for i, bookmark in enumerate(bookmarks): | |
status = "Dead Link" if bookmark.get('dead_link') else "Active" | |
css_class = "dead-link" if bookmark.get('dead_link') else "" | |
data.append({ | |
'Index': i, | |
'Title': bookmark['title'], | |
'URL': bookmark['url'], | |
'Status': status, | |
'ETag': bookmark.get('etag', 'N/A'), | |
'Summary': bookmark.get('summary', ''), | |
'css_class': css_class | |
}) | |
df = pd.DataFrame(data) | |
return df | |
def process_uploaded_file(file): | |
# [Updated as per Step 3] | |
def chatbot_response(user_query): | |
# [Same as before] | |
def edit_bookmark(bookmark_idx, new_title, new_url): | |
# [Update outputs to include the updated bookmarks list] | |
message, updated_df = "Bookmark updated successfully.", display_bookmarks() | |
return message, updated_df | |
def delete_bookmark(bookmark_idx): | |
# [Update outputs to include the updated bookmarks list] | |
message, updated_df = "Bookmark deleted successfully.", display_bookmarks() | |
return message, updated_df | |
def build_app(): | |
with gr.Blocks(css="app.css") as demo: | |
gr.Markdown("# Bookmark Manager App") | |
with gr.Tab("Upload and Process Bookmarks"): | |
upload = gr.File(label="Upload Bookmarks HTML File", type='binary') | |
process_button = gr.Button("Process Bookmarks") | |
output_text = gr.Textbox(label="Output") | |
bookmark_table = gr.Dataframe( | |
label="Bookmarks", | |
headers=["Index", "Title", "URL", "Status", "ETag", "Summary"], | |
datatype=["number", "str", "str", "str", "str", "str"], | |
interactive=False | |
) | |
process_button.click( | |
process_uploaded_file, | |
inputs=upload, | |
outputs=[output_text, bookmark_table] | |
) | |
with gr.Tab("Chat with Bookmarks"): | |
# [Same as before] | |
with gr.Tab("Manage Bookmarks"): | |
manage_output = gr.Textbox(label="Manage Output") | |
bookmark_table_manage = gr.Dataframe( | |
label="Bookmarks", | |
headers=["Index", "Title", "URL", "Status", "ETag", "Summary"], | |
datatype=["number", "str", "str", "str", "str", "str"], | |
interactive=False | |
) | |
refresh_button = gr.Button("Refresh Bookmark List") | |
with gr.Row(): | |
index_input = gr.Number(label="Bookmark Index") | |
new_title_input = gr.Textbox(label="New Title") | |
new_url_input = gr.Textbox(label="New URL") | |
edit_button = gr.Button("Edit Bookmark") | |
delete_button = gr.Button("Delete Bookmark") | |
refresh_button.click( | |
display_bookmarks, | |
inputs=None, | |
outputs=bookmark_table_manage | |
) | |
edit_button.click( | |
edit_bookmark, | |
inputs=[index_input, new_title_input, new_url_input], | |
outputs=[manage_output, bookmark_table_manage] | |
) | |
delete_button.click( | |
delete_bookmark, | |
inputs=index_input, | |
outputs=[manage_output, bookmark_table_manage] | |
) | |
demo.launch() | |
if __name__ == "__main__": | |
build_app() | |