File size: 2,408 Bytes
8098e43 1bab590 12bd11c 8098e43 12bd11c 1bab590 5ea8fbd b646cf8 5ea8fbd 1bab590 b646cf8 1bab590 b646cf8 5ea8fbd b646cf8 1bab590 b646cf8 1bab590 b646cf8 1bab590 12bd11c 1bab590 12bd11c 1bab590 | 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 | 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) |