Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,75 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
# Define your actions
|
| 6 |
-
ACTIONS =
|
| 7 |
-
"move_towards_player",
|
| 8 |
-
"stand_still",
|
| 9 |
-
"follow_player",
|
| 10 |
-
"run_away",
|
| 11 |
-
"jump",
|
| 12 |
-
"mine_block",
|
| 13 |
-
"place_block",
|
| 14 |
-
"attack",
|
| 15 |
-
"use_item",
|
| 16 |
-
"chat_only"
|
| 17 |
-
|
| 18 |
|
| 19 |
-
# Load
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
model
|
| 23 |
|
| 24 |
-
# This function will need to be trained with your data
|
| 25 |
def predict_action(text):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
# Return both the action and the confidence score
|
| 32 |
return {
|
| 33 |
-
"action":
|
| 34 |
-
"confidence":
|
| 35 |
-
"all_actions":
|
| 36 |
}
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
demo = gr.Interface(
|
| 45 |
fn=predict_action,
|
| 46 |
-
inputs=gr.Textbox(label="Character text"),
|
| 47 |
-
outputs=gr.JSON(label="
|
| 48 |
-
title="Minecraft Action Predictor",
|
| 49 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
demo.launch()
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
|
|
|
| 4 |
|
| 5 |
+
# Define your Minecraft actions with descriptions
|
| 6 |
+
ACTIONS = {
|
| 7 |
+
"move_towards_player": "walk or move toward a specific player",
|
| 8 |
+
"stand_still": "remain in place, don't move",
|
| 9 |
+
"follow_player": "continuously follow behind a player",
|
| 10 |
+
"run_away": "move away from a player or danger",
|
| 11 |
+
"jump": "jump up or over something",
|
| 12 |
+
"mine_block": "break or mine a block",
|
| 13 |
+
"place_block": "place or put down a block",
|
| 14 |
+
"attack": "attack or fight something",
|
| 15 |
+
"use_item": "use or activate an item",
|
| 16 |
+
"chat_only": "just chat without taking physical action"
|
| 17 |
+
}
|
| 18 |
|
| 19 |
+
# Load the zero-shot classification pipeline
|
| 20 |
+
# Using a model that works well with Spaces and has good zero-shot performance
|
| 21 |
+
classifier = pipeline("zero-shot-classification",
|
| 22 |
+
model="facebook/bart-large-mnli")
|
| 23 |
|
|
|
|
| 24 |
def predict_action(text):
|
| 25 |
+
# Extract just the action names for classification
|
| 26 |
+
action_names = list(ACTIONS.keys())
|
| 27 |
+
|
| 28 |
+
# Add context to improve classification by combining action names with descriptions
|
| 29 |
+
candidate_labels = [f"{action}: {ACTIONS[action]}" for action in action_names]
|
| 30 |
+
|
| 31 |
+
# Run zero-shot classification
|
| 32 |
+
result = classifier(text, candidate_labels, multi_label=False)
|
| 33 |
+
|
| 34 |
+
# Extract results
|
| 35 |
+
action_scores = {}
|
| 36 |
+
for i, label in enumerate(result["labels"]):
|
| 37 |
+
# Extract just the action name from the combined label+description
|
| 38 |
+
action_name = label.split(":")[0]
|
| 39 |
+
action_scores[action_name] = result["scores"][i]
|
| 40 |
+
|
| 41 |
+
# Find the best action
|
| 42 |
+
best_action = max(action_scores, key=action_scores.get)
|
| 43 |
+
confidence = action_scores[best_action]
|
| 44 |
|
|
|
|
| 45 |
return {
|
| 46 |
+
"action": best_action,
|
| 47 |
+
"confidence": confidence,
|
| 48 |
+
"all_actions": action_scores
|
| 49 |
}
|
| 50 |
|
| 51 |
+
# Example to test (you can remove this in production)
|
| 52 |
+
example_text = "I want to go and meet that player over there"
|
| 53 |
+
test_result = predict_action(example_text)
|
| 54 |
+
print(f"Example: '{example_text}'")
|
| 55 |
+
print(f"Best action: {test_result['action']}")
|
| 56 |
+
print(f"Confidence: {test_result['confidence']:.4f}")
|
| 57 |
|
| 58 |
+
# Create the Gradio interface
|
| 59 |
demo = gr.Interface(
|
| 60 |
fn=predict_action,
|
| 61 |
+
inputs=gr.Textbox(label="Character text from C.AI"),
|
| 62 |
+
outputs=gr.JSON(label="Predicted Action"),
|
| 63 |
+
title="Minecraft NPC Action Predictor",
|
| 64 |
+
description="""This tool analyzes what your C.AI character says and determines the best action to take in Minecraft.
|
| 65 |
+
Input the character's dialogue to get the appropriate in-game action.""",
|
| 66 |
+
examples=[
|
| 67 |
+
["I'll help you build that house!"],
|
| 68 |
+
["Look out, there's a creeper behind you!"],
|
| 69 |
+
["Let me show you the way to the village."],
|
| 70 |
+
["I think I'll just wait here for a while."],
|
| 71 |
+
["I need to break this stone to make a path."]
|
| 72 |
+
]
|
| 73 |
)
|
| 74 |
|
| 75 |
demo.launch()
|