siddhartharya's picture
Update app.py
3276277 verified
raw
history blame
2.2 kB
# 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
# Initialize models and variables
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
faiss_index = None # Renamed from 'index' to 'faiss_index'
bookmarks = []
fetch_cache = {}
# Helper functions remain the same...
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 process_uploaded_file(file):
global bookmarks, faiss_index
if file is None:
return "Please upload a bookmarks HTML file."
# Since 'file' is now bytes, decode it directly
file_content = file.decode('utf-8')
bookmarks = parse_bookmarks(file_content)
for bookmark in bookmarks:
fetch_url_info(bookmark)
generate_summary(bookmark)
faiss_index, embeddings = vectorize_and_index(bookmarks)
return f"Successfully processed {len(bookmarks)} bookmarks."
def chatbot_response(user_query):
# [Same as before]
def display_bookmarks():
# [Same as before]
def edit_bookmark(bookmark_idx, new_title, new_url):
# [Same as before]
def delete_bookmark(bookmark_idx):
# [Same as before]
def build_app():
with gr.Blocks() as demo:
gr.Markdown("# Bookmark Manager App")
with gr.Tab("Upload and Process Bookmarks"):
upload = gr.File(label="Upload Bookmarks HTML File", type='bytes') # Updated here
process_button = gr.Button("Process Bookmarks")
output_text = gr.Textbox(label="Output")
process_button.click(
process_uploaded_file,
inputs=upload,
outputs=output_text
)
with gr.Tab("Chat with Bookmarks"):
# [Same as before]
with gr.Tab("Manage Bookmarks"):
# [Same as before]
demo.launch()
if __name__ == "__main__":
build_app()