-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodalTest.py
More file actions
59 lines (46 loc) · 1.59 KB
/
modalTest.py
File metadata and controls
59 lines (46 loc) · 1.59 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
import modal
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.encoders import jsonable_encoder
web_app = FastAPI()
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(
"pandas",
"scikit-learn",
"numpy",
"requests"
))
stub = modal.Stub(image=image)
class Article(BaseModel):
text: str
@stub.function()
def foo(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['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=.2)
count_vectorizer = CountVectorizer(stop_words='english')
count_train = count_vectorizer.fit_transform(X_train)
nb_classifier = MultinomialNB(probability=True)
nb_classifier.fit(count_train, y_train)
df = pd.DataFrame({'text': [articles]})
input = count_vectorizer.transform(df)
pred = nb_classifier.predict_proba(input)
return jsonable_encoder({'true': pred[1], 'false': pred[0]})
@web_app.post("/analysis/")
async def analysis(articleobj: Article):
return foo.call(articleobj.text)
@stub.asgi(image=image)
def fastapi_App():
return web_app
if __name__ == "__main__":
stub.deploy("webapp")