File size: 1,709 Bytes
4f7d52a 2e215e3 4f7d52a 788991c 4f7d52a 2cbf54a 1ac7c73 |
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 |
import streamlit as st
from pdf_extractor import get_pdf_text
from knowledge_base import create_faiss_index, search_faiss
from chatbot import generate_response
try:
import fitz # PyMuPDF
print("β
PyMuPDF (fitz) is installed successfully!")
except ImportError as e:
print(f"β ImportError: {e}")
st.title("Ψ§ΩΩ
Ψ³Ψ§ΨΉΨ― Ψ§ΩΨ±ΩΩ
Ω")
# Step 1: Upload a PDF
uploaded_file = st.file_uploader("π Upload a PDF", type=["pdf"])
if uploaded_file:
with open("temp.pdf", "wb") as f:
f.write(uploaded_file.getbuffer())
st.success("β
PDF uploaded successfully!")
# Step 2: Extract text from PDF
st.info("π Extracting text from PDF...")
pdf_text = get_pdf_text("temp.pdf") # Load the PDF and extract text
texts = pdf_text.split("\n") # Split text into paragraphs
faiss_index, stored_texts = create_faiss_index(texts) # Store in FAISS
st.success("β
Knowledge base created!")
# Step 3: Chat Interface
st.header("π¬ Chat with Your PDF")
user_query = st.text_input("Ask a question:")
if user_query:
st.info("π Searching for relevant information...")
relevant_texts = search_faiss(faiss_index, stored_texts, user_query, top_k=3)
st.success("β
Found relevant content!")
context = "\n".join(relevant_texts)
st.info("π€ Generating answer...")
answer = generate_response(context, user_query)
st.write("**Chatbot Answer:**", answer)
import os
import streamlit as st
# Your existing Streamlit code goes here
if __name__ == "__main__":
port = int(os.environ.get("PORT", 7860)) # Default Hugging Face Spaces port
|