shukdevdattaEX commited on
Commit
b7a676f
·
verified ·
1 Parent(s): f3d060a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -42
app.py CHANGED
@@ -112,8 +112,8 @@ def moderate_content(api_key, user_message, chat_history):
112
  # Get the response
113
  moderation_result = chat_completion.choices[0].message.content
114
 
115
- # Parse and format the response
116
- formatted_response = format_moderation_response(moderation_result, user_message)
117
 
118
  # Update chat history with proper message format
119
  user_msg = {"role": "user", "content": user_message}
@@ -129,50 +129,17 @@ def moderate_content(api_key, user_message, chat_history):
129
  new_history = chat_history + [user_msg, assistant_msg]
130
  return new_history, new_history
131
 
132
- def format_moderation_response(result, original_content):
133
  """
134
- Format the moderation result into a readable response
135
  """
136
  try:
137
- result_lower = result.lower()
138
-
139
- # Build formatted response
140
- response = "## 🛡️ Content Moderation Result\n\n"
141
- response += f"**Analyzed Content:** _{original_content[:100]}{'...' if len(original_content) > 100 else ''}_\n\n"
142
-
143
- # Check if content appears safe
144
- if "safe" in result_lower and ("unsafe" not in result_lower or result_lower.index("safe") < result_lower.index("unsafe")):
145
- response += "### ✅ Status: SAFE\n\n"
146
- response += "The content appears to be appropriate and does not violate any harm policies.\n\n"
147
- else:
148
- response += "### ⚠️ Status: FLAGGED\n\n"
149
-
150
- # Check for severity
151
- if "critical" in result_lower:
152
- response += "**Severity:** 🔴 CRITICAL\n\n"
153
- elif "high" in result_lower:
154
- response += "**Severity:** 🟠 HIGH\n\n"
155
- elif "medium" in result_lower:
156
- response += "**Severity:** 🟡 MEDIUM\n\n"
157
- elif "low" in result_lower:
158
- response += "**Severity:** 🟢 LOW\n\n"
159
-
160
- # Check for specific categories
161
- flagged_categories = []
162
- for code, category in HARM_CATEGORIES.items():
163
- if code.lower() in result_lower or code in result:
164
- flagged_categories.append(f"- **{code}: {category}** - {HARM_DESCRIPTIONS[code]}")
165
-
166
- if flagged_categories:
167
- response += "**Flagged Categories:**\n" + "\n".join(flagged_categories) + "\n\n"
168
-
169
- # Add detailed analysis
170
- response += "---\n\n### 📊 Detailed Analysis:\n\n" + result
171
-
172
  return response
173
 
174
  except Exception as e:
175
- return f"**Moderation Analysis:**\n\n{result}"
176
 
177
  def clear_chat():
178
  """Clear the chat history"""
@@ -214,7 +181,7 @@ with gr.Blocks(title="Content Moderation Chatbot", theme=gr.themes.Soft()) as ap
214
  chatbot = gr.Chatbot(
215
  label="Moderation Results",
216
  height=450,
217
- show_label=True,
218
  )
219
 
220
  with gr.Row():
@@ -335,10 +302,11 @@ with gr.Blocks(title="Content Moderation Chatbot", theme=gr.themes.Soft()) as ap
335
  ### 🎯 Key Features:
336
 
337
  - ✅ **Enhanced System Prompt**: Detailed instructions for comprehensive analysis
338
- - ✅ **Severity Levels**: LOW, MEDIUM, HIGH, or CRITICAL risk assessment
339
  - ✅ **Category Detection**: Identifies all applicable harm categories
340
  - ✅ **Detailed Explanations**: Clear reasoning for each flag
341
  - ✅ **15 Example Queries**: One safe example + one for each harm category
 
342
 
343
  ### 🔒 Privacy & Security:
344
 
 
112
  # Get the response
113
  moderation_result = chat_completion.choices[0].message.content
114
 
115
+ # Format the response - ONLY show detailed analysis
116
+ formatted_response = format_moderation_response(moderation_result)
117
 
118
  # Update chat history with proper message format
119
  user_msg = {"role": "user", "content": user_message}
 
129
  new_history = chat_history + [user_msg, assistant_msg]
130
  return new_history, new_history
131
 
132
+ def format_moderation_response(result):
133
  """
134
+ Format the moderation result - ONLY show detailed analysis
135
  """
136
  try:
137
+ # Simply return the detailed analysis with header
138
+ response = "### 📊 Detailed Analysis:\n\n" + result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  return response
140
 
141
  except Exception as e:
142
+ return f"### 📊 Detailed Analysis:\n\n{result}"
143
 
144
  def clear_chat():
145
  """Clear the chat history"""
 
181
  chatbot = gr.Chatbot(
182
  label="Moderation Results",
183
  height=450,
184
+ show_label=True
185
  )
186
 
187
  with gr.Row():
 
302
  ### 🎯 Key Features:
303
 
304
  - ✅ **Enhanced System Prompt**: Detailed instructions for comprehensive analysis
305
+ - ✅ **Direct Model Output**: Shows only the detailed analysis from the model
306
  - ✅ **Category Detection**: Identifies all applicable harm categories
307
  - ✅ **Detailed Explanations**: Clear reasoning for each flag
308
  - ✅ **15 Example Queries**: One safe example + one for each harm category
309
+ - ✅ **Clean Interface**: No extra formatting, just pure analysis
310
 
311
  ### 🔒 Privacy & Security:
312