Skip to content

Commit

Permalink
addedApp
Browse files Browse the repository at this point in the history
  • Loading branch information
17297781Karthik committed Oct 21, 2024
1 parent c01e757 commit b360d54
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: Docker Image CIon: push: branches: [ "main" ] pull_request: branches: [ "main" ]jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Build the Docker image run: docker build . --file Dockerfile --tag my-image-name:$(date +%s)
Expand Down
77 changes: 69 additions & 8 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Use an official Python runtime as a base imageFROM python:3.11-slim# Set the working directory in the containerWORKDIR /app# Copy the requirements file into the containerCOPY requirements.txt .# Install dependenciesRUN pip install -r requirements.txt# Copy the current directory contents into the containerCOPY . .# Expose the port the app runs onEXPOSE 8080# Run the appCMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
Expand Down
1 change: 1 addition & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import joblibimport refrom fastapi import FastAPI, Request, Formfrom fastapi.responses import HTMLResponsefrom fastapi.staticfiles import StaticFilesfrom fastapi.templating import Jinja2Templatesfrom nltk.corpus import stopwordsfrom nltk.stem.porter import PorterStemmerfrom pydantic import BaseModel# Load the model and vectorizer from the .pkl filesmodel = joblib.load(r'models/logistic_regression_model.pkl')vectorizer = joblib.load(r'models/tfidf_vectorizer.pkl')# Initialize FastAPI appapp = FastAPI()# Initialize the stemmerport_stem = PorterStemmer()# Mount the static directory for CSS filesapp.mount("static", StaticFiles(directory="static"), name="static")# Initialize templates directory for rendering HTMLtemplates = Jinja2Templates(directory="templates")def preprocess_text(text): """Preprocess the input text by stemming and removing stopwords.""" stemmed_content = re.sub('[^a-zA-Z]', ' ', text) stemmed_content = stemmed_content.lower() stemmed_content = stemmed_content.split() stemmed_content = [port_stem.stem(word) for word in stemmed_content if not word in stopwords.words('english')] stemmed_content = ' '.join(stemmed_content) return stemmed_content# Define the request body schema using Pydanticclass NewsInput(BaseModel): title: str paragraph: str# Root endpoint to render the form@app.get("/", response_class=HTMLResponse)async def read_form(request: Request): return templates.TemplateResponse("index.html", {"request": request})# Prediction route@app.post("/predict")async def predict(input_data: NewsInput): title = input_data.title paragraph = input_data.paragraph # Combine title and paragraph content = title + ' ' + paragraph # Preprocess the text preprocessed_text = preprocess_text(content) # Transform the text using the vectorizer transformed_text = vectorizer.transform([preprocessed_text]) # Make a prediction using the model prediction = model.predict(transformed_text) # Return prediction as a response return {"prediction": "Fake News" if prediction[0] == 1 else "Real News"}if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8080)
Expand Down
Binary file added models/logistic_regression_model.pkl
Binary file not shown.
Binary file added models/tfidf_vectorizer.pkl
Binary file not shown.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fastapi==0.115.2uvicorn==0.32.0joblib==1.4.2nltk==3.9.1pandas==1.1.5scikit-learn==0.24.2numpy==1.19.5rematplotlib==3.4.3seaborn==0.11.2pydantic==1.8.2
Expand Down
1 change: 1 addition & 0 deletions static/styles.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Fake News Prediction</title> <link rel="stylesheet" href="/styles.css"></head><body> <div class="container"> <h1>Fake News Prediction</h1> <form id="newsForm" action="/predict" method="post"> <label for="title">Title:</label><br> <input type="text" id="title" name="title" required><br> <label for="paragraph">Paragraph:</label><br> <textarea id="paragraph" name="paragraph" rows="5" required></textarea><br> <button type="submit">Predict</button> </form> <div id="result"></div> </div> <script> const form = document.getElementById('newsForm'); form.onsubmit = async (event) => { event.preventDefault(); const formData = new FormData(form); const response = await fetch('/predict', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: formData.get('title'), paragraph: formData.get('paragraph') }) }); const result = await response.json(); document.getElementById('result').innerHTML = `<p>Prediction: ${result.prediction}</p>`; }; </script></body></html>
Expand Down

0 comments on commit b360d54

Please sign in to comment.