File size: 8,614 Bytes
ac5e4ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ae77ed
 
e45ea87
ac5e4ff
7ae77ed
 
7268d90
ac5e4ff
 
7ae77ed
 
 
ac5e4ff
 
 
 
 
 
edc46ec
 
ac5e4ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import gradio as gr 
import PyPDF2
import io
import os
import time
from together import Together 
import textwrap 
import tempfile

# 1st work is to extract the pdf file 
def extract_pdf(pdf_file):
    text= ""
    try:
        if hasattr(pdf_file, "read"):  #convert pf into bytes format (data byte bit type)
            pdf_content= pdf_file.read()
            if hasattr(pdf_file,"seek"):
                pdf_file.seek(0)

        else:
            #if it already in bites and read
            pdf_content= pdf_file

        pdf_reader= PyPDF2.PdfReader(io.BytesIO(pdf_content)) #START reading and need to extract the pdf

        for page_num in range(len(pdf_reader.pages)):
            page_text= pdf_reader.pages[page_num].extract_text()
            if page_text: # check the extract is working or not
                print("okay")
                text= text + page_text + "\n"
            else:
                print("no extractable text is found there")
                text += f"[Page{page_num+1}]" 
        if not text.strip():
            return "no text extracted from PDF it a scanned file or image"

        return text
    except Exception as e:
        return f"Error when extracting text from pdf : {str(e)}"


## create a function to chat with the pdf
def chat_with_pdf(api_key, pdf_text, user_question, history):
    if not api_key.strip():
        return history + [(user_question,"Error: plase enter your own api key")], history
    if not pdf_text.strip() or pdf_text.startswith("Error") or pdf_text.startswith("No text"):
        return history + [(user_question,"Error: plase upload a valid pdf file with extractable text format")], history
    if not user_question.strip():
        return history + [(user_question,"Error: please  enter a valid question")], history

    try:
        # model connections all for same
        client= Together(api_key=api_key) # server e giye hi dibe and server response korbe vhul hole server response korbe na 
        max_content_len= 150000 # check for the number of word 

        if len(pdf_text) > max_content_len:
            half_len = max_content_len//2
            pdf_context= pdf_text[: half_len]+ "\n\n[....Content truncated due to length ....]\n\n" + pdf_text[-half_len:]
        else:
            pdf_content= pdf_text

        system_message= f"""You are an intelligent assistant designed to read, understand, and extract information from PDF documents. 
Based on any question or query the user asks—whether it's about content, summaries, data extraction, definitions, insights, or interpretation—you will 
analyze the following PDF content and provide an accurate, helpful response grounded in the document. Always respond with clear, concise, and context-aware information.
PDF CONTENT:
{pdf_context}
Answer the user's questions only based on the PDF content above. If the answer cannot be found in the PDF, politely state that the information is not available in the provided document."""

         # Prepare message history for Together API
        messages = [
            {"role": "system", "content": system_message},
        ]
        
        # Add chat history
        for h_user, h_bot in history:
            messages.append({"role": "user", "content": h_user})
            messages.append({"role": "assistant", "content": h_bot})
        
        # Add the current user question
        messages.append({"role": "user", "content": user_question})


        #connect the model with the app
        reponse = client.chat.completions.create(
            model= "meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
            messages= messages,
            max_tokens= 5000, #5000
            temperature= .7,
        )
        assistance_reponse = response.choices[0].message.content

        # update the chat history
        new_history= history + [(user_question, assistance_reponse)] #history update purono conversation + current conver= new history

        return new_history, new_history
    except Exception as e:
        error_message= f"Error:{str(e)}"
        return history + [(user_question, error_message)], history

