shukdevdattaEX commited on
Commit
7f34864
·
verified ·
1 Parent(s): c21811c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -1
app.py CHANGED
@@ -59,18 +59,197 @@ def chat_with_pdf(api_key, pdf_text, user_question, history):
59
  else:
60
  pdf_context = pdf_text
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
 
 
64
 
 
 
65
 
 
66
 
 
 
67
 
 
 
68
 
 
 
 
69
 
 
 
70
 
 
 
71
 
 
 
72
 
 
73
 
 
 
74
 
75
 
76
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  else:
60
  pdf_context = pdf_text
61
 
62
+
63
+ system_message = f"""You are an intelligent assistant designed to read, understand, and extract information from PDF documents.
64
+ Based on any question or query the user asks—whether it's about content, summaries, data extraction, definitions, insights, or interpretation—you will
65
+ analyze the following PDF content and provide an accurate, helpful response grounded in the document. Always respond with clear, concise, and context-aware information.
66
+ PDF CONTENT:
67
+ {pdf_context}
68
+ 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."""
69
+
70
+ messages = [
71
+ {"role": "system", "content": system_message},
72
+ ]
73
+
74
+ # Add chat history
75
+ for h_user, h_bot in history:
76
+ messages.append({"role": "user", "content": h_user}) #conv1, .....
77
+ messages.append({"role": "assistant", "content": h_bot}) #conv1res, ....
78
 
79
+ messages.append({"role": "user", "content": user_question}) #convx ----- History (context)
80
+
81
+ response=client.chat.completions.create(
82
+ model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
83
+ messages=messages,
84
+ max_tokens=5000, #5000
85
+ temperature=0.7,
86
+ )
87
+
88
+ # Extract the assistant's response
89
+ assistant_response = response.choices[0].message.content ## convx? Ans: convxres (error: ans (something is wrong: model out of limit))
90
+
91
+ new_history = history + [(user_question, assistant_response)]
92
+
93
+ return new_history, new_history
94
+
95
+ except Exception as e:
96
+ error_message = f"Error: {str(e)}"
97
+ return history + [(user_question, error_message)], history ### history + current question + error message
98
 
99
+
100
+ def process_pdf(pdf_file, api_key_input):
101
 
102
+ if pdf_file is None:
103
+ return "Please upload a PDF file.", "", []
104
 
105
+ try:
106
 
107
+ # Get the file name
108
+ file_name = os.path.basename(pdf_file.name) if hasattr(pdf_file, 'name') else "Upload PDF"
109
 
110
+ # Extract text from the PDF
111
+ pdf_text = extract_text_from_pdf(pdf_file)
112
 
113
+ # Check if there was an error in extraction
114
+ if pdf_text.startswith("Error extracting text from PDF"):
115
+ return f"❌ {pdf_text}", "", []
116
 
117
+ if not pdf_text.strip() or pdf_text.startswith("No text could be extracted"):
118
+ return f"⚠️ {pdf_text}", "", []
119
 
120
+ # Count words for information
121
+ word_count = len(pdf_text.split())
122
 
123
+ # Return a message with the file name and text content
124
+ status_message = f"✅ Successfully processed PDF: {file_name} ({word_count} words extracted)"
125
 
126
+ return status_message, pdf_text, []
127
 
128
+ except Exception as e:
129
+ return f"❌ Error processing PDF: {str(e)}", "", []
130
 
131
 
132
+ def validate_api_key(api_key):
133
+ """Simple validation for API key format"""
134
+ if not api_key or not api_key.strip():
135
+ return "❌ API Key is required"
136
+
137
+ if len(api_key.strip()) < 10:
138
+ return "❌ API Key appears to be too short"
139
+
140
+ return "✓ API Key format looks valid (not verified with server)"
141
+
142
+ ###UI
143
+
144
+ with gr.Blocks(title="ChatPDF with Together AI", theme=gr.themes.Ocean()) as app:
145
+ gr.Markdown("# 📄 ChatPDF with Together AI")
146
+ gr.Markdown("Upload a PDF and chat with it using the Llama-3.3-70B model.")
147
+
148
+ with gr.Row():
149
+ with gr.Column(scale=1):
150
+ # API Key input
151
+ api_key_input = gr.Textbox(
152
+ label="Together API Key",
153
+ placeholder="Enter your Together API key here...",
154
+ type="password"
155
+ )
156
+
157
+ # API key validation
158
+ api_key_status = gr.Textbox(
159
+ label="API Key Status",
160
+ interactive=False
161
+ )
162
+
163
+ # PDF upload
164
+ pdf_file = gr.File(
165
+ label="Upload PDF",
166
+ file_types=[".pdf"],
167
+ type="binary" # Ensure we get binary data
168
+ )
169
+
170
+ # Process PDF button
171
+ process_button = gr.Button("Process PDF")
172
+
173
+ # Status message
174
+ status_message = gr.Textbox(
175
+ label="Status",
176
+ interactive=False
177
+ )
178
+
179
+ with gr.Accordion("PDF Content Preview", open=True):
180
+ pdf_preview = gr.Textbox(
181
+ label="Extracted Text Preview",
182
+ interactive=False,
183
+ max_lines=10,
184
+ show_copy_button=True
185
+ )
186
+
187
+ with gr.Column(scale=2):
188
+ # Chat interface
189
+ chatbot = gr.Chatbot(
190
+ label="Chat with PDF",
191
+ height=500,
192
+ show_copy_button=True
193
+ )
194
+
195
+ # Question input
196
+ question = gr.Textbox(
197
+ label="Ask a question about the PDF",
198
+ placeholder="What is the main topic of this document?",
199
+ lines=2
200
+ )
201
+
202
+ # Submit button
203
+ submit_button = gr.Button("Submit Question")
204
+
205
+
206
+ # Event handlers
207
+ def update_preview(text):
208
+ """Update the preview with the first few lines of the PDF text"""
209
+ if not text or text.startswith("Error") or text.startswith("No text"):
210
+ return text
211
+
212
+ # Get the first ~500 characters for preview
213
+ preview = text[:500]
214
+ if len(text) > 500:
215
+ preview += "...\n[Text truncated for preview. Full text will be used for chat.]"
216
+ return preview
217
+
218
+ # API key validation event
219
+ api_key_input.change(
220
+ fn=validate_api_key,
221
+ inputs=[api_key_input],
222
+ outputs=[api_key_status]
223
+ )
224
+
225
+ process_button.click(
226
+ fn=process_pdf,
227
+ inputs=[pdf_file, api_key_input],
228
+ outputs=[status_message, pdf_text, chatbot]
229
+ ).then(
230
+ fn=update_preview,
231
+ inputs=[pdf_text],
232
+ outputs=[pdf_preview]
233
+ )
234
+
235
+ submit_button.click(
236
+ fn=chat_with_pdf,
237
+ inputs=[api_key_input, pdf_text, question, chatbot],
238
+ outputs=[chatbot, chatbot]
239
+ ).then(
240
+ fn=lambda: "",
241
+ outputs=question
242
+ )
243
+
244
+ question.submit(
245
+ fn=chat_with_pdf,
246
+ inputs=[api_key_input, pdf_text, question, chatbot],
247
+ outputs=[chatbot, chatbot]
248
+ ).then(
249
+ fn=lambda: "",
250
+ outputs=question
251
+ )
252
+
253
+ # Launch the app
254
+ if __name__ == "__main__":
255
+ app.launch(share=True)