rui3000 commited on
Commit
3b34d1e
·
verified ·
1 Parent(s): bdef9ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -110
app.py CHANGED
@@ -1,132 +1,155 @@
1
  import gradio as gr
2
- import json
3
- import collections
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
- # Define model name - use a very small model
7
- MODEL_NAME = "Qwen/Qwen2-0.5B-Instruct" # 0.5B parameter instruction model
8
-
9
- print(f"Loading model {MODEL_NAME}...")
10
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
  model = AutoModelForCausalLM.from_pretrained(
12
- MODEL_NAME,
13
- low_cpu_mem_usage=True, # CPU-friendly settings
14
- device_map="cpu" # Force CPU usage
 
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
- Game State:
37
- - Rounds played: {rounds_played}
38
- - Player score: {player_score}
39
- - Opponent score: {opponent_score}
40
- - Draws: {draws}
41
- - Player's moves (oldest to newest): {', '.join(player_history)}
42
- - Opponent's moves (oldest to newest): {', '.join(opponent_history)}
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- Analyze the game history and provide advice on what move (Rock, Paper, or Scissors) the player should make next.
 
 
 
 
 
 
45
 
46
- First, explain your thought process and reasoning in detail:
47
- 1. Look for patterns in the opponent's moves
48
- 2. Consider if the opponent seems to be using any strategy
49
- 3. Evaluate if the player should try to counter the opponent's most likely next move
50
- 4. Consider any psychological factors that might be relevant
 
 
 
 
 
51
 
52
- After your explanation, end with a clear recommendation: "Recommendation: [Rock/Paper/Scissors]"
 
 
 
 
 
 
53
  """
54
- return prompt
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 generate_advice(game_data):
59
- """Generate advice based on game data using the LLM"""
60
- try:
61
- # Format the prompt
62
- prompt = format_rps_game_prompt(game_data)
63
-
64
- # Generate response from LLM (with CPU-only settings)
65
- inputs = tokenizer(prompt, return_tensors="pt")
66
-
67
- # Set max_length to avoid excessive generation
68
- outputs = model.generate(
69
- inputs["input_ids"],
70
- max_new_tokens=200, # Allow more tokens for detailed reasoning
71
- do_sample=True,
72
- temperature=0.7,
73
- top_p=0.9
74
- )
75
- full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
76
-
77
- # Extract just the model's response (remove the prompt)
78
- if full_response.startswith(prompt):
79
- response = full_response[len(prompt):].strip()
80
- else:
81
- response = full_response
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
- # Sample game data for the example
104
- sample_game_data = {
105
- "player_history": ["Rock", "Paper", "Scissors", "Rock", "Paper"],
106
- "opponent_history": ["Scissors", "Rock", "Paper", "Scissors", "Rock"],
107
- "player_score": 3,
108
- "opponent_score": 2,
109
- "draws": 0
110
- }
 
 
 
 
 
 
111
 
112
- # Create Gradio interface
113
- with gr.Blocks(title="Rock-Paper-Scissors AI Assistant") as demo:
114
- gr.Markdown("# Rock-Paper-Scissors AI Assistant")
115
- gr.Markdown("Enter your game data to get advice on your next move.")
 
 
 
 
 
 
 
 
 
116
 
117
  with gr.Row():
118
  with gr.Column():
119
- game_data_input = gr.Textbox(
120
- label="Game State (JSON)",
121
- placeholder=json.dumps(sample_game_data, indent=2),
122
- lines=10
123
  )
124
- submit_btn = gr.Button("Get Advice")
 
 
 
 
 
 
 
 
125
 
126
  with gr.Column():
127
- output = gr.Textbox(label="AI Advice", lines=10)
 
 
 
 
 
 
 
 
 
 
 
128
 
129
- submit_btn.click(generate_advice, inputs=[game_data_input], outputs=[output])
 
 
 
 
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()