Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ else:
|
|
13 |
raise ValueError("HUGGINGFACE_TOKEN environment variable not set.")
|
14 |
|
15 |
# Load the trained model and tokenizer
|
16 |
-
model_name = "
|
17 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
18 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
19 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -43,28 +43,31 @@ def classify_message(message):
|
|
43 |
return "Hate speech/Offensive" if prediction == 1 else "Not hate speech/Offensive"
|
44 |
|
45 |
# Chat simulation function
|
46 |
-
def chat_interface(history
|
|
|
|
|
|
|
47 |
username = random.choice(usernames)
|
48 |
new_message = random.choice(game_responses)
|
49 |
classification = classify_message(new_message)
|
50 |
blurred_message = "****" if classification == "Hate speech/Offensive" else new_message
|
51 |
-
history.append(
|
52 |
|
53 |
# Generate automated game response
|
54 |
bot_username = "GameMaster"
|
55 |
bot_response = random.choice(game_responses)
|
56 |
-
history.append(
|
57 |
|
58 |
-
return history
|
59 |
|
60 |
# Create Gradio interface
|
61 |
def main():
|
62 |
with gr.Blocks() as app:
|
63 |
gr.Markdown("# Game Chat Hate Speech Detection Simulator")
|
64 |
-
chatbot = gr.Chatbot()
|
65 |
submit = gr.Button("Generate Message")
|
66 |
|
67 |
-
submit.click(chat_interface, inputs=[chatbot
|
68 |
|
69 |
app.launch()
|
70 |
|
|
|
13 |
raise ValueError("HUGGINGFACE_TOKEN environment variable not set.")
|
14 |
|
15 |
# Load the trained model and tokenizer
|
16 |
+
model_name = "your-username/hate-speech-classifier" # Replace with your actual Hugging Face model repo
|
17 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
18 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
19 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
43 |
return "Hate speech/Offensive" if prediction == 1 else "Not hate speech/Offensive"
|
44 |
|
45 |
# Chat simulation function
|
46 |
+
def chat_interface(history):
|
47 |
+
if history is None:
|
48 |
+
history = []
|
49 |
+
|
50 |
username = random.choice(usernames)
|
51 |
new_message = random.choice(game_responses)
|
52 |
classification = classify_message(new_message)
|
53 |
blurred_message = "****" if classification == "Hate speech/Offensive" else new_message
|
54 |
+
history.append({"role": "user", "content": f"{username}: {blurred_message}"})
|
55 |
|
56 |
# Generate automated game response
|
57 |
bot_username = "GameMaster"
|
58 |
bot_response = random.choice(game_responses)
|
59 |
+
history.append({"role": "assistant", "content": f"{bot_username}: {bot_response}"})
|
60 |
|
61 |
+
return history
|
62 |
|
63 |
# Create Gradio interface
|
64 |
def main():
|
65 |
with gr.Blocks() as app:
|
66 |
gr.Markdown("# Game Chat Hate Speech Detection Simulator")
|
67 |
+
chatbot = gr.Chatbot(type="messages")
|
68 |
submit = gr.Button("Generate Message")
|
69 |
|
70 |
+
submit.click(chat_interface, inputs=[chatbot], outputs=[chatbot])
|
71 |
|
72 |
app.launch()
|
73 |
|