File size: 9,273 Bytes
347eef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# import os
# import re
# from aiohttp import ClientSession
# from google.cloud import vision
# from io import BytesIO
# import google.generativeai as genai
# import traceback
# import nltk
# from nltk.corpus import stopwords
# from nltk.tokenize import word_tokenize
# from nltk.stem import WordNetLemmatizer
# from string import punctuation
# from PIL import Image
# import io
# from PIL import Image, ImageEnhance, ImageFilter

# # Download necessary NLTK resources
# nltk.download('punkt')
# nltk.download('stopwords')
# nltk.download('wordnet')

# os.environ['GOOGLE_API_KEY'] = "AIzaSyA9sqz4YKQHKXR9TU1imw0DPOghzHOMiBo"
# # genai.configure(api_key = os.environ['GOOGLE_API_KEY'])

# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'cloudVisionAPI.json'

# # model = genai.GenerativeModel('gemini-pro')
# # model = genai.GenerativeModel('gemini-1.5-flash')

# async def remove_text_from_field(texts_to_remove, text_field):
#     for pattern in texts_to_remove:
#         text_field = re.sub(pattern, "", text_field)
#     return text_field

# async def getImage(img_url):
#     try:
#         async with ClientSession() as session:
#             async with session.get(img_url) as response:
#                 img_data = await response.read()
#                 return BytesIO(img_data)
#     except Exception as e:
#         raise ValueError(f"Error in getImage: {str(e)}")

# # async def detectText(url):
# #     try:
# #         client = vision.ImageAnnotatorClient()
# #         image_bytes = await getImage(url)
# #         image = vision.Image(content=image_bytes.getvalue())
        
# #         image_response = client.document_text_detection(image=image)
# #         if image_response.error.message:
# #             raise Exception("{}\nFor more info on error messages, check: ""https://cloud.google.com/apis/design/errors".format(image_response.error.message))
        
# #         image_texts = image_response.text_annotations
# #         imageData = image_texts[0].description
# #         return imageData
# #     except Exception as e:
# #         traceback.print_exc()
# #         raise ValueError(f"Error in detectText: {str(e)}")


# async def detectText(url, threshold=0.0):
#     try:
#         client = vision.ImageAnnotatorClient()
#         image_bytes = await getImage(url)
#         image = vision.Image(content=image_bytes.getvalue())
#         image_response = client.document_text_detection(image=image)

#         if image_response.error.message:
#             raise Exception(
#                 "{}\nFor more info on error messages, check: "
#                 "https://cloud.google.com/apis/design/errors".format(image_response.error.message)
#             )

#         image_texts = image_response.full_text_annotation

#         bangla_words = []
#         for page in image_texts.pages:
#             for block in page.blocks:
#                 for paragraph in block.paragraphs:
#                     for word in paragraph.words:
#                         word_text = ''.join([symbol.text for symbol in word.symbols])
#                         if word.confidence >= threshold:
#                             # Check if the word contains any Bangla character
#                             if re.search(r'[\u0980-\u09FF]', word_text):
#                                 bangla_words.append(word_text)

#         return ' '.join(bangla_words)

#     except Exception as e:
#         traceback.print_exc()
#         raise ValueError(f"Error in detectText: {str(e)}")

# def clean_text(text):
#     tokens = word_tokenize(text)
#     tokens = [word for word in tokens if word not in punctuation]
#     lemmatizer = WordNetLemmatizer()
#     tokens = [lemmatizer.lemmatize(word) for word in tokens]
#     clean_tokens = [i for i in tokens if i != '·']
#     print("Cleaned Tokens:", clean_tokens)         
#     return ' '.join(clean_tokens)

# async def main(url):
#     try:
#         data = await detectText(url)
#         # myQue = f"Extract only name from {data} also correct the name of Cigarettes and person name if the name is wrong. Dont give any other information except those name."
#         # response = model.generate_content(myQue)
#         # text = response.text
        
#         cleaned_text = clean_text(data)

#         return cleaned_text
#     except Exception as e:
#         traceback.print_exc()
#         raise ValueError(f"Error in main: {str(e)}")










