MJobe commited on
Commit
0a12a49
1 Parent(s): a90d0cf

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -2
main.py CHANGED
@@ -129,12 +129,26 @@ async def classify_text(text: str = Form(...)):
129
  except Exception as e:
130
  return JSONResponse(content=f"Error classifying text: {str(e)}", status_code=500)
131
 
132
- @app.post("/test_classify/", description="Classify the provided text with positive, neutral, or negative sentiment.")
133
  async def test_classify_text(text: str = Form(...)):
134
  try:
135
  # Perform text classification using the updated model that returns positive, neutral, or negative
136
  result = nlp_classification_v2(text)
137
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  except Exception as e:
139
  return JSONResponse(content=f"Error classifying text: {str(e)}", status_code=500)
140
 
 
129
  except Exception as e:
130
  return JSONResponse(content=f"Error classifying text: {str(e)}", status_code=500)
131
 
132
+ @@app.post("/test_classify/", description="Classify the provided text with positive, neutral, or negative sentiment.")
133
  async def test_classify_text(text: str = Form(...)):
134
  try:
135
  # Perform text classification using the updated model that returns positive, neutral, or negative
136
  result = nlp_classification_v2(text)
137
+
138
+ # Get the label (positive, neutral, negative) and format it as required
139
+ label = result[0]['label']
140
+
141
+ # Map the model labels to human-readable format
142
+ label_map = {
143
+ "LABEL_0": "Negative", # Cardiff NLP's model may use these labels
144
+ "LABEL_1": "Neutral", # Remap LABEL_1 to Neutral
145
+ "LABEL_2": "Positive" # LABEL_2 corresponds to Positive
146
+ }
147
+
148
+ # Get the readable label from the map
149
+ formatted_label = label_map.get(label, "Unknown")
150
+
151
+ return {"label": formatted_label, "score": result[0]['score']}
152
  except Exception as e:
153
  return JSONResponse(content=f"Error classifying text: {str(e)}", status_code=500)
154