# now process the pdf before the chat
def process_pdf(pdf_file, api_key_input):
    if pdf_file is None:
        return "please upload a PDF file.","",[]
    try:
        file_name= os.path.basename(pdf_file.name) if hasattr(pdf_file,"name") else "please Upload"
        pdf_text= extract_pdf(pdf_file)

        if pdf_text.startswith("Error extracting text from PDF"):
             return f"❌ {pdf_text}", "", []
        if not pdf_text.strip() or pdf_text.startswith("No text could be extracted"):
            return f"⚠️ {pdf_text}", "", []

        word_count= len(pdf_text.split())
        # Return a message with the file name and text content
        status_message = f"✅ Successfully processed PDF: {file_name} ({word_count} words extracted)"

        return status_message, pdf_text,[]
    except Exception as e:
        return f"❌ Error processing PDF: {str(e)}", "", []

# 
def validate_api_key(api_key):
    """Simple validation for API key format"""
    if not api_key or not api_key.strip():
        return "❌ API Key is required"
    
    if len(api_key.strip()) < 10:
        return "❌ API Key appears to be too short"
    
    return "✓ API Key format looks valid (not verified with server)"


with gr.Blocks(title="chatsPDFsupport", theme=gr.themes.Ocean()) as app:
    gr.Markdown("# 📄 ChatPDF with Together AI")
    gr.Markdown("Upload a PDF and chat with it using the Llama-3.3-70B model.")

    with gr.Row():
        with gr.Column(scale=1)
        api_key_input = gr.Textbox(
                label="Together API Key",
                placeholder="Enter your Together API key here...",
                type="password")
        # API key validation
            api_key_status = gr.Textbox(
                label="API Key Status",
                interactive=False
            )
        # PDF upload
            pdf_file = gr.File(
                label="Upload PDF",
                file_types=[".pdf"],
                type="binary"  # Ensure we get binary data
            )
        # Process PDF button
            process_button = gr.Button("Process PDF")
            
        # Status message
        status_message = gr.Textbox(
            label="Status",
            interactive=False
        )
        # Hidden field to store the PDF text
        pdf_text = gr.Textbox(visible=False)

        # Optional: Show PDF preview
        with gr.Accordion("PDF Content Preview", open=False):
            pdf_preview = gr.Textbox(
                label="Extracted Text Preview",
                interactive=False,
                max_lines=10,
                show_copy_button=True
            )

    with gr.Column(scale=2): #right side column row
        # Chat interface
        chatbot = gr.Chatbot(
        label="Chat with PDF",
        height=500,
        show_copy_button=True
        )
    # Question input
        question = gr.Textbox(
            label="Ask a question about the PDF",
            placeholder="What is the main topic of this document?",
            lines=2
            )

        #submit button 
        sumbit_button= gr.Button("Submit Question")
 
       # Event handlers
    def update_preview(text):
        """Update the preview with the first few lines of the PDF text"""
        if not text or text.startswith("Error") or text.startswith("No text"):
            return text
        
        # Get the first ~500 characters for preview
        preview = text[:500]
        if len(text) > 500:
            preview += "...\n[Text truncated for preview. Full text will be used for chat.]"
        return preview

    # API key validation event
    api_key_input.change(
        fn=validate_api_key,
        inputs=[api_key_input],
        outputs=[api_key_status]
    )
    
    process_button.click(
        fn=process_pdf,
        inputs=[pdf_file, api_key_input],
        outputs=[status_message, pdf_text, chatbot]
    ).then(
        fn=update_preview,
        inputs=[pdf_text],
        outputs=[pdf_preview]
    )
    
    submit_button.click(
        fn=chat_with_pdf,
        inputs=[api_key_input, pdf_text, question, chatbot],
        outputs=[chatbot, chatbot]
    ).then(
        fn=lambda: "",
        outputs=question
    )
    
    question.submit(
        fn=chat_with_pdf,
        inputs=[api_key_input, pdf_text, question, chatbot],
        outputs=[chatbot, chatbot]
    ).then(
        fn=lambda: "",
        outputs=question
    )

# Launch the app
if __name__ == "__main__":
    app.launch(share=True)