To upload files, please first save the app
# app.py
import streamlit as st
import sqlite3
from PyPDF2 import PdfReader
# Configuration spéciale pour Pyodide
# (À placer dans config.toml)
"""
[server]
enableCORS = false
enableXsrfProtection = false
fileWatcherType = "none"
enableWebsocketCompression = false
"""
# Base de données en mémoire
DB_NAME = ":memory:"
def init_db():
conn = sqlite3.connect(DB_NAME)
conn.execute('''CREATE TABLE IF NOT EXISTS pdf_chunks
(id INTEGER PRIMARY KEY,
page INTEGER,
chunk TEXT)''')
return conn
def process_pdf(file):
conn = init_db()
pdf = PdfReader(file)
for page_num, page in enumerate(pdf.pages):
if text := page.extract_text():
chunks = [text[i:i+1000] for i in range(0, len(text), 800)]
for chunk in chunks:
conn.execute("INSERT INTO pdf_chunks (page, chunk) VALUES (?, ?)",
(page_num+1, chunk.strip()))
conn.commit()
return conn
def main():
st.title("📄 PDF Processor")
st.caption("Version stable pour Pyodide")
if file := st.file_uploader("Upload PDF", type="pdf"):
with st.spinner("Traitement..."):
conn = process_pdf(file)
stats = conn.execute("""
SELECT COUNT(DISTINCT page) as pages,
COUNT(*) as chunks
FROM pdf_chunks
""").fetchone()
st.success(f"Résultat : {stats[0]} pages | {stats[1]} segments")
preview = conn.execute("""
SELECT page, chunk
FROM pdf_chunks
ORDER BY RANDOM()
LIMIT 3
""").fetchall()
st.write("Exemples de contenu :")
for p, c in preview:
st.write(f"**Page {p}:** `{c[:100].strip()}...`")
conn.close()
if __name__ == "__main__":
main()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?