Spaces:
Runtime error
Runtime error
Commit
·
062c36e
1
Parent(s):
8c3ca89
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
-
from fastapi import Depends, FastAPI, HTTPException,status
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
-
import schemas,models
|
| 4 |
from transformers import pipeline
|
| 5 |
-
import database
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
-
|
| 9 |
models.database.Base.metadata.create_all(bind=database.engine)
|
| 10 |
-
|
| 11 |
classifier = pipeline("sentiment-analysis")
|
| 12 |
-
|
|
|
|
| 13 |
def get_db():
|
| 14 |
db = database.SessionLocal()
|
| 15 |
try:
|
|
@@ -18,9 +17,12 @@ def get_db():
|
|
| 18 |
db.close()
|
| 19 |
|
| 20 |
@app.post("/analyze_sentiment", status_code=status.HTTP_201_CREATED)
|
| 21 |
-
def create_sentiment_result(sentiment_result: schemas.SentimentResultCreate,text_input: str,db: Session = Depends(get_db)):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
-
# Perform sentiment analysis
|
| 24 |
text_content = text_input
|
| 25 |
sentiment_analysis_result = classifier(text_content)
|
| 26 |
|
|
@@ -30,6 +32,7 @@ def create_sentiment_result(sentiment_result: schemas.SentimentResultCreate,text
|
|
| 30 |
negative_score=sentiment_analysis_result["score"] if sentiment_analysis_result["label"] == "NEGATIVE" else 0.0,
|
| 31 |
text_input=text_content
|
| 32 |
)
|
|
|
|
| 33 |
# Add the new SentimentResult to the database
|
| 34 |
db.add(new_sentiment_result)
|
| 35 |
db.commit()
|
|
@@ -37,17 +40,20 @@ def create_sentiment_result(sentiment_result: schemas.SentimentResultCreate,text
|
|
| 37 |
|
| 38 |
return new_sentiment_result
|
| 39 |
except Exception as e:
|
| 40 |
-
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
|
| 41 |
|
| 42 |
@app.delete("/sentiment/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
| 43 |
def delete_sentiment_result(id: int, db: Session = Depends(get_db)):
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"SentimentResult with ID {id} not found")
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
|
|
|
|
| 1 |
+
from fastapi import Depends, FastAPI, HTTPException, status
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
+
import schemas, models
|
| 4 |
from transformers import pipeline
|
| 5 |
+
import database
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
|
|
| 8 |
models.database.Base.metadata.create_all(bind=database.engine)
|
|
|
|
| 9 |
classifier = pipeline("sentiment-analysis")
|
| 10 |
+
|
| 11 |
+
# Database session dependency
|
| 12 |
def get_db():
|
| 13 |
db = database.SessionLocal()
|
| 14 |
try:
|
|
|
|
| 17 |
db.close()
|
| 18 |
|
| 19 |
@app.post("/analyze_sentiment", status_code=status.HTTP_201_CREATED)
|
| 20 |
+
def create_sentiment_result(sentiment_result: schemas.SentimentResultCreate, text_input: str, db: Session = Depends(get_db)):
|
| 21 |
+
# Perform input validation
|
| 22 |
+
if not isinstance(text_input, str) or not text_input.strip():
|
| 23 |
+
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Invalid text input")
|
| 24 |
+
|
| 25 |
try:
|
|
|
|
| 26 |
text_content = text_input
|
| 27 |
sentiment_analysis_result = classifier(text_content)
|
| 28 |
|
|
|
|
| 32 |
negative_score=sentiment_analysis_result["score"] if sentiment_analysis_result["label"] == "NEGATIVE" else 0.0,
|
| 33 |
text_input=text_content
|
| 34 |
)
|
| 35 |
+
|
| 36 |
# Add the new SentimentResult to the database
|
| 37 |
db.add(new_sentiment_result)
|
| 38 |
db.commit()
|
|
|
|
| 40 |
|
| 41 |
return new_sentiment_result
|
| 42 |
except Exception as e:
|
| 43 |
+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"An error occurred: {str(e)}")
|
| 44 |
|
| 45 |
@app.delete("/sentiment/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
| 46 |
def delete_sentiment_result(id: int, db: Session = Depends(get_db)):
|
| 47 |
+
# Retrieve the sentiment result and check for existence
|
| 48 |
+
sentiment_result_to_delete = db.query(models.SentimentResult).filter(models.SentimentResult.id == id).first()
|
| 49 |
+
|
| 50 |
+
if sentiment_result_to_delete is None:
|
| 51 |
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"SentimentResult with ID {id} not found")
|
| 52 |
+
|
| 53 |
+
# Delete the sentiment result
|
| 54 |
+
db.delete(sentiment_result_to_delete)
|
| 55 |
+
db.commit()
|
| 56 |
+
|
| 57 |
|
| 58 |
|
| 59 |
|