Update app.py
Browse files
app.py
CHANGED
|
@@ -81,10 +81,12 @@ def moderate_content(api_key, user_message, chat_history):
|
|
| 81 |
Moderate content using Groq's safeguard model with system prompt
|
| 82 |
"""
|
| 83 |
if not api_key or not api_key.strip():
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
if not user_message or not user_message.strip():
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
try:
|
| 90 |
# Initialize Groq client
|
|
@@ -113,14 +115,18 @@ def moderate_content(api_key, user_message, chat_history):
|
|
| 113 |
# Parse and format the response
|
| 114 |
formatted_response = format_moderation_response(moderation_result, user_message)
|
| 115 |
|
| 116 |
-
# Update chat history
|
| 117 |
-
|
|
|
|
|
|
|
| 118 |
|
| 119 |
return new_history, new_history
|
| 120 |
|
| 121 |
except Exception as e:
|
| 122 |
error_message = f"❌ **Error:** {str(e)}\n\nPlease check your API key and try again."
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
return new_history, new_history
|
| 125 |
|
| 126 |
def format_moderation_response(result, original_content):
|
|
@@ -208,8 +214,7 @@ with gr.Blocks(title="Content Moderation Chatbot", theme=gr.themes.Soft()) as ap
|
|
| 208 |
chatbot = gr.Chatbot(
|
| 209 |
label="Moderation Results",
|
| 210 |
height=450,
|
| 211 |
-
show_label=True
|
| 212 |
-
avatar_images=(None, "🤖")
|
| 213 |
)
|
| 214 |
|
| 215 |
with gr.Row():
|
|
@@ -350,4 +355,4 @@ with gr.Blocks(title="Content Moderation Chatbot", theme=gr.themes.Soft()) as ap
|
|
| 350 |
|
| 351 |
# Launch the app
|
| 352 |
if __name__ == "__main__":
|
| 353 |
-
app.launch(share=
|
|
|
|
| 81 |
Moderate content using Groq's safeguard model with system prompt
|
| 82 |
"""
|
| 83 |
if not api_key or not api_key.strip():
|
| 84 |
+
error_msg = {"role": "assistant", "content": "⚠️ Please enter your Groq API key first."}
|
| 85 |
+
return chat_history + [{"role": "user", "content": user_message}, error_msg], chat_history + [{"role": "user", "content": user_message}, error_msg]
|
| 86 |
|
| 87 |
if not user_message or not user_message.strip():
|
| 88 |
+
error_msg = {"role": "assistant", "content": "⚠️ Please enter content to moderate."}
|
| 89 |
+
return chat_history + [{"role": "user", "content": user_message}, error_msg], chat_history + [{"role": "user", "content": user_message}, error_msg]
|
| 90 |
|
| 91 |
try:
|
| 92 |
# Initialize Groq client
|
|
|
|
| 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}
|
| 120 |
+
assistant_msg = {"role": "assistant", "content": formatted_response}
|
| 121 |
+
new_history = chat_history + [user_msg, assistant_msg]
|
| 122 |
|
| 123 |
return new_history, new_history
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
error_message = f"❌ **Error:** {str(e)}\n\nPlease check your API key and try again."
|
| 127 |
+
user_msg = {"role": "user", "content": user_message}
|
| 128 |
+
assistant_msg = {"role": "assistant", "content": error_message}
|
| 129 |
+
new_history = chat_history + [user_msg, assistant_msg]
|
| 130 |
return new_history, new_history
|
| 131 |
|
| 132 |
def format_moderation_response(result, original_content):
|
|
|
|
| 214 |
chatbot = gr.Chatbot(
|
| 215 |
label="Moderation Results",
|
| 216 |
height=450,
|
| 217 |
+
show_label=True
|
|
|
|
| 218 |
)
|
| 219 |
|
| 220 |
with gr.Row():
|
|
|
|
| 355 |
|
| 356 |
# Launch the app
|
| 357 |
if __name__ == "__main__":
|
| 358 |
+
app.launch(share=False, server_name="0.0.0.0", server_port=7860)
|