sairika commited on
Commit
6162181
·
verified ·
1 Parent(s): 6308683

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +271 -0
app.py ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import PyPDF2
3
+ import io
4
+ import time
5
+ import os
6
+ from together import Together
7
+ import textwrap
8
+ import tempfile
9
+
10
+ def extract_text_from_pdf(pdf_file):
11
+ """Extract text from a PDF file"""
12
+ text = ""
13
+ try:
14
+ # Check if the pdf_file is already in bytes format or needs conversion
15
+ if hasattr(pdf_file, 'read'):
16
+ # If it's a file-like object (from gradio upload)
17
+ pdf_content = pdf_file.read()
18
+ # Reset the file pointer for potential future reads
19
+ if hasattr(pdf_file, 'seek'):
20
+ pdf_file.seek(0)
21
+ else:
22
+ # If it's already bytes
23
+ pdf_content = pdf_file
24
+
25
+ # Read the PDF file
26
+ pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_content))
27
+
28
+ # Extract text from each page
29
+ for page_num in range(len(pdf_reader.pages)):
30
+ page_text = pdf_reader.pages[page_num].extract_text()
31
+ if page_text: # Check if text extraction worked
32
+ text += page_text + "\n\n"
33
+ else:
34
+ text += f"[Page {page_num+1} - No extractable text found]\n\n"
35
+
36
+ if not text.strip():
37
+ return "No text could be extracted from the PDF. The document may be scanned or image-based."
38
+
39
+ return text
40
+ except Exception as e:
41
+ return f"Error extracting text from PDF: {str(e)}"
42
+
43
+ def format_chat_history(history):
44
+ """Format the chat history for display"""
45
+ formatted_history = []
46
+ for user_msg, bot_msg in history:
47
+ formatted_history.append((user_msg, bot_msg))
48
+ return formatted_history
49
+
50
+ def chat_with_pdf(api_key, pdf_text, user_question, history):
51
+ """Chat with the PDF using Together API"""
52
+ if not api_key.strip():
53
+ return history + [(user_question, "Error: Please enter your Together API key.")], history
54
+
55
+ if not pdf_text.strip() or pdf_text.startswith("Error") or pdf_text.startswith("No text"):
56
+ return history + [(user_question, "Error: Please upload a valid PDF file with extractable text first.")], history
57
+
58
+ if not user_question.strip():
59
+ return history + [(user_question, "Error: Please enter a question.")], history
60
+
61
+ try:
62
+ # Initialize Together client with the API key
63
+ client = Together(api_key=api_key)
64
+
65
+ # Create the system message with PDF context
66
+ # Truncate the PDF text if it's too long (model context limit handling)
67
+ max_context_length = 10000 #10000
68
+
69
+ if len(pdf_text) > max_context_length:
70
+ # More sophisticated truncation that preserves beginning and end
71
+ half_length = max_context_length // 2
72
+ pdf_context = pdf_text[:half_length] + "\n\n[...Content truncated due to length...]\n\n" + pdf_text[-half_length:]
73
+ else:
74
+ pdf_context = pdf_text
75
+
76
+ system_message = f"""You are an intelligent assistant designed to read, understand, and extract information from PDF documents.
77
+ Based on any question or query the user asks—whether it's about content, summaries, data extraction, definitions, insights, or interpretation—you will
78
+ analyze the following PDF content and provide an accurate, helpful response grounded in the document. Always respond with clear, concise, and context-aware information.
79
+ PDF CONTENT:
80
+ {pdf_context}
81
+ 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."""
82
+
83
+ # Prepare message history for Together API
84
+ messages = [
85
+ {"role": "system", "content": system_message},
86
+ ]
87
+
88
+ # Add chat history
89
+ for h_user, h_bot in history:
90
+ messages.append({"role": "user", "content": h_user})
91
+ messages.append({"role": "assistant", "content": h_bot})
92
+
93
+ # Add the current user question
94
+ messages.append({"role": "user", "content": user_question})
95
+
96
+ # Call the Together API
97
+ response = client.chat.completions.create(
98
+ model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
99
+ messages=messages,
100
+ max_tokens=5000, #5000
101
+ temperature=0.7,
102
+ )
103
+
104
+ # Extract the assistant's response
105
+ assistant_response = response.choices[0].message.content
106
+
107
+ # Update the chat history
108
+ new_history = history + [(user_question, assistant_response)]
109
+
110
+ return new_history, new_history
111
+
112
+ except Exception as e:
113
+ error_message = f"Error: {str(e)}"
114
+ return history + [(user_question, error_message)], history
115
+
116
+ def process_pdf(pdf_file, api_key_input):
117
+ """Process the uploaded PDF file"""
118
+ if pdf_file is None:
119
+ return "Please upload a PDF file.", "", []
120
+
121
+ try:
122
+ # Get the file name
123
+ file_name = os.path.basename(pdf_file.name) if hasattr(pdf_file, 'name') else "Uploaded PDF"
124
+
125
+ # Extract text from the PDF
126
+ pdf_text = extract_text_from_pdf(pdf_file)
127
+
128
+ # Check if there was an error in extraction
129
+ if pdf_text.startswith("Error extracting text from PDF"):
130
+ return f"❌ {pdf_text}", "", []
131
+
132
+ if not pdf_text.strip() or pdf_text.startswith("No text could be extracted"):
133
+ return f"⚠️ {pdf_text}", "", []
134
+
135
+ # Count words for information
136
+ word_count = len(pdf_text.split())
137
+
138
+ # Return a message with the file name and text content
139
+ status_message = f"✅ Successfully processed PDF: {file_name} ({word_count} words extracted)"
140
+
141
+ # Also return an empty history
142
+ return status_message, pdf_text, []
143
+ except Exception as e:
144
+ return f"❌ Error processing PDF: {str(e)}", "", []
145
+
146
+ def validate_api_key(api_key):
147
+ """Simple validation for API key format"""
148
+ if not api_key or not api_key.strip():
149
+ return "❌ API Key is required"
150
+
151
+ if len(api_key.strip()) < 10:
152
+ return "❌ API Key appears to be too short"
153
+
154
+ return "✓ API Key format looks valid (not verified with server)"
155
+
156
+ # Create the Gradio interface
157
+ with gr.Blocks(title="ChatPDF with Together AI", theme=gr.themes.Ocean()) as app:
158
+ gr.Markdown("# 📄 ChatPDF with Together AI")
159
+ gr.Markdown("Upload a PDF and chat with it using the Llama-3.3-70B model.")
160
+
161
+ with gr.Row():
162
+ with gr.Column(scale=1):
163
+ # API Key input
164
+ api_key_input = gr.Textbox(
165
+ label="Together API Key",
166
+ placeholder="Enter your Together API key here...",
167
+ type="password"
168
+ )
169
+
170
+ # API key validation
171
+ api_key_status = gr.Textbox(
172
+ label="API Key Status",
173
+ interactive=False
174
+ )
175
+
176
+ # PDF upload
177
+ pdf_file = gr.File(
178
+ label="Upload PDF",
179
+ file_types=[".pdf"],
180
+ type="binary" # Ensure we get binary data
181
+ )
182
+
183
+ # Process PDF button
184
+ process_button = gr.Button("Process PDF")
185
+
186
+ # Status message
187
+ status_message = gr.Textbox(
188
+ label="Status",
189
+ interactive=False
190
+ )
191
+
192
+ # Hidden field to store the PDF text
193
+ pdf_text = gr.Textbox(visible=False)
194
+
195
+ # Optional: Show PDF preview
196
+ with gr.Accordion("PDF Content Preview", open=False):
197
+ pdf_preview = gr.Textbox(
198
+ label="Extracted Text Preview",
199
+ interactive=False,
200
+ max_lines=10,
201
+ show_copy_button=True
202
+ )
203
+
204
+ with gr.Column(scale=2):
205
+ # Chat interface
206
+ chatbot = gr.Chatbot(
207
+ label="Chat with PDF",
208
+ height=500,
209
+ show_copy_button=True
210
+ )
211
+
212
+ # Question input
213
+ question = gr.Textbox(
214
+ label="Ask a question about the PDF",
215
+ placeholder="What is the main topic of this document?",
216
+ lines=2
217
+ )
218
+
219
+ # Submit button
220
+ submit_button = gr.Button("Submit Question")
221
+
222
+ # Event handlers
223
+ def update_preview(text):
224
+ """Update the preview with the first few lines of the PDF text"""
225
+ if not text or text.startswith("Error") or text.startswith("No text"):
226
+ return text
227
+
228
+ # Get the first ~500 characters for preview
229
+ preview = text[:500]
230
+ if len(text) > 500:
231
+ preview += "...\n[Text truncated for preview. Full text will be used for chat.]"
232
+ return preview
233
+
234
+ # API key validation event
235
+ api_key_input.change(
236
+ fn=validate_api_key,
237
+ inputs=[api_key_input],
238
+ outputs=[api_key_status]
239
+ )
240
+
241
+ process_button.click(
242
+ fn=process_pdf,
243
+ inputs=[pdf_file, api_key_input],
244
+ outputs=[status_message, pdf_text, chatbot]
245
+ ).then(
246
+ fn=update_preview,
247
+ inputs=[pdf_text],
248
+ outputs=[pdf_preview]
249
+ )
250
+
251
+ submit_button.click(
252
+ fn=chat_with_pdf,
253
+ inputs=[api_key_input, pdf_text, question, chatbot],
254
+ outputs=[chatbot, chatbot]
255
+ ).then(
256
+ fn=lambda: "",
257
+ outputs=question
258
+ )
259
+
260
+ question.submit(
261
+ fn=chat_with_pdf,
262
+ inputs=[api_key_input, pdf_text, question, chatbot],
263
+ outputs=[chatbot, chatbot]
264
+ ).then(
265
+ fn=lambda: "",
266
+ outputs=question
267
+ )
268
+
269
+ # Launch the app
270
+ if __name__ == "__main__":
271
+ app.launch(share=True)