File size: 2,612 Bytes
3b2a05d
2af7ec4
202fb2f
3b2a05d
2af7ec4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202fb2f
2af7ec4
 
 
 
 
 
 
 
 
202fb2f
2af7ec4
 
 
 
202fb2f
2af7ec4
 
 
 
 
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
import streamlit as st
import os
from model_processor import LlamaProcessor

st.set_page_config(page_title="Llama PDF Expert", layout="wide")

# --- UI Header ---
st.title("📄 PDF QA with Llama 3.2")
st.markdown("Upload a document and ask questions using a local Llama model.")

# --- Sidebar Configuration ---
with st.sidebar:
    st.header("1. Authentication")
    token = st.text_input("Hugging Face Token", type="password", help="Enter your HF 'Read' token.")
    model_choice = st.selectbox("Select Model", ["meta-llama/Llama-3.2-1B-Instruct", "meta-llama/Llama-3.2-3B-Instruct"])
    
    st.divider()
    st.header("2. Document Upload")
    uploaded_file = st.file_uploader("Upload PDF", type="pdf")

# --- Session State Initialization ---
if "processor" not in st.session_state:
    st.session_state.processor = None
if "vector_db" not in st.session_state:
    st.session_state.vector_db = None
if "messages" not in st.session_state:
    st.session_state.messages = []

# --- Logic: Model Loading & Processing ---
if uploaded_file and token:
    if st.session_state.processor is None:
        try:
            with st.spinner("Initializing Llama... (this may take a minute)"):
                st.session_state.processor = LlamaProcessor(model_choice, token)
            
            # Save and process PDF
            with open("temp_upload.pdf", "wb") as f:
                f.write(uploaded_file.getbuffer())
            
            with st.spinner("Indexing document..."):
                st.session_state.vector_db = st.session_state.processor.process_pdf("temp_upload.pdf")
            st.success("Document processed! Ready to chat.")
        except Exception as e:
            st.error(f"Error initializing: {str(e)}")

# --- Logic: Chat Interface ---
# Display chat history
for msg in st.session_state.messages:
    with st.chat_message(msg["role"]):
        st.markdown(msg["content"])

# User Input
if prompt := st.chat_input("Ask a question about the PDF..."):
    if not st.session_state.vector_db:
        st.warning("Please upload a PDF and provide a token first.")
    else:
        # User message
        st.session_state.messages.append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.markdown(prompt)

        # Assistant response
        with st.chat_message("assistant"):
            with st.spinner("Thinking..."):
                answer = st.session_state.processor.get_answer(prompt, st.session_state.vector_db)
                st.markdown(answer)
                st.session_state.messages.append({"role": "assistant", "content": answer})