Spaces:
Runtime error
Runtime error
File size: 4,041 Bytes
6e0814e 1b2fdf3 e72395f 5dd965b 5f16f9a 6e0814e e72395f 5f16f9a 5dd965b 5f16f9a e72395f 6e0814e 5dd965b 1b2fdf3 e72395f 0caf469 1b2fdf3 0caf469 1b2fdf3 0caf469 1b2fdf3 0caf469 1b2fdf3 0caf469 1b2fdf3 0caf469 1b2fdf3 0caf469 1b2fdf3 e72395f 5dd965b e72395f 5f16f9a e72395f 5dd965b | 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from typing import List
from transformers import pipeline
app = FastAPI(title="Sentimind API")
# Load sentiment analysis model
sentiment_analyzer = pipeline(
"sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english"
)
class InputText(BaseModel):
text: str
class InputBatch(BaseModel):
texts: List[str]
@app.get("/", response_class=HTMLResponse)
def root():
"""Landing page with branding + API info"""
return """
<html>
<head>
<title>Sentimind API</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; background: #f8fafc; color: #222; }
h1 { color: #1a202c; }
.section { margin-top: 25px; }
code, pre { background: #edf2f7; padding: 4px 6px; border-radius: 4px; }
.logo { width: 80px; }
.footer { margin-top: 40px; font-size: 0.9em; color: #555; }
</style>
</head>
<body>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f7/Brain_icon.svg/1024px-Brain_icon.svg.png"
alt="AI Brain Logo" class="logo"/>
<h1>🧠 Sentimind API</h1>
<p>Welcome to <b>Sentimind Labs</b> sentiment analysis service for financial insights.</p>
<div class="section">
<h2>Available Endpoints</h2>
<ul>
<li><code>GET /v1/health</code> — Health check</li>
<li><code>POST /v1/analyze/sentiment/single</code> — Analyze sentiment of a single stock-related statement</li>
<li><code>POST /v1/analyze/sentiment/batch</code> — Analyze sentiment of multiple statements and return overall score</li>
</ul>
</div>
<div class="section">
<h2>Example Usage</h2>
<p><b>Single analysis:</b></p>
<pre>
POST /v1/analyze/sentiment/single
{
"text": "Apple stock is set to rally after strong iPhone sales."
}
</pre>
<p><b>Batch analysis:</b></p>
<pre>
POST /v1/analyze/sentiment/batch
{
"texts": [
"Tesla shares might drop due to weaker demand in China.",
"Microsoft reported record cloud revenue growth."
]
}
</pre>
</div>
<div class="footer">
<p><i>Powered and hosted by Sentimind Labs ✨</i></p>
</div>
</body>
</html>
"""
@app.get("/v1/health")
def health_check():
return {"status": "ok", "service": "Sentimind API", "version": "v1"}
@app.post("/v1/analyze/sentiment/single")
def analyze_single(input_data: InputText):
result = sentiment_analyzer(input_data.text)[0]
return {
"input_text": input_data.text,
"sentiment": result['label'].lower(),
"confidence": float(result['score'])
}
@app.post("/v1/analyze/sentiment/batch")
def analyze_batch(input_data: InputBatch):
results = []
positive_count = 0
negative_count = 0
for text in input_data.texts:
result = sentiment_analyzer(text)[0]
sentiment = result['label'].lower()
confidence = float(result['score'])
results.append({
"text": text,
"sentiment": sentiment,
"confidence": confidence
})
if sentiment == "positive":
positive_count += 1
else:
negative_count += 1
total = len(input_data.texts)
positive_ratio = positive_count / total if total > 0 else 0
negative_ratio = negative_count / total if total > 0 else 0
overall_sentiment = "positive" if positive_count >= negative_count else "negative"
return {
"results": results,
"overall_sentiment": overall_sentiment,
"positive_ratio": round(positive_ratio, 2),
"negative_ratio": round(negative_ratio, 2)
}
|