anshharora commited on
Commit
d950883
·
verified ·
1 Parent(s): be27fdd

Upload 3 files

Browse files
Files changed (3) hide show
  1. .env +8 -0
  2. .gitignore +5 -0
  3. app.py +215 -0
.env ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # Path to Tesseract executable
2
+ TESSERACT_PATH=C:\Users\GGN06-Ansh\Downloads\Tesseract-OCR\Tesseract-OCR\tesseract.exe
3
+
4
+ # Path to Poppler for PDF image extraction
5
+ POPPLER_PATH=C:\Users\GGN06-Ansh\Downloads\Release-24.02.0-0\poppler-24.02.0\Library\bin
6
+
7
+ # Groq API Key
8
+ GROQ_API_KEY=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
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # Ignore .env file
2
+ .env
3
+
4
+ # Ignore app.py
5
+ app.py
app.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq, RateLimitError
3
+ import pandas as pd
4
+ from PIL import Image
5
+ import pytesseract
6
+ import pdfplumber
7
+ from pdf2image import convert_from_path
8
+ import os
9
+ import time
10
+ from dotenv import load_dotenv
11
+
12
+ # Load environment variables from .env file
13
+ load_dotenv()
14
+
15
+ # Set the path to Tesseract executable
16
+ pytesseract.pytesseract.tesseract_cmd = os.getenv("TESSERACT_CMD")
17
+
18
+ # Set the path to Poppler for PDF image extraction
19
+ poppler_path = os.getenv("POPPLER_PATH")
20
+
21
+ # Your Groq API key
22
+ YOUR_GROQ_API_KEY = os.getenv("GROQ_API_KEY")
23
+
24
+ # Initialize Groq client
25
+ client = Groq(api_key=YOUR_GROQ_API_KEY)
26
+
27
+ # Global variable to store extracted text
28
+ extracted_text = ""
29
+
30
+ def extract_text_from_image(image):
31
+ return pytesseract.image_to_string(image)
32
+
33
+ def remove_header_footer(image, header_height=3.9, footer_height=2.27):
34
+ width, height = image.size
35
+ header_height_pixels = int(header_height * 96) # Convert inches to pixels (assuming 96 DPI)
36
+ footer_height_pixels = int(footer_height * 96)
37
+ cropping_box = (0, header_height_pixels, width, height - footer_height_pixels)
38
+ return image.crop(cropping_box)
39
+
40
+ def handle_file(file, page_range=None):
41
+ global extracted_text
42
+ extracted_text = ""
43
+
44
+ if file is None:
45
+ return None, "No file uploaded"
46
+
47
+ file_name = file.name.lower()
48
+
49
+ if file_name.endswith(('png', 'jpg', 'jpeg')):
50
+ image = Image.open(file)
51
+ extracted_text = extract_text_from_image(image)
52
+ return image, extracted_text
53
+
54
+ elif file_name.endswith('pdf'):
55
+ text = ""
56
+ pdf_images = []
57
+ start_page = 1
58
+ end_page = None
59
+
60
+ if page_range:
61
+ try:
62
+ start_page, end_page = map(int, page_range.split('-'))
63
+ except ValueError:
64
+ start_page = int(page_range)
65
+ end_page = start_page
66
+
67
+ with pdfplumber.open(file) as pdf_file:
68
+ total_pages = len(pdf_file.pages)
69
+ end_page = end_page or total_pages
70
+
71
+ for page_number in range(start_page - 1, end_page):
72
+ page = pdf_file.pages[page_number]
73
+ page_text = page.extract_text() or ""
74
+ text += f"Page {page_number + 1}:\n{page_text}\n"
75
+
76
+ try:
77
+ page_images = convert_from_path(file.name, first_page=page_number + 1, last_page=page_number + 1, poppler_path=poppler_path)
78
+ page_images = [remove_header_footer(img) for img in page_images]
79
+ pdf_images.extend(page_images)
80
+ for img in page_images:
81
+ image_text = extract_text_from_image(img)
82
+ text += f"Page {page_number + 1} (Image):\n{image_text}\n"
83
+ except Exception as e:
84
+ text += f"Error processing images on page {page_number + 1}: {e}\n"
85
+
86
+ extracted_text = text
87
+ if pdf_images:
88
+ return pdf_images[0], extracted_text
89
+ else:
90
+ return None, extracted_text
91
+
92
+ elif file_name.endswith(('xls', 'xlsx')):
93
+ df = pd.read_excel(file)
94
+ extracted_text = df.to_string()
95
+ return None, extracted_text
96
+
97
+ elif file_name.endswith('csv'):
98
+ df = pd.read_csv(file)
99
+ extracted_text = df.to_string()
100
+ return None, extracted_text
101
+
102
+ else:
103
+ return None, "Unsupported file type"
104
+
105
+ def split_text(text, max_length=2000):
106
+ words = text.split()
107
+ chunks = []
108
+ current_chunk = []
109
+ current_length = 0
110
+ for word in words:
111
+ word_length = len(word) + 1 # +1 for the space or punctuation
112
+ if current_length + word_length > max_length:
113
+ chunks.append(" ".join(current_chunk))
114
+ current_chunk = [word]
115
+ current_length = word_length
116
+ else:
117
+ current_chunk.append(word)
118
+ current_length += word_length
119
+ if current_chunk:
120
+ chunks.append(" ".join(current_chunk))
121
+ return chunks
122
+
123
+ def is_rate_limited():
124
+ # Implement a method to check rate limit status if needed
125
+ return False
126
+
127
+ def chat_groq_sync(user_input, history, extracted_text):
128
+ retries = 5
129
+ while retries > 0:
130
+ rate_limit_status = is_rate_limited()
131
+ if rate_limit_status:
132
+ return f"{rate_limit_status} Please try again later."
133
+
134
+ messages = [{"role": "system", "content": "The following text is extracted from the uploaded file:\n" + extracted_text}]
135
+ for msg in history:
136
+ messages.append({"role": "user", "content": msg[0]})
137
+ messages.append({"role": "assistant", "content": msg[1]})
138
+ messages.append({"role": "user", "content": user_input})
139
+
140
+ try:
141
+ response = client.chat.completions.create(
142
+ model="llama3-70b-8192",
143
+ messages=messages,
144
+ max_tokens=1000,
145
+ temperature=0.4
146
+ )
147
+
148
+ response_content = response.choices[0].message.content
149
+ return response_content
150
+ except RateLimitError as e:
151
+ error_info = e.args[0] if e.args else {}
152
+ error_message = error_info.get('error', {}).get('message', '') if isinstance(error_info, dict) else str(error_info)
153
+
154
+ wait_time = 60
155
+ if 'try again in' in error_message:
156
+ try:
157
+ wait_time = float(error_message.split('try again in ')[-1].split('s')[0])
158
+ except ValueError:
159
+ pass
160
+
161
+ print(f"Rate limit error: {error_message}")
162
+ print(f"Retrying in {wait_time:.2f} seconds...")
163
+ retries -= 1
164
+ if retries > 0:
165
+ time.sleep(wait_time)
166
+ else:
167
+ return "Rate limit exceeded. Please try again later."
168
+ except Exception as e:
169
+ print(f"An unexpected error occurred: {e}")
170
+ return "An unexpected error occurred. Please try again later."
171
+
172
+ def update_chat(user_input, history):
173
+ global extracted_text
174
+ response = chat_groq_sync(user_input, history, extracted_text)
175
+ history.append((user_input, response))
176
+ return history, history, ""
177
+
178
+ with gr.Blocks() as demo:
179
+ with gr.Row():
180
+ with gr.Column(scale=1):
181
+ gr.Markdown("# RAG Chatbot")
182
+ gr.Markdown("Check out the [GitHub](https://github.com/anshh-arora?tab=repositories) for more information.")
183
+
184
+ file = gr.File(label="Upload your file")
185
+ page_range = gr.Textbox(label="If the uploaded document is a PDF and has more than 10 pages, enter the page range (e.g., 1-3) or specific page number (e.g., 2):", lines=1, visible=False, interactive=True)
186
+ file_upload_button = gr.Button("Upload File")
187
+ image_display = gr.Image(label="Uploaded Image", visible=False)
188
+ extracted_text_display = gr.Textbox(label="Extracted Text", interactive=False)
189
+
190
+ with gr.Column(scale=3):
191
+ gr.Markdown("# Chat with your file")
192
+ history = gr.State([])
193
+ with gr.Column():
194
+ chatbot = gr.Chatbot(height=500, bubble_full_width=False)
195
+ user_input = gr.Textbox(placeholder="Enter Your Query", visible=True, scale=7, interactive=True)
196
+
197
+ clear_btn = gr.Button("Clear")
198
+ undo_btn = gr.Button("Undo")
199
+
200
+ user_input.submit(update_chat, [user_input, history], [chatbot, history, user_input])
201
+ clear_btn.click(lambda: ([], []), None, [chatbot, history])
202
+ undo_btn.click(lambda h: h[:-2], history, history)
203
+
204
+ def show_page_range_input(file):
205
+ if file and file.name.lower().endswith('pdf'):
206
+ with pdfplumber.open(file) as pdf_file:
207
+ if len(pdf_file.pages) > 10:
208
+ return gr.update(visible=True)
209
+ return gr.update(visible=False)
210
+
211
+ file.change(show_page_range_input, inputs=file, outputs=page_range)
212
+ file_upload_button.click(handle_file, [file, page_range], [image_display, extracted_text_display])
213
+
214
+ if __name__ == "__main__":
215
+ demo.launch(share=True)