File size: 4,907 Bytes
4abd716 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import streamlit as st
from langchain_community.document_loaders import WebBaseLoader
from openai import OpenAI
from sentence_transformers import SentenceTransformer
# Initialize session state for OpenAI summary
if 'openai_summary' not in st.session_state:
st.session_state.openai_summary = None
if 'show_summary' not in st.session_state:
st.session_state.show_summary = False
def toggle_summary():
st.session_state.show_summary = not st.session_state.show_summary
# Set page configuration
st.set_page_config(
page_title="π¦ LangChain Document Explorer",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for better styling
st.markdown("""
<style>
.main {
padding: 2rem;
}
.stButton>button {
width: 100%;
margin-top: 1rem;
}
.css-1d391kg {
padding: 1rem;
}
</style>
""", unsafe_allow_html=True)
# Main title with emoji
st.title("π¦ Webscrapping and Summarizing using OpenAI")
st.markdown("""
Explore web content with AI-powered analysis and processing.
Upload a URL to get started!
""")
# Sidebar configuration
with st.sidebar:
st.header("βοΈ Configuration")
openai_api_key = st.text_input("OpenAI API Key:", type="password")
st.markdown("---")
st.markdown("""
### π Quick Guide
1. Enter your OpenAI API key
2. Input a webpage URL
3. Explore different analyses in the tabs
""")
st.markdown("---")
st.markdown("Made with β€οΈ using LangChain 0.3 & Streamlit 1.41.0")
# Main content area
url = st.text_input("π Enter webpage URL:", "https://python.langchain.com/docs/")
# Document loading
docs = None
if url:
try:
with st.spinner("Loading webpage..."):
loader = WebBaseLoader(web_paths=[url])
docs = loader.load()
st.success("β
Webpage loaded successfully!")
except Exception as e:
st.error(f"β Error loading webpage: {str(e)}")
# Process and display content in tabs
if docs:
tabs = st.tabs(["π Original Content", "π€ AI Analysis", "π Embeddings"])
# Original Content Tab
with tabs[0]:
full_text = " ".join([doc.page_content for doc in docs])
st.markdown("### Original Web Content")
st.markdown(full_text)
# AI Analysis Tab
with tabs[1]:
if openai_api_key:
st.markdown("### AI Content Analysis")
if st.button("Generate AI Summary", key="generate_summary"):
try:
with st.spinner("Generating AI summary..."):
client = OpenAI(api_key=openai_api_key)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "Create a detailed writeup with key points and insights from the following text. Be grounded in the given text"},
{"role": "user", "content": full_text}
],
max_tokens=500
)
st.session_state.openai_summary = response.choices[0].message.content
except Exception as e:
st.error(f"β Error generating summary: {str(e)}")
# Display OpenAI summary if available
if st.session_state.openai_summary:
st.markdown("#### π AI-Generated Summary")
st.markdown(st.session_state.openai_summary)
else:
st.warning("β οΈ Please enter your OpenAI API key in the sidebar to use AI analysis.")
# Embeddings Tab
with tabs[2]:
st.markdown("### Document Embeddings")
try:
with st.spinner("Generating embeddings..."):
model = SentenceTransformer('all-MiniLM-L6-v2')
embeddings = model.encode(full_text)
st.markdown(f"**Embeddings Shape**: {embeddings.shape}")
st.markdown("#### Embedding Vector Preview")
st.write(embeddings[:10]) # Show first 10 dimensions
# Visualize embedding statistics
import numpy as np
st.markdown("#### Embedding Statistics")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Mean", f"{np.mean(embeddings):.4f}")
with col2:
st.metric("Std Dev", f"{np.std(embeddings):.4f}")
with col3:
st.metric("Dimensions", embeddings.shape[0])
except Exception as e:
st.error(f"β Error generating embeddings: {str(e)}")
else:
st.info("π Please enter a URL above to get started!")
|