import os
import re
import io
import traceback
import nltk
from aiohttp import ClientSession
from google.cloud import vision
from io import BytesIO
from string import punctuation
from PIL import Image, ImageEnhance, ImageFilter, ImageDraw, ImageFont
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
import google.generativeai as genai

# Environment variables
os.environ['GOOGLE_API_KEY'] = "AIzaSyA9sqz4YKQHKXR9TU1imw0DPOghzHOMiBo"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'cloudVisionAPI.json'

genai.configure(api_key = os.environ['GOOGLE_API_KEY'])

# # model = genai.GenerativeModel('gemini-pro')
model = genai.GenerativeModel('gemini-1.5-flash')

# ========== Utility Functions ==========

async def getImage(img_url):
    try:
        async with ClientSession() as session:
            async with session.get(img_url) as response:
                img_data = await response.read()
                return BytesIO(img_data)
    except Exception as e:
        raise ValueError(f"Error in getImage: {str(e)}")

counter = 0

async def preprocess_image_for_ocr(image_bytes):
    global counter
    image = Image.open(image_bytes).convert('L')  # Convert to grayscale

    # Save image to a bytes buffer
    byte_arr = io.BytesIO()
    image.save(byte_arr, format='PNG')
    counter +=1
    image.save(f"images/{counter}_image.jpg", format='PNG')
    return byte_arr.getvalue()

async def detectText(url, threshold=0.0):
    try:
        # Initialize the Vision client
        client = vision.ImageAnnotatorClient()
        
        # Fetch and preprocess image
        image_bytes = await getImage(url)
        processed_image_bytes = await preprocess_image_for_ocr(image_bytes)

        # Create Image object for Google Vision API
        image = vision.Image(content=processed_image_bytes)
        
        # Perform document text detection
        response = client.document_text_detection(image=image)

        # Check for errors in the API response
        if response.error.message:
            raise Exception(f"API Error: {response.error.message}\nCheck: https://cloud.google.com/apis/design/errors")

        # Extract text from the response
        image_texts = response.full_text_annotation
        bangla_words = []

        # Loop through the detected text and filter Bangla words
        for page in image_texts.pages:
            for block in page.blocks:
                for paragraph in block.paragraphs:
                    for word in paragraph.words:
                        word_text = ''.join([symbol.text for symbol in word.symbols])
                        if word.confidence >= threshold and re.search(r'[\u0980-\u09FF]', word_text):
                            bangla_words.append(word_text)

        # Return Bangla words as a space-separated string
        return ' '.join(bangla_words)

    except Exception as e:
        traceback.print_exc()
        raise ValueError(f"Error in detectText: {str(e)}")    
    
    
def clean_text(text):
    tokens = word_tokenize(text)
    tokens = [word for word in tokens if word not in punctuation]
    lemmatizer = WordNetLemmatizer()
    tokens = [lemmatizer.lemmatize(word) for word in tokens]
    clean_tokens = [i for i in tokens if i != '·']
    print("Cleaned Tokens:", clean_tokens)
    return ' '.join(clean_tokens)

    
    
    
    
    
async def main(url):
    try:
        dic = ['গোল্ডলিফ', 'লাকি স্ট্রাইক', 'বেনসন']
        
        # Extract the data using detectText function
        data = await detectText(url)
        
        # Define the query (myQue) to pass to the model
        myQue = f"""
        Extract and correct the names of cigarette brands and Bangladeshi people's names from the following text. The text may contain spelling errors, grammatical issues, and improperly formatted names. 

        Your task:
        - Extract and correct the names of cigarette brands and Bangladeshi people also bangla writings.
        - Return only the corrected data (in Bangla), with no additional information or explanations.

        Here is the provided text: 
        {data}

        Only return the corrected names, no extra text.
        """

        print("Query to Model:", myQue)  # Optional: For debugging to see the query sent to the model
        
        # Generate the response from the model
        response = model.generate_content(myQue)
        text = response.text.strip()
        clean_txt = clean_text(text)
        print("Response Text:", clean_txt)

        # Return the cleaned response with only the extracted names
        return clean_txt

    except Exception as e:
        # Print and raise a detailed error message for debugging
        traceback.print_exc()
        raise ValueError(f"Error in main: {str(e)}")