monsimas commited on
Commit
af1cc53
Β·
1 Parent(s): b395403

major design improvement

Browse files
Files changed (1) hide show
  1. app.py +137 -24
app.py CHANGED
@@ -3,6 +3,8 @@ import json
3
  import os
4
  from mistralai.client import MistralClient
5
  from mistralai.models.chat_completion import ChatMessage
 
 
6
 
7
  client = MistralClient(api_key=os.environ["MISTRAL_API_KEY"])
8
  model = "open-mixtral-8x7b"
@@ -22,44 +24,155 @@ messages = {
22
  }
23
  }
24
 
 
25
  def make_request(client, model, system_message, user_message, temperature=0.7, top_p=0.6, max_tokens=50):
 
 
26
  messages = [
27
  ChatMessage(role="system", content=system_message),
28
  ChatMessage(role="user", content=user_message)
29
  ]
30
  response = client.chat(model=model, messages=messages, temperature=temperature, top_p=top_p, max_tokens=max_tokens)
 
31
  return response.choices[0].message.content if response.choices else ""
32
 
33
  def moderate_content(content, content_context, moderation_rules, language):
 
 
 
 
 
34
  system_message = messages[language]['system_message'].format("content", content_context)
35
  user_message = messages[language]['user_message'].format(content, content_context)
36
 
37
  response = make_request(client, model, system_message, user_message, temperature=0.3, top_p=0.6, max_tokens=50)
38
- try:
39
- response_json = json.loads(response)
40
- if 'spam' in response_json:
41
- spam_status = response_json['spam'].lower()
42
- if spam_status == 'true':
43
- return "Spam"
44
- elif spam_status == 'false':
45
- return "Not Spam"
46
- elif spam_status == 'possible':
47
- return "Possibly Spam"
48
- except (json.JSONDecodeError, KeyError, ValueError):
49
- pass
 
 
 
 
 
 
 
 
 
 
 
 
50
  return "Unable to determine spam status"
51
 
52
- content = gr.Textbox(label="Content")
53
- content_context = gr.Textbox(label="What is the context of this content?")
54
- moderation_rules = gr.Textbox(label="Moderation Rules")
55
- language = gr.Dropdown(["English", "Spanish", "French"], label="Language")
 
 
 
 
 
 
 
56
 
57
- interface = gr.Interface(
58
- fn=moderate_content,
59
- inputs=[content, content_context, moderation_rules, language],
60
- outputs="text",
61
- title="Content Moderation",
62
- description="Enter a piece of content, its context, and moderation rules to determine if it's spam.",
63
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- interface.launch()
 
3
  import os
4
  from mistralai.client import MistralClient
5
  from mistralai.models.chat_completion import ChatMessage
6
+ import re
7
+
8
 
9
  client = MistralClient(api_key=os.environ["MISTRAL_API_KEY"])
10
  model = "open-mixtral-8x7b"
 
24
  }
25
  }
26
 
27
+
28
  def make_request(client, model, system_message, user_message, temperature=0.7, top_p=0.6, max_tokens=50):
29
+ print(f"Making request with system message: {system_message}")
30
+ print(f"Making request with user message: {user_message}")
31
  messages = [
32
  ChatMessage(role="system", content=system_message),
33
  ChatMessage(role="user", content=user_message)
34
  ]
35
  response = client.chat(model=model, messages=messages, temperature=temperature, top_p=top_p, max_tokens=max_tokens)
36
+ print(f"Received response from the model: {response}")
37
  return response.choices[0].message.content if response.choices else ""
38
 
39
  def moderate_content(content, content_context, moderation_rules, language):
40
+ print(f"Moderating content: {content}")
41
+ print(f"Content context: {content_context}")
42
+ print(f"Moderation rules: {moderation_rules}")
43
+ print(f"Language: {language}")
44
+
45
  system_message = messages[language]['system_message'].format("content", content_context)
46
  user_message = messages[language]['user_message'].format(content, content_context)
47
 
48
  response = make_request(client, model, system_message, user_message, temperature=0.3, top_p=0.6, max_tokens=50)
49
+ print(f"Received response: {response}")
50
+
51
+ # Extract the JSON part of the response using regular expressions
52
+ json_match = re.search(r'{.*}', response, re.DOTALL)
53
+ if json_match:
54
+ json_response = json_match.group()
55
+ print(f"Extracted JSON response: {json_response}")
56
+ try:
57
+ response_json = json.loads(json_response)
58
+ print(f"Parsed JSON response: {response_json}")
59
+ if 'spam' in response_json:
60
+ spam_status = response_json['spam'].lower()
61
+ print(f"Spam status: {spam_status}")
62
+ if spam_status == 'true':
63
+ return "Spam"
64
+ elif spam_status == 'false':
65
+ return "Not Spam"
66
+ elif spam_status == 'possible':
67
+ return "Possibly Spam"
68
+ except (json.JSONDecodeError, KeyError, ValueError) as e:
69
+ print(f"Error occurred while parsing JSON: {str(e)}")
70
+ else:
71
+ print("No JSON found in the response.")
72
+
73
  return "Unable to determine spam status"
