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