Spaces:
Sleeping
Sleeping
Commit ·
a6dbdd5
1
Parent(s): 19946f1
Fix matplotlib dir and add httpx to requirements
Browse files
app.py
CHANGED
|
@@ -1,35 +1,41 @@
|
|
| 1 |
import os
|
| 2 |
-
import logging
|
| 3 |
-
import datetime
|
| 4 |
-
from io import BytesIO
|
| 5 |
-
|
| 6 |
-
from fastapi import FastAPI, HTTPException
|
| 7 |
-
from pydantic import BaseModel
|
| 8 |
-
from fastapi.responses import StreamingResponse
|
| 9 |
-
|
| 10 |
-
import matplotlib
|
| 11 |
-
matplotlib.use('Agg') # headless backend
|
| 12 |
-
import matplotlib.pyplot as plt
|
| 13 |
|
| 14 |
-
from transformers import pipeline
|
| 15 |
-
import httpx
|
| 16 |
-
|
| 17 |
-
# Set environment variables ONCE before transformers import
|
| 18 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 19 |
os.environ["MPLCONFIGDIR"] = "/tmp/mplconfig"
|
|
|
|
| 20 |
|
| 21 |
-
# Create necessary dirs if not exist
|
| 22 |
os.makedirs("/tmp/huggingface", exist_ok=True)
|
| 23 |
os.makedirs("/tmp/mplconfig", exist_ok=True)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# Configure logging
|
| 26 |
logging.basicConfig(level=logging.INFO)
|
| 27 |
logger = logging.getLogger(__name__)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
app = FastAPI()
|
| 31 |
|
| 32 |
-
# Load transformers
|
| 33 |
try:
|
| 34 |
ner_model = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
|
| 35 |
sentiment_model = pipeline("sentiment-analysis", model="ProsusAI/finbert")
|
|
@@ -39,10 +45,11 @@ except Exception as e:
|
|
| 39 |
ner_model = None
|
| 40 |
sentiment_model = None
|
| 41 |
|
| 42 |
-
# Request body
|
| 43 |
class TextRequest(BaseModel):
|
| 44 |
text: str
|
| 45 |
|
|
|
|
| 46 |
class CoinRequest(BaseModel):
|
| 47 |
coin_id: str
|
| 48 |
|
|
@@ -84,7 +91,7 @@ def analyze_ner(req: TextRequest):
|
|
| 84 |
raise HTTPException(status_code=500, detail="NER analysis failed")
|
| 85 |
|
| 86 |
@app.post("/chart")
|
| 87 |
-
|
| 88 |
coin_id = req.coin_id.strip().lower()
|
| 89 |
if not coin_id:
|
| 90 |
raise HTTPException(status_code=400, detail="coin_id cannot be empty")
|
|
@@ -93,8 +100,7 @@ async def generate_chart(req: CoinRequest):
|
|
| 93 |
params = {"vs_currency": "usd", "days": "1"}
|
| 94 |
|
| 95 |
try:
|
| 96 |
-
|
| 97 |
-
res = await client.get(url, params=params)
|
| 98 |
if res.status_code != 200:
|
| 99 |
logger.error(f"CoinGecko API error: {res.text}")
|
| 100 |
raise HTTPException(status_code=502, detail="Failed to fetch coin data from CoinGecko")
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 4 |
os.environ["MPLCONFIGDIR"] = "/tmp/mplconfig"
|
| 5 |
+
os.environ["HF_HOME"] = "/tmp"
|
| 6 |
|
|
|
|
| 7 |
os.makedirs("/tmp/huggingface", exist_ok=True)
|
| 8 |
os.makedirs("/tmp/mplconfig", exist_ok=True)
|
| 9 |
|
| 10 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 11 |
+
|
| 12 |
+
os.environ["HF_HOME"] = "/tmp"
|
| 13 |
+
# Load the NER model
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained("dslim/bert-base-NER")
|
| 15 |
+
model = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
from fastapi import FastAPI, HTTPException
|
| 20 |
+
from pydantic import BaseModel
|
| 21 |
+
from transformers import pipeline
|
| 22 |
+
from fastapi.responses import StreamingResponse
|
| 23 |
+
import matplotlib
|
| 24 |
+
matplotlib.use('Agg') # ✅ Force headless-safe backend
|
| 25 |
+
import matplotlib.pyplot as plt # ✅ Safe to use now
|
| 26 |
+
import requests
|
| 27 |
+
import datetime
|
| 28 |
+
from io import BytesIO
|
| 29 |
+
import logging
|
| 30 |
+
|
| 31 |
# Configure logging
|
| 32 |
logging.basicConfig(level=logging.INFO)
|
| 33 |
logger = logging.getLogger(__name__)
|
| 34 |
|
| 35 |
+
# Create FastAPI app
|
| 36 |
app = FastAPI()
|
| 37 |
|
| 38 |
+
# Load transformers models
|
| 39 |
try:
|
| 40 |
ner_model = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
|
| 41 |
sentiment_model = pipeline("sentiment-analysis", model="ProsusAI/finbert")
|
|
|
|
| 45 |
ner_model = None
|
| 46 |
sentiment_model = None
|
| 47 |
|
| 48 |
+
# Request body schema for sentiment and NER
|
| 49 |
class TextRequest(BaseModel):
|
| 50 |
text: str
|
| 51 |
|
| 52 |
+
# Request body schema for chart
|
| 53 |
class CoinRequest(BaseModel):
|
| 54 |
coin_id: str
|
| 55 |
|
|
|
|
| 91 |
raise HTTPException(status_code=500, detail="NER analysis failed")
|
| 92 |
|
| 93 |
@app.post("/chart")
|
| 94 |
+
def generate_chart(req: CoinRequest):
|
| 95 |
coin_id = req.coin_id.strip().lower()
|
| 96 |
if not coin_id:
|
| 97 |
raise HTTPException(status_code=400, detail="coin_id cannot be empty")
|
|
|
|
| 100 |
params = {"vs_currency": "usd", "days": "1"}
|
| 101 |
|
| 102 |
try:
|
| 103 |
+
res = requests.get(url, params=params)
|
|
|
|
| 104 |
if res.status_code != 200:
|
| 105 |
logger.error(f"CoinGecko API error: {res.text}")
|
| 106 |
raise HTTPException(status_code=502, detail="Failed to fetch coin data from CoinGecko")
|