File size: 5,643 Bytes
85c933f
 
 
 
 
 
 
 
 
 
 
 
 
9cd93f0
 
 
 
 
 
 
 
85c933f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e9c01b
85c933f
 
 
 
 
 
 
 
 
 
 
26caa1a
85c933f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import streamlit as st
import PyPDF2
import io
from typing import List, Dict
from groq import Groq

# Page configuration and styling
st.set_page_config(
    page_title="PDF AI Chat Assistant", 
    page_icon="📄", 
    layout="wide"
)

hide_menu_style = """
        <style>
        #MainMenu {visibility: hidden;}
        footer {visibility: hidden;}
        </style>
        """
st.markdown(hide_menu_style, unsafe_allow_html=True)

class PDFChatAssistant:
    def __init__(self):
        # Initialize session state variables
        if 'chat_history' not in st.session_state:
            st.session_state.chat_history = []
        if 'uploaded_pdf' not in st.session_state:
            st.session_state.uploaded_pdf = None
        if 'pdf_text' not in st.session_state:
            st.session_state.pdf_text = ""

    def extract_pdf_text(self, pdf_file) -> str:
        """
        Extract text from uploaded PDF file
        """
        try:
            pdf_reader = PyPDF2.PdfReader(pdf_file)
            text = ""
            for page in pdf_reader.pages:
                text += page.extract_text() + "\n\n"
            return text
        except Exception as e:
            st.error(f"Error extracting PDF text: {e}")
            return ""

    def create_groq_client(self) -> Groq:
        """
        Create Groq client with API key
        """
        client = Groq(api_key=st.secrets["GROQ_API_KEY_DOCUMENTOR"])
        return client

    def generate_ai_response(self, client: Groq, context: str, user_query: str) -> str:
        """
        Generate AI response using Groq API
        """
        try:
            chat_completion = client.chat.completions.create(
                messages=[
                    {
                        "role": "system", 
                        "content": f"Bạn là một trợ lý PDF hữu ích. Luôn trả lời bằng tiếng Việt. Sử dụng nội dung trong file PDF đính kèm để trả lời câu hỏi sau:\n\n{context}"
                    },
                    {
                        "role": "user", 
                        "content": user_query
                    }
                ],
                model="llama3-8b-8192"
            )
            return chat_completion.choices[0].message.content
        except Exception as e:
            st.error(f"Error generating AI response: {e}")
            return "Sorry, I couldn't generate a response."

    def display_chat_interface(self):
        """
        Main chat interface with PDF upload and interaction
        """
        st.title("📄 PDF AI Chat Assistant")

        # Sidebar for PDF upload and context management
        with st.sidebar:
            st.header("PDF Management")
            uploaded_file = st.file_uploader("Upload PDF", type=['pdf'])
            
            if uploaded_file is not None:
                st.session_state.uploaded_pdf = uploaded_file
                with st.spinner('Extracting text from PDF...'):
                    st.session_state.pdf_text = self.extract_pdf_text(uploaded_file)
                    st.success("PDF text extracted successfully!")
                
                # Context actions
                col1, col2 = st.columns(2)
                with col1:
                    if st.button("Summarize PDF", key="summarize_btn"):
                        st.session_state.chat_history.append({
                            "role": "user", 
                            "content": "Provide a comprehensive summary of this PDF"
                        })

                with col2:
                    if st.button("Key Points", key="key_points_btn"):
                        st.session_state.chat_history.append({
                            "role": "user", 
                            "content": "Extract the most important key points from this PDF"
                        })

        # Main chat area
        st.header("Chat with Your PDF")
        
        # Display chat history
        for message in st.session_state.chat_history:
            with st.chat_message(message['role']):
                st.markdown(message['content'])

        # Groq Client setup
        client = self.create_groq_client()
        
        # User input
        if prompt := st.chat_input("Ask a question about your PDF"):
            # Add user message to chat history
            st.session_state.chat_history.append({
                "role": "user", 
                "content": prompt
            })

            # Display user message
            with st.chat_message("user"):
                st.markdown(prompt)

            # Generate and display AI response
            if client and st.session_state.pdf_text:
                with st.chat_message("assistant"):
                    with st.spinner('Generating response...'):
                        response = self.generate_ai_response(
                            client, 
                            st.session_state.pdf_text, 
                            prompt
                        )
                        st.markdown(response)
                
                # Add AI response to chat history
                st.session_state.chat_history.append({
                    "role": "assistant", 
                    "content": response
                })
            else:
                st.warning("Please upload a PDF.")

        # Reset button
        if st.button("Clear Chat", key="reset_btn"):
            st.session_state.chat_history = []
            st.experimental_rerun()

def main():
    assistant = PDFChatAssistant()
    assistant.display_chat_interface()

if __name__ == "__main__":
    main()