File size: 2,832 Bytes
5c76e7c
81ff115
5c76e7c
81ff115
5c76e7c
 
 
 
 
 
 
 
 
 
 
 
 
916976b
5c76e7c
 
 
 
916976b
 
5c76e7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
916976b
 
5c76e7c
 
 
916976b
 
5c76e7c
 
 
 
 
 
916976b
5c76e7c
85f2aad
 
5c76e7c
 
 
 
 
 
 
 
 
 
 
 
85f2aad
916976b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

import gradio as gr
from transformers import pipeline

# Define your Minecraft actions with descriptions
ACTIONS = {
    "move_towards_player": "walk or move toward a specific player",
    "stand_still": "remain in place, don't move",
    "follow_player": "continuously follow behind a player",
    "run_away": "move away from a player or danger",
    "jump": "jump up or over something",
    "mine_block": "break or mine a block",
    "place_block": "place or put down a block",
    "attack": "attack or fight something",
    "use_item": "use or activate an item",
    "chat_only": "just chat without taking physical action"
}

# Load the zero-shot classification pipeline
# Using a model that works well with Spaces and has good zero-shot performance
classifier = pipeline("zero-shot-classification", 
                      model="facebook/bart-large-mnli")

def predict_action(text):
    # Extract just the action names for classification
    action_names = list(ACTIONS.keys())
    
    # Add context to improve classification by combining action names with descriptions
    candidate_labels = [f"{action}: {ACTIONS[action]}" for action in action_names]
    
    # Run zero-shot classification
    result = classifier(text, candidate_labels, multi_label=False)
    
    # Extract results
    action_scores = {}
    for i, label in enumerate(result["labels"]):
        # Extract just the action name from the combined label+description
        action_name = label.split(":")[0]
        action_scores[action_name] = result["scores"][i]
    
    # Find the best action
    best_action = max(action_scores, key=action_scores.get)
    confidence = action_scores[best_action]
    
    return {
        "action": best_action,
        "confidence": confidence,
        "all_actions": action_scores
    }

# Example to test (you can remove this in production)
example_text = "I want to go and meet that player over there"
test_result = predict_action(example_text)
print(f"Example: '{example_text}'")
print(f"Best action: {test_result['action']}")
print(f"Confidence: {test_result['confidence']:.4f}")

# Create the Gradio interface
demo = gr.Interface(
    fn=predict_action,
    inputs=gr.Textbox(label="Character text from C.AI"),
    outputs=gr.JSON(label="Predicted Action"),
    title="Minecraft NPC Action Predictor",
    description="""This tool analyzes what your C.AI character says and determines the best action to take in Minecraft.
                   Input the character's dialogue to get the appropriate in-game action.""",
    examples=[
        ["I'll help you build that house!"],
        ["Look out, there's a creeper behind you!"],
        ["Let me show you the way to the village."],
        ["I think I'll just wait here for a while."],
        ["I need to break this stone to make a path."]
    ]
)

demo.launch()