Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,132 +1,155 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import collections
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
print(f"Loading model {MODEL_NAME}...")
|
10 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
11 |
model = AutoModelForCausalLM.from_pretrained(
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
15 |
)
|
16 |
-
print("Model loaded successfully!")
|
17 |
-
|
18 |
-
def format_rps_game_prompt(game_data):
|
19 |
-
"""Format Rock-Paper-Scissors game data into a simple prompt for the LLM"""
|
20 |
-
try:
|
21 |
-
# Parse the JSON game data
|
22 |
-
if isinstance(game_data, str):
|
23 |
-
game_data = json.loads(game_data)
|
24 |
-
|
25 |
-
# Extract key game information
|
26 |
-
player_history = game_data.get("player_history", [])
|
27 |
-
opponent_history = game_data.get("opponent_history", [])
|
28 |
-
rounds_played = len(player_history)
|
29 |
-
player_score = game_data.get("player_score", 0)
|
30 |
-
opponent_score = game_data.get("opponent_score", 0)
|
31 |
-
draws = game_data.get("draws", 0)
|
32 |
-
|
33 |
-
# Create a simple prompt with just the game state
|
34 |
-
prompt = f"""You are a Rock-Paper-Scissors strategy advisor.
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
"""
|
54 |
-
|
55 |
-
except Exception as e:
|
56 |
-
return f"Error formatting prompt: {str(e)}\n\nPlease provide game data in a valid JSON format."
|
57 |
|
58 |
-
def
|
59 |
-
"""
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
# If model response is too short, add fallback advice
|
84 |
-
if len(response) < 30:
|
85 |
-
# Simple fallback based on opponent's last move
|
86 |
-
if len(opponent_history) > 0:
|
87 |
-
last_move = opponent_history[-1]
|
88 |
-
if last_move == "Rock":
|
89 |
-
suggestion = "Paper"
|
90 |
-
elif last_move == "Paper":
|
91 |
-
suggestion = "Scissors"
|
92 |
-
else:
|
93 |
-
suggestion = "Rock"
|
94 |
-
|
95 |
-
response += f"\n\nBased on the opponent's last move ({last_move}), a reasonable counter would be:\nRecommendation: {suggestion}"
|
96 |
-
else:
|
97 |
-
response += "\n\nWith no game history to analyze, each move has equal probability of success.\nRecommendation: Paper"
|
98 |
-
|
99 |
-
return response
|
100 |
-
except Exception as e:
|
101 |
-
return f"Error generating advice: {str(e)}"
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
"
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
-
|
113 |
-
with
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
with gr.Row():
|
118 |
with gr.Column():
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
)
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
with gr.Column():
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
-
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
# Launch the app
|
132 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
|
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
|
5 |
+
# Load model and tokenizer
|
6 |
+
model_name = "Qwen/Qwen2-0.5B"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
|
|
|
|
8 |
model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
model_name,
|
10 |
+
device_map="auto",
|
11 |
+
torch_dtype=torch.float16,
|
12 |
+
trust_remote_code=True
|
13 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
def generate_response(prompt, max_length=300, temperature=0.7):
|
16 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
17 |
+
|
18 |
+
with torch.no_grad():
|
19 |
+
outputs = model.generate(
|
20 |
+
**inputs,
|
21 |
+
max_new_tokens=max_length,
|
22 |
+
do_sample=True,
|
23 |
+
temperature=temperature,
|
24 |
+
top_p=0.9,
|
25 |
+
)
|
26 |
+
|
27 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
28 |
+
# Remove the prompt from the response
|
29 |
+
if response.startswith(prompt):
|
30 |
+
response = response[len(prompt):]
|
31 |
+
|
32 |
+
return response.strip()
|
33 |
|
34 |
+
# Create different test templates
|
35 |
+
test_templates = {
|
36 |
+
"Basic Game History": """
|
37 |
+
Game history: {game_history}
|
38 |
+
Player score: {player_score}
|
39 |
+
AI score: {ai_score}
|
40 |
+
Last move: {last_move}
|
41 |
|
42 |
+
Based on this information, analyze the game and recommend a next move.
|
43 |
+
""",
|
44 |
+
|
45 |
+
"With Pre-calculated Statistics": """
|
46 |
+
Game history: {game_history}
|
47 |
+
Player's move frequencies: Rock ({rock_freq}%), Paper ({paper_freq}%), Scissors ({scissors_freq}%)
|
48 |
+
Player's patterns:
|
49 |
+
- After playing Rock, chooses Paper: {rock_to_paper}%
|
50 |
+
- After playing Paper, chooses Scissors: {paper_to_scissors}%
|
51 |
+
- After playing Scissors, chooses Rock: {scissors_to_rock}%
|
52 |
|
53 |
+
What should be the AI's next move?
|
54 |
+
""",
|
55 |
+
|
56 |
+
"Simplified Decision": """
|
57 |
+
Recent moves: {recent_moves}
|
58 |
+
Based on this pattern, the player is likely to play {likely_next} next.
|
59 |
+
To counter {likely_next}, the AI should play:
|
60 |
"""
|
61 |
+
}
|
|
|
|
|
62 |
|
63 |
+
def create_sample_data(template_key):
|
64 |
+
"""Create sample data for the selected template"""
|
65 |
+
if template_key == "Basic Game History":
|
66 |
+
return {
|
67 |
+
"game_history": "R,P,S,R,P,S,S,R,P,R",
|
68 |
+
"player_score": "5",
|
69 |
+
"ai_score": "3",
|
70 |
+
"last_move": "P"
|
71 |
+
}
|
72 |
+
elif template_key == "With Pre-calculated Statistics":
|
73 |
+
return {
|
74 |
+
"game_history": "R,P,S,R,P,S,S,R,P,R",
|
75 |
+
"rock_freq": "40",
|
76 |
+
"paper_freq": "30",
|
77 |
+
"scissors_freq": "30",
|
78 |
+
"rock_to_paper": "75",
|
79 |
+
"paper_to_scissors": "67",
|
80 |
+
"scissors_to_rock": "50"
|
81 |
+
}
|
82 |
+
elif template_key == "Simplified Decision":
|
83 |
+
return {
|
84 |
+
"recent_moves": "R,P,S,R,P",
|
85 |
+
"likely_next": "S"
|
86 |
+
}
|
87 |
+
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
def format_prompt(template_key, **kwargs):
|
90 |
+
"""Format the selected template with provided values"""
|
91 |
+
template = test_templates[template_key]
|
92 |
+
return template.format(**kwargs)
|
93 |
+
|
94 |
+
def update_template_inputs(template_name):
|
95 |
+
"""Update the input fields based on the selected template"""
|
96 |
+
sample_data = create_sample_data(template_name)
|
97 |
+
inputs = []
|
98 |
+
|
99 |
+
for key, value in sample_data.items():
|
100 |
+
inputs.append(gr.Textbox(value=value, label=key))
|
101 |
+
|
102 |
+
return inputs
|
103 |
|
104 |
+
def test_model(template_name, *args):
|
105 |
+
"""Test the model with the provided template and inputs"""
|
106 |
+
sample_data = create_sample_data(template_name)
|
107 |
+
data = dict(zip(sample_data.keys(), args))
|
108 |
+
|
109 |
+
prompt = format_prompt(template_name, **data)
|
110 |
+
response = generate_response(prompt)
|
111 |
+
|
112 |
+
return prompt, response
|
113 |
+
|
114 |
+
# Define the interface
|
115 |
+
with gr.Blocks() as demo:
|
116 |
+
gr.Markdown("# Qwen2 0.5B Testing for Rock-Paper-Scissors Game Analysis")
|
117 |
|
118 |
with gr.Row():
|
119 |
with gr.Column():
|
120 |
+
template_dropdown = gr.Dropdown(
|
121 |
+
choices=list(test_templates.keys()),
|
122 |
+
value="Basic Game History",
|
123 |
+
label="Select Template"
|
124 |
)
|
125 |
+
|
126 |
+
input_container = gr.Column()
|
127 |
+
sample_data = create_sample_data("Basic Game History")
|
128 |
+
input_fields = [gr.Textbox(value=v, label=k) for k, v in sample_data.items()]
|
129 |
+
|
130 |
+
for field in input_fields:
|
131 |
+
input_container.append(field)
|
132 |
+
|
133 |
+
test_button = gr.Button("Test Model")
|
134 |
|
135 |
with gr.Column():
|
136 |
+
prompt_output = gr.Textbox(label="Formatted Prompt")
|
137 |
+
response_output = gr.Textbox(label="Model Response")
|
138 |
+
|
139 |
+
def update_inputs(template_name):
|
140 |
+
sample_data = create_sample_data(template_name)
|
141 |
+
return [gr.Textbox(value=v, label=k) for k, v in sample_data.items()]
|
142 |
+
|
143 |
+
template_dropdown.change(
|
144 |
+
fn=update_inputs,
|
145 |
+
inputs=template_dropdown,
|
146 |
+
outputs=input_container
|
147 |
+
)
|
148 |
|
149 |
+
test_button.click(
|
150 |
+
fn=test_model,
|
151 |
+
inputs=[template_dropdown] + input_fields,
|
152 |
+
outputs=[prompt_output, response_output]
|
153 |
+
)
|
154 |
|
|
|
155 |
demo.launch()
|