-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcitation.py
More file actions
362 lines (299 loc) · 15.1 KB
/
Copy pathcitation.py
File metadata and controls
362 lines (299 loc) · 15.1 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from anthropic import Anthropic
import time
import requests
from bs4 import BeautifulSoup
import base64
from io import BytesIO
import os
from dotenv import load_dotenv
import tempfile
from trafilatura import fetch_url, extract
from twocaptcha import TwoCaptcha
class ImageCitationFinder:
def __init__(self, show_browser=False): # Changed parameter name to be more descriptive
load_dotenv()
self.api_key = os.getenv('CLAUDE_API_KEY')
if not self.api_key:
raise ValueError("CLAUDE_API_KEY not found in environment variables")
self.client = Anthropic(api_key=self.api_key)
self.show_browser = show_browser # Store parameter for text extraction
self.captcha_api_key = os.getenv('CAPTCHA_API_KEY')
if not self.captcha_api_key:
print("Warning: CAPTCHA_API_KEY not found in environment variables. Captcha solving will be disabled.")
self.solver = TwoCaptcha(self.captcha_api_key) if self.captcha_api_key else None
# Setup Selenium for image search (always headless)
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument("--log-level=1")
options.add_argument('--headless') # Always headless for image search
self.driver = webdriver.Chrome(options=options)
def search_image(self, image_bytes):
"""Perform reverse image search using Google Images."""
# print("Starting image search...")
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:
tmp_file.write(image_bytes)
tmp_path = tmp_file.name
try:
# Go to Google Images
self.driver.get('https://images.google.com')
# Click on camera icon
camera_button = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "[aria-label='Search by image']"))
)
camera_button.click()
# Upload image
file_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "input[type='file']"))
)
file_input.send_keys(tmp_path)
# Wait for results and click on "Exact matches" tab
try:
# print("Looking for 'Exact matches' tab...")
exact_matches_tab = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//div[text()='Exact matches']"))
)
# Add a small wait to let any overlays clear
time.sleep(1)
# Try regular click first
try:
WebDriverWait(self.driver, 5).until(
EC.element_to_be_clickable((By.XPATH, "//div[text()='Exact matches']"))
).click()
except Exception:
# If regular click fails, try JavaScript click
self.driver.execute_script("arguments[0].click();", exact_matches_tab)
# Wait a bit longer for results after successful click
time.sleep(2)
# Get all links and filter
all_links = self.driver.find_elements(By.TAG_NAME, "a")
urls = []
excluded_domains = ['google.co', 'gstatic.com', 'javascript:void(0)']
for link in all_links:
url = link.get_attribute('href')
if url and not any(domain in url.lower() for domain in excluded_domains):
urls.append(url)
# Get first 3 unique URLs
unique_urls = list(dict.fromkeys(urls))[:3] # Remove duplicates and get first 3
# print(f"Found {len(unique_urls)} unique external links: {unique_urls}")
return unique_urls
except TimeoutException as e:
print("Error occurred:", e)
return []
finally:
os.unlink(tmp_path)
def solve_captcha(self, url, driver):
"""
Solve captcha using 2captcha service.
Returns True if solved successfully, False otherwise.
"""
if not self.solver:
print("Captcha solver not initialized - missing API key")
return False
try:
# Wait for page to fully load
time.sleep(3)
# Handle reCAPTCHA v2
recaptcha_element = driver.find_elements(By.CLASS_NAME, "g-recaptcha")
if recaptcha_element:
print("Found reCAPTCHA v2")
site_key = recaptcha_element[0].get_attribute("data-sitekey")
result = self.solver.recaptcha(
sitekey=site_key,
url=url
)
# Wait for the response
time.sleep(2)
# Execute JavaScript to set the response
driver.execute_script(
'document.querySelector("[name=g-recaptcha-response]").innerHTML = "{}";'.format(result["code"])
)
# Trigger the grecaptcha callback if it exists
driver.execute_script(
'if (typeof ___grecaptcha_cfg !== "undefined") {'
' Object.entries(___grecaptcha_cfg.clients).forEach(([k, v]) => {'
' if (v["K"]) {'
' v["K"].callback("{}");'.format(result["code"]) +
' }'
' });'
'}'
)
# Find and click the form submit button
try:
submit_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit'], input[type='submit']"))
)
submit_button.click()
except:
print("No submit button found, continuing...")
return True
# Handle reCAPTCHA v3
recaptcha_v3 = driver.find_elements(By.XPATH, "//script[contains(@src, 'recaptcha/api.js?render=')]")
if recaptcha_v3:
print("Found reCAPTCHA v3")
script_src = recaptcha_v3[0].get_attribute("src")
site_key = script_src.split("render=")[1].split("&")[0] # Handle additional URL parameters
result = self.solver.recaptcha(
sitekey=site_key,
url=url,
version='v3',
action='submit'
)
# Wait for the response
time.sleep(2)
# Set the response token
driver.execute_script(
f'document.querySelector("[name=g-recaptcha-response]").innerHTML = "{result["code"]}";'
)
return True
# Handle hCaptcha
hcaptcha_element = driver.find_elements(By.CLASS_NAME, "h-captcha")
if hcaptcha_element:
print("Found hCaptcha")
site_key = hcaptcha_element[0].get_attribute("data-sitekey")
result = self.solver.hcaptcha(
sitekey=site_key,
url=url
)
# Wait for the response
time.sleep(2)
# Set the response token
driver.execute_script(
f'document.querySelector("[name=h-captcha-response]").innerHTML = "{result["code"]}";'
)
try:
submit_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit'], input[type='submit']"))
)
submit_button.click()
except:
print("No submit button found, continuing...")
return True
print(f"No supported captcha type found on {url}")
return False
except Exception as e:
print(f"Error solving captcha: {e}")
return False
def extract_text_from_url(self, url):
"""Extract main text content from a URL. Falls back to Selenium if direct request fails."""
# Try direct request first
try:
response = requests.get(url)
# print(url)
# print(response.status_code)
if response.status_code == 200:
downloaded = response.text
if downloaded:
return extract(downloaded)
except Exception as e:
print(f"Direct request failed: {e}")
# Fall back to Selenium if direct request fails
try:
# Create a new Chrome driver - headless unless show_browser is True
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
if not self.show_browser: # Only add headless if show_browser is False
options.add_argument('--headless')
options.add_argument("--log-level=1")
text_extraction_driver = webdriver.Chrome(options=options)
try:
text_extraction_driver.get(url)
safe_filename = url.replace('://', '_').replace('/', '_')[:100]
screenshot_path = f"C:\\Users\\hp\\copyright_handling\\error_ss\\{safe_filename}_{response.status_code}.png"
text_extraction_driver.save_screenshot(screenshot_path)
print(f"Screenshot saved as {screenshot_path}")
# Check for captchas and solve if needed
captcha_selectors = [
"//iframe[contains(@src, 'recaptcha')]",
"//div[contains(@class, 'captcha')]",
"//form[contains(@action, 'captcha')]"
]
for selector in captcha_selectors:
try:
captcha_element = WebDriverWait(text_extraction_driver, 3).until(
EC.presence_of_element_located((By.XPATH, selector))
)
if captcha_element:
solved = self.solve_captcha(url)
print("solving captcha")
if not solved:
print(f"Captcha solving failed for {url}")
return ""
time.sleep(2)
break
except TimeoutException:
continue
# Wait for body content to load
wait = WebDriverWait(text_extraction_driver, 10)
content = wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
page_text = content.text
if page_text:
return page_text
finally:
text_extraction_driver.quit()
except Exception as e:
print(f"Selenium extraction failed: {e}")
return ""
def generate_citation(self, image_bytes, extracted_texts):
"""Generate citation recommendation using Claude."""
prompt = """Based on the following extracted texts from potential source websites of an image,
generate an appropriate citation or attribution text for the image. You must strictly follow one of these formats:
1. © [Organization]. "[Paper/Article Title]" by [Author]. All rights reserved. This content is excluded from our Creative Commons license.
2. Images courtesy of [Organization] from "[Paper/Article Title]" by [Author]. This image is in the public domain.
3. Image courtesy of [Organization] on [Platform], from "[Paper/Article Title]" by [Author]. Used under [License].
4. Source: [Organization], "[Paper/Article Title]" by [Author]. This image is in the public domain.
5. Courtesy of [Author] on [Platform], "[Paper/Article Title]". Used with permission.
Choose the most appropriate format and fill in all available information. If certain information (like paper title or author)
is not available, you may omit those parts while keeping the basic structure intact.
If the provided texts do not contain relevant information about the image source, respond with: "Unable to determine image source information."
Extracted texts:
{texts}
Respond only with the formatted citation or the unable to determine message, nothing else."""
response = self.client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=300,
messages=[{
"role": "user",
"content": prompt.format(texts="\n\n".join(extracted_texts))
}]
)
return response.content[0].text.strip()
def get_citation_for_image(self, image_bytes):
"""Main method to get citation for an image."""
try:
# Search image on Google
source_urls = self.search_image(image_bytes)
# Extract text from each URL
extracted_texts = []
for url in source_urls:
text = self.extract_text_from_url(url)
if text:
extracted_texts.append(text)
# Generate citation using Claude
if extracted_texts:
return self.generate_citation(image_bytes, extracted_texts)
return "Source information could not be determined."
except Exception as e:
return f"Error finding citation: {str(e)}"
def __del__(self):
"""Cleanup Selenium driver."""
if hasattr(self, 'driver'):
self.driver.quit()
def get_image_citation(pdf_image_data, show_browser=False): # Changed parameter name
finder = ImageCitationFinder(show_browser=show_browser) # Changed parameter name
urls = finder.search_image(pdf_image_data)
# Extract text from each URL
extracted_texts = []
for url in urls:
text = finder.extract_text_from_url(url)
if text:
extracted_texts.append(text)
citation = finder.generate_citation(pdf_image_data, extracted_texts) if extracted_texts else "Source information could not be determined."
return {
'citation': citation,
'source_urls': urls
}