74
 
75
+ def format_spam_status(spam_status):
76
+ print(f"Formatting spam status: {spam_status}")
77
+ if spam_status == "Spam":
78
+ return "<div style='background-color: #ff4d4d; color: white; padding: 10px; border-radius: 5px;'><strong>Spam</strong></div>"
79
+ elif spam_status == "Not Spam":
80
+ return "<div style='background-color: #66bb6a; color: white; padding: 10px; border-radius: 5px;'><strong>Not Spam</strong></div>"
81
+ elif spam_status == "Possibly Spam":
82
+ return "<div style='background-color: #ffa726; color: white; padding: 10px; border-radius: 5px;'><strong>Possibly Spam</strong></div>"
83
+ else:
84
+ return spam_status
85
+
86
 
87
+ with gr.Blocks() as demo:
88
+ gr.Markdown(
89
+ """
90
+ # 🚨 Content Moderation Pilot 🚨
91
+
92
+ This is a pilot application for content moderation using the Mistral AI API. The intended use of this moderation system is via an API integration. 🌐
93
+
94
+ Please enter the content, context, moderation rules, and select the language to determine if the content is spam. πŸ•΅οΈβ€β™€οΈ
95
+ """
96
+ )
97
+
98
+ with gr.Row():
99
+ with gr.Column():
100
+ content = gr.Textbox(label="Content", placeholder="Enter the content to moderate...")
101
+ content_context = gr.Textbox(label="What is the context of this content?", placeholder="Provide the context...")
102
+ with gr.Column():
103
+ moderation_rules = gr.Textbox(label="Moderation Rules", placeholder="Enter the moderation rules...")
104
+ language = gr.Dropdown(["English", "Spanish", "French"], label="Language", value="English")
105
+
106
+ submit_button = gr.Button("πŸš€ Moderate Content")
107
+
108
+ gr.Markdown("---")
109
+
110
+ with gr.Row():
111
+ spam_status = gr.HTML(label="Spam Status")
112
+
113
+ submit_button.click(
114
+ fn=moderate_content,
115
+ inputs=[content, content_context, moderation_rules, language],
116
+ outputs=spam_status
117
+ )
118
+
119
+ gr.Markdown(
120
+ """
121
+ ## πŸ“š API Tutorial
122
+
123
+ To use the content moderation system programmatically, you can follow these steps:
124
+
125
+ 1. Make a POST request to the `/call/moderate` endpoint with the following JSON payload:
126
+
127
+ ```json
128
+ {
129
+ "data": [
130
+ "The content to moderate",
131
+ "The context of the content",
132
+ "The moderation rules to apply",
133
+ "The language of the content (English, Spanish, or French)"
134
+ ]
135
+ }
136
+ ```
137
+
138
+ 2. The POST request will return a unique `event_id` that you can use to retrieve the results.
139
+
140
+ 3. Make a GET request to the `/call/moderate/<event_id>` endpoint to stream the results using server-sent events.
141
+
142
+ Here's an example using `curl`:
143
+
144
+ 1. POST request to submit the moderation task:
145
+
146
+ ```bash
147
+ curl -X POST -H "Content-Type: application/json" -d '{
148
+ "data": [
149
+ "This is the content to moderate",
150
+ "Social media post",
151
+ "Detect spam and offensive language",
152
+ "English"
153
+ ]
154
+ }' https://huggingface.co/spaces/monsimas/SpamOrNot/call/moderate
155
+ ```
156
+
157
+ 2. GET request to retrieve the results:
158
+
159
+ ```bash
160
+ curl -N https://huggingface.co/spaces/monsimas/SpamOrNot/call/moderate/<event_id>
161
+ ```
162
+
163
+ Replace `<event_id>` with the actual `event_id` returned from the POST request.
164
+
165
+ The GET request will stream the moderation results using server-sent events. The events will have the following structure:
166
+
167
+ ```
168
+ event: completed
169
+ data: {"spam_status": "Spam" | "Not Spam" | "Possibly Spam"}
170
+ ```
171
+
172
+ You can integrate this API into your content moderation workflow to automatically detect and flag spam content. πŸš€
173
+
174
+ Note: If you're using a programming language like Python, you can use libraries like `requests` to make the API requests and handle the server-sent events.
175
+ """
176
+ )
177
 
178
+ demo.launch()