-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
171 lines (143 loc) · 5.12 KB
/
main.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
163
164
165
166
167
168
169
170
171
import concurrent.futures
from text_factagent_tools import *
from text_source_tracer import *
from text_fact_checker import *
from aging import diff_finder
from util import download_image, download_youtube_video
from image_deepfake_detection import predict
from image_reverse_search import reverse_image_search
from video_deepfake_detector import video_deepfake_detector
from video_reverse_search import video_reverse_search
from media_description_tool import process_video_and_generate_content
import os
from article_parser import Article
import re
def pipeline(url, image: bool, video: bool):
results = dict()
article = Article(url)
text = re.sub("\s+", " ", article.text).strip()
results["text"] = text_pipeline(article.title, text, url)
print(article.images)
if image == True and article.images != []:
try:
results["images"] = image_pipeline(article.images[:5], url)[0]
except:
pass
if video == True and article.videos != []:
try:
results["videos"] = image_pipeline(article.videos[:1], url)[0]
except:
pass
print("Text:----------------", results["text"], "\n\n\n")
if "images" in results:
print("Images:----------------", results["images"], "\n\n\n")
if "videos" in results:
print("Videos:----------------", results["videos"], "\n\n\n")
# return 0.69
# have to change code in cache to accomodate this
return results
def text_pipeline(title, text, article_url):
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as exe:
p = exe.submit(phrase_tool, title, text)
l = exe.submit(language_tool, title, text)
c = exe.submit(commonsense_tool, title, text)
s = exe.submit(standing_tool, title, text)
urls = exe.submit(revtextsearch, text)
t = exe.submit(fact_check, title)
ages = []
# if urls.result() is not []:
for url in urls.result():
try:
ages.append(diff_finder(url, article_url))
except:
continue
return {
"phrase_tool": p.result(),
"language_tool": l.result(),
"commonsense_tool": c.result(),
"standing_tool": s.result(),
"reverse_search_ages": ages,
"fact_check": t.result()
}
def image_pipeline(urls, article_url):
article_urls = [article_url for _ in urls]
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as exe:
result = exe.map(process_image, urls, article_urls)
result = list(result)
for i in range(len(result)):
if result[i] is not None:
result[i]["text_processing"] = text_pipeline(result[i]["description"], result[i]["description"], article_url)
result = [i for i in result if i is not None]
return result
def process_image(url, article_url):
try:
path, img_data = download_image(url)
except Exception as e:
print(e)
return None
predictions, _, _ = predict(img_data, "real")
if predictions is None:
print("predictions NONEEEE")
return None
try:
description = process_video_and_generate_content(path, "image")
print(description)
except Exception as e:
print(e)
return None
try:
ages = reverse_image_search(path, article_url)
except Exception as e:
print(e)
return None
os.remove(path)
return {
"prediction": predictions["fake"],
"description": description,
"ages": ages
}
def video_pipeline(urls, article_url):
# print("article_url", article_url)
article_urls = [article_url for _ in urls]
print("processing video...")
print(urls)
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as exe:
result = exe.map(process_video, urls, article_urls)
result = list(result)
# print(result)
for i in range(len(result)):
if result is not None:
result[i]["text_processing"] = text_pipeline(result[i]["description"], result[i]["description"], article_url)
result = [i for i in result if i is not None]
return result
def process_video(url, article_url):
print("article_url", article_url)
try:
path = download_youtube_video(url)
except Exception as e:
print(e)
return None
try:
predictions = video_deepfake_detector(path)
description = process_video_and_generate_content(path, "video")
print(description)
ages = video_reverse_search(path, article_url)
except Exception as e:
print(e)
return None
os.remove(path)
return {
"predictions": predictions["fake"],
"description": description,
"ages": ages
}
if __name__ == "__main__":
url = input("Enter a url: ")
article = Article(url)
# text_result = text_pipeline(article.title, article.text, url)
image_result = image_pipeline(article.images[:4], url)
# video_result = video_pipeline(article.videos, url)
# print(f"Text result:\n{text_result}\n\n\n")
print(f"Image result:\n{image_result}\n\n\n")
print(article.images[:4])
# print(f"Video result:\n{video_result}\n\n\n")