cheesecz commited on
Commit
8e6d62e
·
verified ·
1 Parent(s): 690b451

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -1
app.py CHANGED
@@ -23,6 +23,28 @@ def load_model():
23
  # Load model at startup
24
  load_model()
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  @app.route("/", methods=["GET"])
27
  def home():
28
  return jsonify({"message": "Formality Classifier API is running! Use /predict to classify text."})
@@ -49,12 +71,16 @@ def predict_formality():
49
  # Convert score to formality classification
50
  formal_percent = round(score * 100)
51
  informal_percent = 100 - formal_percent
 
 
 
 
52
 
53
  return jsonify({
54
  "formality_score": round(score, 3),
55
  "formal_percent": formal_percent,
56
  "informal_percent": informal_percent,
57
- "classification": f"Your speech is {formal_percent}% formal and {informal_percent}% informal."
58
  })
59
 
60
  except Exception as e:
 
23
  # Load model at startup
24
  load_model()
25
 
26
+ # Define formality range responses
27
+ FORMALITY_RESPONSES = {
28
+ "very_informal": "Your speech formality score is {}/100 — quite informal! Your tone is very casual, which could be great for personal conversations with friends or informal settings. However, in professional or formal situations, you may want to avoid using overly relaxed language. Consider using more respectful and polished language when addressing colleagues, clients, or superiors in business settings.",
29
+ "informal": "Your speech formality score is {}/100 — a bit informal but showing improvement! While your tone is still casual, it's better suited for friendly exchanges or informal conversations. However, for interviews, meetings, or any formal discussion, it's a good idea to increase the level of professionalism in your language. Using more polite phrases and professional vocabulary can enhance your communication in these contexts.",
30
+ "balanced": "Your speech formality score is {}/100 — a balanced mix of casual and formal! You're in a comfortable middle ground, which works well for most social interactions and some professional settings. It's appropriate for emails or casual discussions with colleagues. However, if you're presenting to a high-level audience or in formal meetings, consider polishing your language slightly by avoiding colloquialisms and being more direct in your communication.",
31
+ "formal": "Your speech formality score is {}/100 — mostly formal with a hint of informality! This tone is well-suited for professional emails, presentations, and discussions in business meetings. Your language shows respect and professionalism while still remaining approachable. For more formal settings like conferences or addressing senior executives, you may want to avoid casual phrases and adopt more sophisticated language to leave a stronger impression.",
32
+ "very_formal": "Your speech formality score is {}/100 — highly formal and polished! Your tone is perfect for formal meetings, academic presentations, or professional interactions where respect and authority are key. It's appropriate for official correspondence, addressing clients or superiors, and speaking at conferences. Keep in mind that, in more casual settings or among peers, this level of formality might feel a bit too stiff. Tailor your approach depending on your audience, but you're definitely on track for professional success."
33
+ }
34
+
35
+ def get_formality_category(formal_percent):
36
+ """Determine formality category based on percentage"""
37
+ if formal_percent <= 20:
38
+ return "very_informal"
39
+ elif formal_percent <= 40:
40
+ return "informal"
41
+ elif formal_percent <= 60:
42
+ return "balanced"
43
+ elif formal_percent <= 80:
44
+ return "formal"
45
+ else:
46
+ return "very_formal"
47
+
48
  @app.route("/", methods=["GET"])
49
  def home():
50
  return jsonify({"message": "Formality Classifier API is running! Use /predict to classify text."})
 
71
  # Convert score to formality classification
72
  formal_percent = round(score * 100)
73
  informal_percent = 100 - formal_percent
74
+
75
+ # Get the appropriate formality category and response
76
+ category = get_formality_category(formal_percent)
77
+ detailed_response = FORMALITY_RESPONSES[category].format(formal_percent)
78
 
79
  return jsonify({
80
  "formality_score": round(score, 3),
81
  "formal_percent": formal_percent,
82
  "informal_percent": informal_percent,
83
+ "classification": detailed_response
84
  })
85
 
86
  except Exception as e: