Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from fastapi import Depends, FastAPI, HTTPException, status
|
| 2 |
from sqlalchemy.orm import Session
|
|
|
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import schemas, models
|
| 5 |
from transformers import pipeline
|
|
@@ -24,20 +25,27 @@ def get_db():
|
|
| 24 |
finally:
|
| 25 |
db.close()
|
| 26 |
|
| 27 |
-
@app.post("/analyze_sentiment", status_code=status.HTTP_201_CREATED)
|
| 28 |
-
def create_sentiment_result(
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
text_content = text_input
|
| 35 |
sentiment_analysis_result = pipe(text_content)
|
| 36 |
|
| 37 |
# Create a new SentimentResult instance
|
| 38 |
new_sentiment_result = models.SentimentResult(
|
| 39 |
score=sentiment_analysis_result[0]['score'],
|
| 40 |
-
label
|
| 41 |
text_input=text_content
|
| 42 |
)
|
| 43 |
|
|
@@ -46,9 +54,14 @@ def create_sentiment_result(sentiment_result: schemas.SentimentResultCreate, tex
|
|
| 46 |
db.commit()
|
| 47 |
db.refresh(new_sentiment_result)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
raise HTTPException(
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
@app.delete("/sentiment/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
| 54 |
def delete_sentiment_result(id: int, db: Session = Depends(get_db)):
|
|
|
|
| 1 |
from fastapi import Depends, FastAPI, HTTPException, status
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
import schemas, models
|
| 6 |
from transformers import pipeline
|
|
|
|
| 25 |
finally:
|
| 26 |
db.close()
|
| 27 |
|
| 28 |
+
@app.post("/analyze_sentiment", response_model=schemas.SentimentResult, status_code=status.HTTP_201_CREATED)
|
| 29 |
+
def create_sentiment_result(
|
| 30 |
+
sentiment_result: schemas.SentimentResultCreate,
|
| 31 |
+
text_input: str,
|
| 32 |
+
db: Session = Depends(get_db)
|
| 33 |
+
):
|
| 34 |
try:
|
| 35 |
+
# Perform input validation
|
| 36 |
+
if not isinstance(text_input, str) or not text_input.strip():
|
| 37 |
+
raise HTTPException(
|
| 38 |
+
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
| 39 |
+
detail="Invalid text input"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
text_content = text_input
|
| 43 |
sentiment_analysis_result = pipe(text_content)
|
| 44 |
|
| 45 |
# Create a new SentimentResult instance
|
| 46 |
new_sentiment_result = models.SentimentResult(
|
| 47 |
score=sentiment_analysis_result[0]['score'],
|
| 48 |
+
label=sentiment_analysis_result[0]['label'],
|
| 49 |
text_input=text_content
|
| 50 |
)
|
| 51 |
|
|
|
|
| 54 |
db.commit()
|
| 55 |
db.refresh(new_sentiment_result)
|
| 56 |
|
| 57 |
+
# Return JSON response
|
| 58 |
+
return JSONResponse(content=new_sentiment_result.dict(), status_code=status.HTTP_201_CREATED)
|
| 59 |
+
|
| 60 |
except Exception as e:
|
| 61 |
+
raise HTTPException(
|
| 62 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 63 |
+
detail=f"An error occurred: {str(e)}"
|
| 64 |
+
)
|
| 65 |
|
| 66 |
@app.delete("/sentiment/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
| 67 |
def delete_sentiment_result(id: int, db: Session = Depends(get_db)):
|