Spaces:
Runtime error
Runtime error
Download app.py from CaerusTech/sentimind-api: direct link, hf CLI and curl.
- Browser
- Download file 4.04 kB
-
https://huggingface.co/spaces/CaerusTech/sentimind-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/CaerusTech/sentimind-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/CaerusTech/sentimind-api/resolve/main/app.py
4.04 kB
| 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] | |
| 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> | |
| """ | |
| def health_check(): | |
| return {"status": "ok", "service": "Sentimind API", "version": "v1"} | |
| 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']) | |
| } | |
| 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) | |
| } | |