Spaces:
Runtime error
Runtime error
Commit Β·
1b2fdf3
1
Parent(s): 5dd965b
Updates
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import List
|
| 4 |
from transformers import pipeline
|
|
@@ -17,18 +18,65 @@ class InputText(BaseModel):
|
|
| 17 |
class InputBatch(BaseModel):
|
| 18 |
texts: List[str]
|
| 19 |
|
| 20 |
-
@app.get("/")
|
| 21 |
def root():
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
@app.get("/v1/health")
|
| 25 |
def health_check():
|
| 26 |
-
"""Lightweight health check endpoint"""
|
| 27 |
return {"status": "ok", "service": "Sentimind API", "version": "v1"}
|
| 28 |
|
| 29 |
@app.post("/v1/analyze/sentiment/single")
|
| 30 |
def analyze_single(input_data: InputText):
|
| 31 |
-
"""Analyze sentiment of a single text"""
|
| 32 |
result = sentiment_analyzer(input_data.text)[0]
|
| 33 |
return {
|
| 34 |
"input_text": input_data.text,
|
|
@@ -38,7 +86,6 @@ def analyze_single(input_data: InputText):
|
|
| 38 |
|
| 39 |
@app.post("/v1/analyze/sentiment/batch")
|
| 40 |
def analyze_batch(input_data: InputBatch):
|
| 41 |
-
"""Analyze sentiment of multiple texts and provide overall summary"""
|
| 42 |
results = []
|
| 43 |
positive_count = 0
|
| 44 |
negative_count = 0
|
|
@@ -52,7 +99,6 @@ def analyze_batch(input_data: InputBatch):
|
|
| 52 |
"sentiment": sentiment,
|
| 53 |
"confidence": confidence
|
| 54 |
})
|
| 55 |
-
|
| 56 |
if sentiment == "positive":
|
| 57 |
positive_count += 1
|
| 58 |
else:
|
|
@@ -61,7 +107,6 @@ def analyze_batch(input_data: InputBatch):
|
|
| 61 |
total = len(input_data.texts)
|
| 62 |
positive_ratio = positive_count / total if total > 0 else 0
|
| 63 |
negative_ratio = negative_count / total if total > 0 else 0
|
| 64 |
-
|
| 65 |
overall_sentiment = "positive" if positive_count >= negative_count else "negative"
|
| 66 |
|
| 67 |
return {
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import List
|
| 5 |
from transformers import pipeline
|
|
|
|
| 18 |
class InputBatch(BaseModel):
|
| 19 |
texts: List[str]
|
| 20 |
|
| 21 |
+
@app.get("/", response_class=HTMLResponse)
|
| 22 |
def root():
|
| 23 |
+
"""Landing page with service info"""
|
| 24 |
+
return """
|
| 25 |
+
<html>
|
| 26 |
+
<head>
|
| 27 |
+
<title>Sentimind API</title>
|
| 28 |
+
<style>
|
| 29 |
+
body { font-family: Arial, sans-serif; margin: 40px; background: #fafafa; }
|
| 30 |
+
h1 { color: #333; }
|
| 31 |
+
.section { margin-top: 20px; }
|
| 32 |
+
code { background: #eee; padding: 2px 5px; border-radius: 4px; }
|
| 33 |
+
</style>
|
| 34 |
+
</head>
|
| 35 |
+
<body>
|
| 36 |
+
<h1>π Sentimind API</h1>
|
| 37 |
+
<p>Welcome to <b>Sentimind Labs</b> sentiment analysis API.</p>
|
| 38 |
+
|
| 39 |
+
<div class="section">
|
| 40 |
+
<h2>Available Endpoints</h2>
|
| 41 |
+
<ul>
|
| 42 |
+
<li><code>GET /v1/health</code> β Health check</li>
|
| 43 |
+
<li><code>POST /v1/analyze/sentiment/single</code> β Analyze a single text</li>
|
| 44 |
+
<li><code>POST /v1/analyze/sentiment/batch</code> β Analyze multiple texts and return overall sentiment</li>
|
| 45 |
+
</ul>
|
| 46 |
+
</div>
|
| 47 |
+
|
| 48 |
+
<div class="section">
|
| 49 |
+
<h2>Example Usage</h2>
|
| 50 |
+
<p><b>Single analysis:</b></p>
|
| 51 |
+
<pre>
|
| 52 |
+
POST /v1/analyze/sentiment/single
|
| 53 |
+
{
|
| 54 |
+
"text": "I love this service!"
|
| 55 |
+
}
|
| 56 |
+
</pre>
|
| 57 |
+
|
| 58 |
+
<p><b>Batch analysis:</b></p>
|
| 59 |
+
<pre>
|
| 60 |
+
POST /v1/analyze/sentiment/batch
|
| 61 |
+
{
|
| 62 |
+
"texts": ["I love this!", "This is bad."]
|
| 63 |
+
}
|
| 64 |
+
</pre>
|
| 65 |
+
</div>
|
| 66 |
+
|
| 67 |
+
<div class="section">
|
| 68 |
+
<p><i>Powered by DistilBERT, hosted on Sentimind Labs β¨</i></p>
|
| 69 |
+
</div>
|
| 70 |
+
</body>
|
| 71 |
+
</html>
|
| 72 |
+
"""
|
| 73 |
|
| 74 |
@app.get("/v1/health")
|
| 75 |
def health_check():
|
|
|
|
| 76 |
return {"status": "ok", "service": "Sentimind API", "version": "v1"}
|
| 77 |
|
| 78 |
@app.post("/v1/analyze/sentiment/single")
|
| 79 |
def analyze_single(input_data: InputText):
|
|
|
|
| 80 |
result = sentiment_analyzer(input_data.text)[0]
|
| 81 |
return {
|
| 82 |
"input_text": input_data.text,
|
|
|
|
| 86 |
|
| 87 |
@app.post("/v1/analyze/sentiment/batch")
|
| 88 |
def analyze_batch(input_data: InputBatch):
|
|
|
|
| 89 |
results = []
|
| 90 |
positive_count = 0
|
| 91 |
negative_count = 0
|
|
|
|
| 99 |
"sentiment": sentiment,
|
| 100 |
"confidence": confidence
|
| 101 |
})
|
|
|
|
| 102 |
if sentiment == "positive":
|
| 103 |
positive_count += 1
|
| 104 |
else:
|
|
|
|
| 107 |
total = len(input_data.texts)
|
| 108 |
positive_ratio = positive_count / total if total > 0 else 0
|
| 109 |
negative_ratio = negative_count / total if total > 0 else 0
|
|
|
|
| 110 |
overall_sentiment = "positive" if positive_count >= negative_count else "negative"
|
| 111 |
|
| 112 |
return {
|