CRYPTONEWS34 commited on
Commit
5fca79f
·
1 Parent(s): f2ee3e8

Added FastAPI app

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +52 -0
  3. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ # Working directory
4
+ WORKDIR /app
5
+
6
+ # Copy files
7
+ COPY requirements.txt .
8
+ RUN pip install --no-cache-dir -r requirements.txt
9
+
10
+ COPY app.py .
11
+
12
+ # Expose port 7860 (as per HF spec)
13
+ EXPOSE 7860
14
+
15
+ # Command to run FastAPI app with uvicorn
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ from transformers import pipeline
4
+ import logging
5
+
6
+ logging.basicConfig(level=logging.INFO)
7
+ logger = logging.getLogger(__name__)
8
+
9
+ app = FastAPI()
10
+
11
+ # Load models once on startup
12
+ try:
13
+ ner_model = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
14
+ sentiment_model = pipeline("sentiment-analysis", model="ProsusAI/finbert")
15
+ except Exception as e:
16
+ logger.error(f"Model loading failed: {e}")
17
+ ner_model = None
18
+ sentiment_model = None
19
+
20
+ class TextRequest(BaseModel):
21
+ text: str
22
+
23
+ @app.get("/")
24
+ def home():
25
+ return {"message": "Crypto News API is alive!"}
26
+
27
+ @app.post("/sentiment")
28
+ def analyze_sentiment(req: TextRequest):
29
+ if not sentiment_model:
30
+ raise HTTPException(status_code=503, detail="Sentiment model not available")
31
+ text = req.text
32
+ if not text:
33
+ raise HTTPException(status_code=400, detail="Text cannot be empty")
34
+ result = sentiment_model(text[:512])[0]
35
+ return {
36
+ "label": result["label"],
37
+ "score": round(result["score"] * 100, 2)
38
+ }
39
+
40
+ @app.post("/ner")
41
+ def analyze_ner(req: TextRequest):
42
+ if not ner_model:
43
+ raise HTTPException(status_code=503, detail="NER model not available")
44
+ text = req.text
45
+ if not text:
46
+ raise HTTPException(status_code=400, detail="Text cannot be empty")
47
+ entities = ner_model(text[:512])
48
+ # Filter relevant entities (ORG, PERSON, MISC, PRODUCT, GPE)
49
+ relevant = [e['word'] for e in entities if e['entity_group'] in ['ORG', 'PERSON', 'MISC', 'PRODUCT', 'GPE']]
50
+ # Remove duplicates and limit to 5
51
+ unique_entities = list(dict.fromkeys(relevant))[:5]
52
+ return {"entities": unique_entities}
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ transformers
4
+ torch