-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
305 lines (222 loc) · 7.87 KB
/
main.py
File metadata and controls
305 lines (222 loc) · 7.87 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
# ---
# lambda-test: false
# ---
from fastapi.encoders import jsonable_encoder
from typing import Optional
from fastapi import FastAPI, Header
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import modal
web_app = FastAPI()
origins = ["*"]
web_app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
image = (
modal.Image.debian_slim()
.apt_install("curl")
.run_commands(
"apt-get update",
"curl -O https://raw.githubusercontent.com/TruthQuestWeb/ml-model/main/train.csv",
)
.pip_install(
"bs4", "youdotcom", "requests", "openai", "pandas", "scikit-learn", "numpy"
)
)
# Define the filename and path to save the pickled classifier
filename = "classifier.pickle"
volume = modal.SharedVolume().persist("model-cache-vol")
stub = modal.Stub("modal-serverless-api", image=image)
# 1 day duration
CACHE_DURATION = 86400
CACHE_DIR = "/cache"
class URL(BaseModel):
url_link: str
@stub.function
def extract_source(url):
start = url.find("www.") + 4
end = url.find(".com")
source = url[start:end]
return source
@stub.function
def search_youdotcom(url):
import requests
import os
import json
from bs4 import BeautifulSoup
import time
from youdotcom import Search
article_response = requests.get(url, timeout=10)
soup = BeautifulSoup(article_response.text, "html.parser")
article = soup.get_text(" ", strip=True)
title = soup.find("title").text
search_results = Search.search_for(title)
parsed = json.loads(search_results["results"])
print(json.dumps(parsed, indent=4))
@stub.function(secret=modal.Secret.from_name("modal_serverless_api_secrets"))
def summarizer(url):
import os
import openai
import requests
import json
from bs4 import BeautifulSoup
import time
API_KEY = os.environ["GOOGLE_API_KEY"]
ENGINE_ID = os.environ["ENGINE_ID"]
# Get the article text and title from the URL
article_response = requests.get(url)
soup = BeautifulSoup(article_response.text, "html.parser")
article = soup.get_text(" ", strip=True)
title = soup.find("title").text
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Completion.create(
model="text-davinci-003",
prompt=article + "\n\n Remove opinion and summarize the article.",
temperature=0.36,
max_tokens=200,
top_p=1,
frequency_penalty=0,
presence_penalty=1,
)
print(response)
return jsonable_encoder(response)
@stub.function(memory=4048)
def foo(articles):
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn import metrics
import pickle
import pandas as pd
import time
df = pd.read_csv("/train.csv")
import numpy as np
# Check if the pickled file exists and if it has not expired
try:
with open(filename, "rb") as f:
classifier, cache_time = pickle.load(f)
if time.time() - cache_time < CACHE_DURATION:
df = pd.DataFrame({"text": [articles]})
input = count_vectorizer.transform(df)
# Make predictions and return the results
pred = nb_classifier.predict(input)
if pred == 1:
return jsonable_encoder({"result": "true"})
else:
return jsonable_encoder({"result": "false"})
except FileNotFoundError:
pass
df["text"].replace("", np.nan, inplace=True)
df.dropna(subset=["text"], inplace=True)
X_train, X_test, y_train, y_test = train_test_split(
df.text, df.label, test_size=0.2
)
count_vectorizer = CountVectorizer(stop_words="english")
count_train = count_vectorizer.fit_transform(X_train)
count_test = count_vectorizer.transform(X_test)
nb_classifier = MultinomialNB()
nb_classifier.fit(count_train, y_train)
with open(filename, "wb") as f:
pickle.dump((nb_classifier, time.time()), f)
df = pd.DataFrame({"text": [articles]})
input = count_vectorizer.transform(df)
pred = nb_classifier.predict(input)
if pred == 1:
return jsonable_encoder({"result": "true"})
else:
return jsonable_encoder({"result": "false"})
class Article(BaseModel):
text: str
@stub.function(memory=4048, cpu=4.0)
def confidence(articles):
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
import pandas as pd
df = pd.read_csv('/train.csv')
import numpy as np
df[['title', 'author']].replace('', np.nan, inplace=True)
df.dropna(subset=['title'], inplace=True)
df.dropna(subset=['author'], inplace=True)
df['content'] = df['author'] + ' ' + df['title']
X_train, X_test, y_train, y_test = train_test_split(df.content, df.label, test_size=.2)
count_vectorizer = CountVectorizer(stop_words='english')
count_train = count_vectorizer.fit_transform(X_train)
nb_classifier = MultinomialNB()
nb_classifier.fit(count_train, y_train)
df = pd.DataFrame([articles ])
input = count_vectorizer.transform(df)
pred = nb_classifier.predict_proba(input)
return jsonable_encoder({'true': pred[0][1], 'false': pred[0][0]})
@web_app.post("/analysis/")
async def analysis(articleobj: Article):
return confidence.call(articleobj.text)
@stub.function(
secret=modal.Secret.from_name("modal_serverless_api_secrets"), memory=12288, cpu=6.0
)
def search_initial_article(url):
import requests
import os
import json
from bs4 import BeautifulSoup
import time
API_KEY = os.environ["GOOGLE_API_KEY"]
ENGINE_ID = os.environ["ENGINE_ID"]
# Get the article text and title from the URL
article_response = requests.get(url)
soup = BeautifulSoup(article_response.text, "html.parser")
article = soup.get_text(" ", strip=True)
# Get the author of the article
author = soup.find("meta", property="article:author")
title = soup.find("title").text
# Search the web for similar articles using the article title
response = requests.get(
"https://www.googleapis.com/customsearch/v1?q="
+ title
+ "&cx="
+ ENGINE_ID
+ "&key="
+ API_KEY
)
print(response.json())
results = response.json()["items"]
comparison_articles = {}
comparison_articles["title"] = title
comparison_articles["author"] = author
#result = foo.call(comparison_articles["text"])
confi = confidence.call(comparison_articles)
#merged_data = {**result, **confi}
return jsonable_encoder(confi)
"""
sources = []
sources.append(extract_source.call(url))
for result in results[:5]:
if extract_source.call(result["link"]) not in sources:
sources.append(extract_source.call(result["link"]))
article_response = requests.get(result["link"])
soup = BeautifulSoup(article_response.text, "html.parser")
article = soup.get_text(" ", strip=True)
comparison_articles[result["link"]] = article
print("Comparison article:", result["link"])
print(json.dumps(comparison_articles, indent=4))
"""
@web_app.post("/search/")
async def search(url: URL):
art = search_initial_article.call(url.url_link)
return jsonable_encoder(art)
@web_app.post("/summarize/")
async def summarize(url: URL):
return summarizer.call(url.url_link)
# @web_app.post("/video/")
# async def video(url: URL):
# return search_youdotcom.call(url.url_link)
@stub.asgi(image=image)
def fastapi_app():
return web_app
if __name__ == "__main__":
stub.deploy("webapp")