Jiahuita
commited on
Commit
·
936247f
1
Parent(s):
54c3b7f
Minor Modifications
Browse files- __pycache__/pipeline.cpython-39.pyc +0 -0
- app.py +0 -57
- pipeline.py +0 -1
__pycache__/pipeline.cpython-39.pyc
ADDED
|
Binary file (1.81 kB). View file
|
|
|
app.py
DELETED
|
@@ -1,57 +0,0 @@
|
|
| 1 |
-
import tensorflow as tf
|
| 2 |
-
import numpy as np
|
| 3 |
-
from tensorflow.keras.models import load_model
|
| 4 |
-
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
| 5 |
-
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 6 |
-
import json
|
| 7 |
-
import pickle
|
| 8 |
-
from fastapi import FastAPI, HTTPException
|
| 9 |
-
from pydantic import BaseModel
|
| 10 |
-
|
| 11 |
-
app = FastAPI(title="News Source Classifier")
|
| 12 |
-
|
| 13 |
-
try:
|
| 14 |
-
model = load_model('news_classifier.h5')
|
| 15 |
-
|
| 16 |
-
with open('tokenizer.json') as f:
|
| 17 |
-
tokenizer_data = json.load(f)
|
| 18 |
-
tokenizer = tokenizer_from_json(tokenizer_data)
|
| 19 |
-
|
| 20 |
-
with open('vectorizer.pkl', 'rb') as f:
|
| 21 |
-
vectorizer = pickle.load(f)
|
| 22 |
-
except Exception as e:
|
| 23 |
-
print(f"Error loading model: {str(e)}")
|
| 24 |
-
raise
|
| 25 |
-
|
| 26 |
-
class PredictionRequest(BaseModel):
|
| 27 |
-
text: str
|
| 28 |
-
|
| 29 |
-
class PredictionResponse(BaseModel):
|
| 30 |
-
source: str
|
| 31 |
-
confidence: float
|
| 32 |
-
|
| 33 |
-
@app.post("/predict", response_model=PredictionResponse)
|
| 34 |
-
async def predict(request: PredictionRequest):
|
| 35 |
-
try:
|
| 36 |
-
sequence = tokenizer.texts_to_sequences([request.text])
|
| 37 |
-
padded = pad_sequences(sequence, maxlen=100)
|
| 38 |
-
|
| 39 |
-
prediction = model.predict(padded)
|
| 40 |
-
confidence = float(np.max(prediction))
|
| 41 |
-
|
| 42 |
-
predicted_class = int(np.argmax(prediction))
|
| 43 |
-
source = 'foxnews' if predicted_class == 0 else 'nbc'
|
| 44 |
-
|
| 45 |
-
return PredictionResponse(
|
| 46 |
-
source=source,
|
| 47 |
-
confidence=confidence
|
| 48 |
-
)
|
| 49 |
-
except Exception as e:
|
| 50 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 51 |
-
|
| 52 |
-
@app.get("/")
|
| 53 |
-
async def root():
|
| 54 |
-
return {
|
| 55 |
-
"message": "News Source Classifier API",
|
| 56 |
-
"usage": "Make a POST request to /predict with a JSON payload containing 'text' field"
|
| 57 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pipeline.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# pipeline.py
|
| 2 |
from transformers import Pipeline
|
| 3 |
import tensorflow as tf
|
| 4 |
from tensorflow.keras.models import load_model
|
|
|
|
|
|
|
| 1 |
from transformers import Pipeline
|
| 2 |
import tensorflow as tf
|
| 3 |
from tensorflow.keras.models import load_model
|