QuickLearnerAI commited on
Commit
a885d27
·
verified ·
1 Parent(s): e051f65

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +211 -0
app.py ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import PyPDF2
3
+ import io
4
+ import os
5
+ from together import Together
6
+ from pdf2image import convert_from_bytes
7
+ import pytesseract
8
+ from PIL import Image
9
+
10
+ # Optional (for Windows): Uncomment if needed
11
+ # pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
12
+
13
+ def extract_text_with_ocr(pdf_bytes):
14
+ try:
15
+ images = convert_from_bytes(pdf_bytes)
16
+ text = ""
17
+ for i, img in enumerate(images):
18
+ ocr_result = pytesseract.image_to_string(img)
19
+ if ocr_result.strip():
20
+ text += f"[Page {i+1} - OCR Extracted Text]\n{ocr_result}\n\n"
21
+ else:
22
+ text += f"[Page {i+1} - No text found via OCR]\n\n"
23
+ return text if text.strip() else "No text could be extracted using OCR."
24
+ except Exception as e:
25
+ return f"Error during OCR extraction: {str(e)}"
26
+
27
+ def extract_text_from_pdf(pdf_file):
28
+ text = ""
29
+ try:
30
+ if hasattr(pdf_file, 'read'):
31
+ pdf_content = pdf_file.read()
32
+ if hasattr(pdf_file, 'seek'):
33
+ pdf_file.seek(0)
34
+ else:
35
+ pdf_content = pdf_file
36
+
37
+ pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_content))
38
+ for page_num in range(len(pdf_reader.pages)):
39
+ page_text = pdf_reader.pages[page_num].extract_text()
40
+ if page_text:
41
+ text += page_text + "\n\n"
42
+ else:
43
+ text += f"[Page {page_num+1} - No extractable text found]\n\n"
44
+
45
+ # OCR fallback if needed
46
+ if not text.strip() or all("No extractable text" in line for line in text.splitlines()):
47
+ ocr_text = extract_text_with_ocr(pdf_content)
48
+ if ocr_text:
49
+ text += "\n[OCR Fallback Extracted Text]\n" + ocr_text
50
+
51
+ return text if text.strip() else "No text could be extracted from the PDF."
52
+ except Exception as e:
53
+ return f"Error extracting text from PDF: {str(e)}"
54
+
55
+ def chat_with_pdf(api_key, pdf_text, user_question, history):
56
+ if not api_key.strip():
57
+ return history + [{"role": "user", "content": user_question}, {"role": "assistant", "content": "Error: Please enter your Together API key."}], history
58
+
59
+ if not pdf_text.strip() or pdf_text.startswith("Error") or pdf_text.startswith("No text"):
60
+ return history + [{"role": "user", "content": user_question}, {"role": "assistant", "content": "Error: Please upload a valid PDF file with extractable text first."}], history
61
+
62
+ if not user_question.strip():
63
+ return history + [{"role": "user", "content": user_question}, {"role": "assistant", "content": "Error: Please enter a question."}], history
64
+
65
+ try:
66
+ client = Together(api_key=api_key)
67
+
68
+ max_context_length = 10000
69
+ if len(pdf_text) > max_context_length:
70
+ half_length = max_context_length // 2
71
+ pdf_context = pdf_text[:half_length] + "\n\n[...Content truncated due to length...]\n\n" + pdf_text[-half_length:]
72
+ else:
73
+ pdf_context = pdf_text
74
+
75
+ system_message = f"""You are an intelligent assistant designed to read, understand, and extract information from PDF documents.
76
+ Based on any question or query the user asks—whether it's about content, summaries, data extraction, definitions, insights, or interpretation—you will
77
+ analyze the following PDF content and provide an accurate, helpful response grounded in the document. Always respond with clear, concise, and context-aware information.
78
+ PDF CONTENT:
79
+ {pdf_context}
80
+ 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."""
81
+
82
+ messages = [{"role": "system", "content": system_message}]
83
+ for msg in history:
84
+ messages.append(msg)
85
+
86
+ messages.append({"role": "user", "content": user_question})
87
+
88
+ response = client.chat.completions.create(
89
+ model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
90
+ messages=messages,
91
+ max_tokens=5000,
92
+ temperature=0.7,
93
+ )
94
+
95
+ assistant_response = response.choices[0].message.content
96
+ new_history = history + [
97
+ {"role": "user", "content": user_question},
98
+ {"role": "assistant", "content": assistant_response}
99
+ ]
100
+
101
+ return new_history, new_history
102
+
103
+ except Exception as e:
104
+ return history + [{"role": "user", "content": user_question}, {"role": "assistant", "content": f"Error: {str(e)}"}], history
105
+
106
+ def process_pdf(pdf_file, api_key_input):
107
+ if pdf_file is None:
108
+ return "Please upload a PDF file.", "", []
109
+
110
+ try:
111
+ file_name = os.path.basename(pdf_file.name) if hasattr(pdf_file, 'name') else "Uploaded PDF"
112
+ pdf_text = extract_text_from_pdf(pdf_file)
113
+
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
+ word_count = len(pdf_text.split())
121
+ status_message = f"✅ Successfully processed PDF: {file_name} ({word_count} words extracted)"
122
+ return status_message, pdf_text, []
123
+ except Exception as e:
124
+ return f"❌ Error processing PDF: {str(e)}", "", []
125
+
126
+ def validate_api_key(api_key):
127
+ if not api_key or not api_key.strip():
128
+ return "❌ API Key is required"
129
+ if len(api_key.strip()) < 10:
130
+ return "❌ API Key appears to be too short"
131
+ return "✓ API Key format looks valid (not verified with server)"
132
+
133
+ def update_preview(text):
134
+ if not text or text.startswith("Error") or text.startswith("No text"):
135
+ return text
136
+ preview = text[:500]
137
+ if len(text) > 500:
138
+ preview += "...\n[Text truncated for preview. Full text will be used for chat.]"
139
+ return preview
140
+
141
+ def clear_all():
142
+ return "", "", "", "", [], "", ""
143
+
144
+ # 🚀 Gradio Interface
145
+ with gr.Blocks(title="ChatPDF with OCR + Together AI", theme=gr.themes.Ocean()) as app:
146
+ gr.Markdown("# 📄 ChatPDF with OCR + Together AI")
147
+ gr.Markdown("Upload a PDF (even scanned/image-based), and chat with it using the Llama-3.3-70B model.")
148
+
149
+ with gr.Row():
150
+ with gr.Column(scale=1):
151
+ api_key_input = gr.Textbox(label="Together API Key", placeholder="Enter your Together API key here...", type="password")
152
+ api_key_status = gr.Textbox(label="API Key Status", interactive=False)
153
+ pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"], type="binary")
154
+ process_button = gr.Button("Process PDF")
155
+ status_message = gr.Textbox(label="Status", interactive=False)
156
+ pdf_text = gr.Textbox(visible=False)
157
+
158
+ with gr.Accordion("PDF Content Preview", open=False):
159
+ pdf_preview = gr.Textbox(label="Extracted Text Preview", interactive=False, max_lines=10, show_copy_button=True)
160
+
161
+ with gr.Column(scale=2):
162
+ chatbot = gr.Chatbot(label="Chat with PDF", height=500, show_copy_button=True, type="messages")
163
+ question = gr.Textbox(label="Ask a question about the PDF", placeholder="What is the main topic of this document?", lines=2)
164
+ submit_button = gr.Button("Submit Question")
165
+ clear_button = gr.Button("Clear Chat & Reset", variant="stop")
166
+
167
+ api_key_input.change(fn=validate_api_key, inputs=[api_key_input], outputs=[api_key_status])
168
+
169
+ process_button.click(
170
+ fn=process_pdf,
171
+ inputs=[pdf_file, api_key_input],
172
+ outputs=[status_message, pdf_text, chatbot]
173
+ ).then(
174
+ fn=update_preview,
175
+ inputs=[pdf_text],
176
+ outputs=[pdf_preview]
177
+ )
178
+
179
+ submit_button.click(
180
+ fn=chat_with_pdf,
181
+ inputs=[api_key_input, pdf_text, question, chatbot],
182
+ outputs=[chatbot, chatbot]
183
+ ).then(
184
+ fn=lambda: "",
185
+ outputs=question
186
+ )
187
+
188
+ question.submit(
189
+ fn=chat_with_pdf,
190
+ inputs=[api_key_input, pdf_text, question, chatbot],
191
+ outputs=[chatbot, chatbot]
192
+ ).then(
193
+ fn=lambda: "",
194
+ outputs=question
195
+ )
196
+
197
+ clear_button.click(
198
+ fn=clear_all,
199
+ outputs=[
200
+ api_key_input,
201
+ api_key_status,
202
+ question,
203
+ pdf_text,
204
+ chatbot,
205
+ status_message,
206
+ pdf_preview
207
+ ]
208
+ )
209
+
210
+ if __name__ == "__main__":
211
+ app.launch(share=True)