hardik-kanzariya commited on
Commit
2f6f156
1 Parent(s): d72a1fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -51,14 +51,10 @@ def getPrediction(input):
51
  return result, confidence_score
52
 
53
  def getSentiment(idx):
54
- match idx:
55
- case 0:
56
- return "Negative"
57
- case 1:
58
- return "Positive"
59
- case default:
60
- return "Neutral"
61
 
 
 
62
  text = st.text_area("Enter Text...")
63
 
64
  if text:
@@ -67,3 +63,13 @@ if text:
67
  response = {"prediction": getSentiment(prediction[0]) + " Statement",
68
  "confidence": "{:.2f}".format(float(confidence_score[0] * 100)) + "%"} # Adjust as necessary for output formatting
69
  st.json(response)
 
 
 
 
 
 
 
 
 
 
 
51
  return result, confidence_score
52
 
53
  def getSentiment(idx):
54
+ return {0: "Negative", 1: "Positive", 2: "Neutral"}.get(idx, "Neutral")
 
 
 
 
 
 
55
 
56
+ # Streamlit UI
57
+ st.title("Sentiment Analysis")
58
  text = st.text_area("Enter Text...")
59
 
60
  if text:
 
63
  response = {"prediction": getSentiment(prediction[0]) + " Statement",
64
  "confidence": "{:.2f}".format(float(confidence_score[0] * 100)) + "%"} # Adjust as necessary for output formatting
65
  st.json(response)
66
+
67
+ # Add a POST endpoint
68
+ def api_predict(text):
69
+ prediction, confidence_score = getPrediction(text)
70
+ return {
71
+ "prediction": getSentiment(prediction) + " Statement",
72
+ "confidence": f"{confidence_score * 100:.2f}%"
73
+ }
74
+
75
+ st.experimental_set_query_params(api_predict=api_predict)