anshharora commited on
Commit
510a110
·
verified ·
1 Parent(s): ab598d7

initial commit

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