QuickLearnerAI commited on
Commit
ac5e4ff
·
verified ·
1 Parent(s): e480586

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +235 -0
app.py ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import PyPDF2
3
+ import io
4
+ import os
5
+ import time
6
+ from together import Together
7
+ import textwrap
8
+ import tempfile
9
+
10
+ # 1st work is to extract the pdf file
11
+ def extract_pdf(pdf_file):
12
+ text= ""
13
+ try:
14
+ if hasattr(pdf_file, "read"): #convert pf into bytes format (data byte bit type)
15
+ pdf_content= pdf_file.read()
16
+ if hasattr(pdf_file,"seek"):
17
+ pdf_file.seek(0)
18
+
19
+ else:
20
+ #if it already in bites and read
21
+ pdf_content= pdf_file
22
+
23
+ pdf_reader= PyPDF2.PdfReader(io.BytesIO(pdf_content)) #START reading and need to extract the pdf
24
+
25
+ for page_num in range(len(pdf_reader.pages)):
26
+ page_text= pdf_reader.pages[page_num].extract_text()
27
+ if page_text: # check the extract is working or not
28
+ print("okay")
29
+ text= text + page_text + "\n"
30
+ else:
31
+ print("no extractable text is found there")
32
+ text += f"[Page{page_num+1}]"
33
+ if not text.strip():
34
+ return "no text extracted from PDF it a scanned file or image"
35
+
36
+ return text
37
+ except Exception as e:
38
+ return f"Error when extracting text from pdf : {str(e)}"
39
+
40
+
41
+ ## create a function to chat with the pdf
42
+ def chat_with_pdf(api_key, pdf_text, user_question, history):
43
+ if not api_key.strip():
44
+ return history + [(user_question,"Error: plase enter your own api key")], history
45
+ if not pdf_text.strip() or pdf_text.startswith("Error") or pdf_text.startswith("No text"):
46
+ return history + [(user_question,"Error: plase upload a valid pdf file with extractable text format")], history
47
+ if not user_question.strip():
48
+ return history + [(user_question,"Error: please enter a valid question")], history
49
+
50
+ try:
51
+ # model connections all for same
52
+ client= Together(api_key=api_key) # server e giye hi dibe and server response korbe vhul hole server response korbe na
53
+ max_content_len= 150000 # check for the number of word
54
+
55
+ if len(pdf_text) > max_content_len:
56
+ half_len = max_content_len//2
57
+ pdf_context= pdf_text[: half_len]+ "\n\n[....Content truncated due to length ....]\n\n" + pdf_text[-half_len:]
58
+ else:
59
+ pdf_content= pdf_text
60
+
61
+ system_message= f"""You are an intelligent assistant designed to read, understand, and extract information from PDF documents.
62
+ Based on any question or query the user asks—whether it's about content, summaries, data extraction, definitions, insights, or interpretation—you will
63
+ analyze the following PDF content and provide an accurate, helpful response grounded in the document. Always respond with clear, concise, and context-aware information.
64
+ PDF CONTENT:
65
+ {pdf_context}
66
+ 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."""
67
+
68
+ messages= [
69
+ {"role": "system", "content": system_message}
70
+ ]
71
+ for h_user , h_bot in history:
72
+ messages.append({"role": "user", "content": h_user})
73
+ messages.append({"role": "assistant", "content": h_bot})
74
+
75
+ #add the current user question into history
76
+ messages.append(["role": "user", "content": user_question])
77
+
78
+ #connect the model with the app
79
+ reponse = client.chat.completions.create(
80
+ model= "meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
81
+ messages= messages,
82
+ max_tokens= 5000 #5000
83
+ temperature= .7
84
+ )
85
+ assistance_reponse = response.choices[0].message.content
86
+
87
+ # update the chat history
88
+ new_history= history + [(user_question, assistance_reponse)] #history update purono conversation + current conver= new history
89
+
90
+ return new_history, new_history
91
+ except Exception as e:
92
+ error_message= f"Error:{str(e)}"
93
+ return history + [(user_question, error_message)], history
94
+
95
+ # now process the pdf before the chat
96
+ def process_pdf(pdf_file, api_key_input):
97
+ if pdf_file is None:
98
+ return "please upload a PDF file.","",[]
99
+ try:
100
+ file_name= os.path.basename(pdf_file.name) if hasattr(pdf_file,"name") else "please Upload"
101
+ pdf_text= extract_pdf(pdf_file)
102
+
103
+ if pdf_text.startswith("Error extracting text from PDF"):
104
+ return f"❌ {pdf_text}", "", []
105
+ if not pdf_text.strip() or pdf_text.startswith("No text could be extracted"):
106
+ return f"⚠️ {pdf_text}", "", []
107
+
108
+ word_count= len(pdf_text.split())
109
+ # Return a message with the file name and text content
110
+ status_message = f"✅ Successfully processed PDF: {file_name} ({word_count} words extracted)"
111
+
112
+ return status_message, pdf_text,[]
113
+ except Exception as e:
114
+ return f"❌ Error processing PDF: {str(e)}", "", []
115
+
116
+ #
117
+ def validate_api_key(api_key):
118
+ """Simple validation for API key format"""
119
+ if not api_key or not api_key.strip():
120
+ return "❌ API Key is required"
121
+
122
+ if len(api_key.strip()) < 10:
123
+ return "❌ API Key appears to be too short"
124
+
125
+ return "✓ API Key format looks valid (not verified with server)"
126
+
127
+
128
+ with gr.Blocks(title="chatsPDFsupport", theme=gr.themes.Ocean()) as app:
129
+ gr.Markdown("# 📄 ChatPDF with Together AI")
130
+ gr.Markdown("Upload a PDF and chat with it using the Llama-3.3-70B model.")
131
+
132
+ with gr.Row():
133
+ with gr.Column(scale=1)
134
+ api_key_input = gr.Textbox(
135
+ label="Together API Key",
136
+ placeholder="Enter your Together API key here...",
137
+ type="password")
138
+ # API key validation
139
+ api_key_status = gr.Textbox(
140
+ label="API Key Status",
141
+ interactive=False
142
+ )
143
+ # PDF upload
144
+ pdf_file = gr.File(
145
+ label="Upload PDF",
146
+ file_types=[".pdf"],
147
+ type="binary" # Ensure we get binary data
148
+ )
149
+ # Process PDF button
150
+ process_button = gr.Button("Process PDF")
151
+
152
+ # Status message
153
+ status_message = gr.Textbox(
154
+ label="Status",
155
+ interactive=False
156
+ )
157
+ # Hidden field to store the PDF text
158
+ pdf_text = gr.Textbox(visible=False)
159
+
160
+ # Optional: Show PDF preview
161
+ with gr.Accordion("PDF Content Preview", open=False):
162
+ pdf_preview = gr.Textbox(
163
+ label="Extracted Text Preview",
164
+ interactive=False,
165
+ max_lines=10,
166
+ show_copy_button=True
167
+ )
168
+
169
+ with gr.Column(scale=2): #right side column row
170
+ # Chat interface
171
+ chatbot = gr.Chatbot(
172
+ label="Chat with PDF",
173
+ height=500,
174
+ show_copy_button=True
175
+ )
176
+ # Question input
177
+ question = gr.Textbox(
178
+ label="Ask a question about the PDF",
179
+ placeholder="What is the main topic of this document?",
180
+ lines=2
181
+ )
182
+
183
+ #submit button
184
+ sumbit_button= gr.Button("Submit Question")
185
+
186
+ # Event handlers
187
+ def update_preview(text):
188
+ """Update the preview with the first few lines of the PDF text"""
189
+ if not text or text.startswith("Error") or text.startswith("No text"):
190
+ return text
191
+
192
+ # Get the first ~500 characters for preview
193
+ preview = text[:500]
194
+ if len(text) > 500:
195
+ preview += "...\n[Text truncated for preview. Full text will be used for chat.]"
196
+ return preview
197
+
198
+ # API key validation event
199
+ api_key_input.change(
200
+ fn=validate_api_key,
201
+ inputs=[api_key_input],
202
+ outputs=[api_key_status]
203
+ )
204
+
205
+ process_button.click(
206
+ fn=process_pdf,
207
+ inputs=[pdf_file, api_key_input],
208
+ outputs=[status_message, pdf_text, chatbot]
209
+ ).then(
210
+ fn=update_preview,
211
+ inputs=[pdf_text],
212
+ outputs=[pdf_preview]
213
+ )
214
+
215
+ submit_button.click(
216
+ fn=chat_with_pdf,
217
+ inputs=[api_key_input, pdf_text, question, chatbot],
218
+ outputs=[chatbot, chatbot]
219
+ ).then(
220
+ fn=lambda: "",
221
+ outputs=question
222
+ )
223
+
224
+ question.submit(
225
+ fn=chat_with_pdf,
226
+ inputs=[api_key_input, pdf_text, question, chatbot],
227
+ outputs=[chatbot, chatbot]
228
+ ).then(
229
+ fn=lambda: "",
230
+ outputs=question
231
+ )
232
+
233
+ # Launch the app
234
+ if __name__ == "__main__":
235
+ app.launch(share=True)