-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
162 lines (122 loc) · 4.93 KB
/
api.py
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
import re
import io
import sys
import json
import asyncio
import argparse
import requests
import nodriver as uc
from fuzzywuzzy import fuzz
from bs4 import BeautifulSoup
from urllib.parse import quote
from difflib import SequenceMatcher
from urllib.parse import quote_plus
# fuzzywuzzy
def check_similarity_fuzzy(assignment_text, search_results):
highest_similarity = 0
most_similar_result = ""
for result in search_results:
similarity_ratio = fuzz.ratio(assignment_text, result['content'])
if similarity_ratio > highest_similarity:
highest_similarity = similarity_ratio
most_similar_result = result['url']
if highest_similarity > 30:
return str(highest_similarity) + "%" + most_similar_result
else:
return "0"
def extract_google_search_results(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
search_results = []
for result in soup.find_all('article', class_='result'):
title = result.find('h3').get_text(strip=True)
url = result.find('a', href=True)['href']
content = result.find('p', class_='content').get_text()
search_result = {
'title': title,
'url': url,
'content': content
}
search_results.append(search_result)
return search_results
async def GoogleSearch(query):
driver = await uc.start(headless=True)
url = 'https://searxng.site/searxng/search?q=' + quote(query) + '&language=auto&time_range=&safesearch=2&categories=general'
tab = await driver.get(url)
html = await tab.get_content()
# Close the browser
await tab.close()
return html
async def ZeroGPTSearch(query):
driver = await uc.start(headless=True)
tab = await driver.get("https://www.zerogpt.com/fr/")
input_text = query
textarea = await tab.select("textarea#textArea")
await textarea.click()
textarea = await tab.select("textarea#textArea")
await textarea.click()
textarea = await tab.select("textarea#textArea")
await textarea.click()
await tab.sleep(5)
textarea = await tab.select("textarea#textArea")
await textarea.click()
await textarea.send_keys(input_text)
detect_button = await tab.select("button.scoreButton")
await detect_button.click()
try:
result_elements = await tab.select("div.final-result")
# Close the browser
await tab.close()
return result_elements
except Exception as e:
print("Erreur:", e)
if __name__ == "__main__":
debug_mode = False
resultIA = 0
google_result = ""
if debug_mode:
input_text = "Le langage est capable de dire aussi bien ce qui existe que ce qui n'existe pas : il me permet aussi bien de décrire un événement qui s'est réellement produit"
else:
parser = argparse.ArgumentParser(description="Récupéré le pourcentage de similarité et d'I.A.")
parser.add_argument("input", type=str, help="Le texte à vérifier")
args = parser.parse_args()
input_text = args.input
#print("Bonjour ! Vérification de l'authenticité de la composition suivante :")
#print("```")
#print(input_text)
#print("```")
#print("Cherchons sur Google tout d'abord...")
# GOOGLE SERP
html_content = asyncio.run(GoogleSearch(input_text))
assignment_text = input_text
search_results = extract_google_search_results(html_content)
google_results = check_similarity_fuzzy(assignment_text, search_results)
if(google_results != "0"):
google_score = google_results.split("%")[0] + "%"
google_result = google_results.split("%")[1]
#print(f"[!] Attention : contenu très similaire ({google_score}) trouvé sur Google")
#print(google_result)
else:
#print("Rien de similaire sur Google :-) !")
#print("Vérifions désormais via ZeroGPT si ce n'est pas un texte généré par I.A. :")
# ZEROGPT
old_stdout = sys.stdout # Impossible pour moi de trouver une autre façon de récupérer le contenu Async de tab.Elements ! Une vraie galère
sys.stdout = buffer = io.StringIO()
result_texts = asyncio.run(ZeroGPTSearch(input_text))
rint(result_texts)
sys.stdout = old_stdout
resultIA = buffer.getvalue()
resultIA = resultIA.split("highlights-container")[0]
resultIA = resultIA.split("header-text text-center")[2]
resultIA = resultIA.split("%")[0]
resultIA = resultIA.split('">')[1]
#print(resultIA + "% de chance que ce texte soit produit par un GPT !")
results_dic = {"google": google_result, "IA": resultIA}
# Convert dictionary to JSON string
json_string = json.dumps(results_dic)
# Print the JSON string
print(json_string)
# Define the file path
file_path = "result.json"
# Write the JSON string to the file
with open(file_path, "w") as json_file:
json_file.write(json_string)