| from fastapi import FastAPI |
| from fastapi.responses import HTMLResponse |
| from transformers import pipeline |
|
|
| app = FastAPI() |
| classifier = pipeline("sentiment-analysis") |
|
|
| html = """ |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>Sentiment Checker</title> |
| <style> |
| body { |
| margin: 0; |
| height: 100vh; |
| display: flex; |
| flex-direction: column; |
| align-items: center; |
| justify-content: center; |
| |
| background: linear-gradient(135deg, #ff9ecb, #ffcce6, #ffe6f2); |
| font-family: Arial, sans-serif; |
| } |
| |
| h2 { |
| font-size: 48px; |
| margin-bottom: 30px; |
| color: white; |
| } |
| |
| .search-box { |
| display: flex; |
| align-items: center; |
| background: white; |
| border-radius: 50px; |
| padding: 10px 15px; |
| width: min(500px, 90vw); |
| box-shadow: 0 4px 20px rgba(0,0,0,0.1); |
| } |
| |
| .search-box input { |
| border: none; |
| outline: none; |
| flex: 1; |
| font-size: 16px; |
| padding: 10px; |
| } |
| |
| .search-box button { |
| border: none; |
| background: #ff69b4; |
| color: white; |
| padding: 10px 18px; |
| border-radius: 25px; |
| cursor: pointer; |
| font-weight: bold; |
| } |
| |
| .search-box button:hover { |
| background: #ff4fa3; |
| } |
| |
| #result { |
| margin-top: 20px; |
| font-size: 22px; |
| color: white; |
| text-align: center; |
| } |
| </style> |
| </head> |
| |
| <body> |
| <h2>Sentiment Checker</h2> |
| |
| <div class="search-box"> |
| <input id="text" placeholder="Type something..." /> |
| <button onclick="send()">Check</button> |
| </div> |
| |
| <p id="result"></p> |
| |
| <script> |
| async function send() { |
| const text = document.getElementById("text").value; |
| const res = await fetch(`/predict?text=${encodeURIComponent(text)}`); |
| const data = await res.json(); |
| |
| document.getElementById("result").innerText = |
| data[0].label + " (" + data[0].score.toFixed(2) + ")"; |
| } |
| </script> |
| </body> |
| </html> |
| """ |
|
|
| @app.get("/", response_class=HTMLResponse) |
| def home(): |
| return html |
|
|
| @app.get("/predict") |
| def predict(text: str): |
| return classifier(text) |