rui3000 commited on
Commit
b053859
·
verified ·
1 Parent(s): 66889c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +176 -40
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import torch
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
  # Load the Qwen2 0.5B model
@@ -12,6 +13,114 @@ model = AutoModelForCausalLM.from_pretrained(
12
  trust_remote_code=True
13
  )
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def generate_response(prompt, max_length=512, temperature=0.7, top_p=0.9):
16
  """Generate a response from the Qwen2 model based on the input prompt."""
17
 
@@ -38,23 +147,29 @@ def generate_response(prompt, max_length=512, temperature=0.7, top_p=0.9):
38
  return response.strip()
39
 
40
  def process_input(
41
- raw_prompt,
42
- game_stats_template,
43
- template_type,
 
 
44
  max_length,
45
  temperature,
46
  top_p
47
  ):
48
- """Process the input and template to create the final prompt for the model."""
49
 
50
- final_prompt = ""
 
 
51
 
52
- if template_type == "Raw Prompt Only":
53
- final_prompt = raw_prompt
54
- elif template_type == "Template + Prompt":
55
- final_prompt = f"{game_stats_template}\n\n{raw_prompt}"
56
- elif template_type == "Custom Format":
57
- final_prompt = f"{game_stats_template}\n\nBased on the game statistics above, {raw_prompt}"
 
 
58
 
59
  # Generate response from the model
60
  response = generate_response(
@@ -69,32 +184,47 @@ def process_input(
69
  # Create the Gradio interface
70
  with gr.Blocks() as demo:
71
  gr.Markdown("# Qwen2 0.5B Game Analysis Tester")
72
- gr.Markdown("Use this interface to test how the Qwen2 0.5B model responds to different prompts about your game statistics.")
73
 
74
  with gr.Row():
75
  with gr.Column():
76
- template_type = gr.Radio(
77
- ["Raw Prompt Only", "Template + Prompt", "Custom Format"],
78
- label="Prompt Template Type",
79
- value="Template + Prompt"
 
80
  )
81
 
82
- game_stats_template = gr.Textbox(
83
- label="Game Statistics Template",
84
- placeholder="Enter your game statistics here (scores, round history, etc.)",
85
- lines=10
 
86
  )
87
 
88
- raw_prompt = gr.Textbox(
89
- label="Prompt",
90
- placeholder="What do you want the model to analyze or respond to?",
91
- lines=3
 
 
 
 
 
 
 
 
 
 
 
 
92
  )
93
 
 
94
  with gr.Row():
95
  max_length = gr.Slider(
96
  minimum=50,
97
- maximum=1024,
98
  value=256,
99
  step=1,
100
  label="Max Response Length"
@@ -114,40 +244,46 @@ with gr.Blocks() as demo:
114
  label="Top P"
115
  )
116
 
 
117
  submit_btn = gr.Button("Generate Response")
118
 
119
  with gr.Column():
 
120
  final_prompt_display = gr.Textbox(
121
  label="Final Prompt Sent to Model",
122
- lines=10
123
  )
124
  response_display = gr.Textbox(
125
  label="Model Response",
126
- lines=15
127
  )
 
 
 
 
 
 
 
 
 
 
 
128
 
 
129
  submit_btn.click(
130
  process_input,
131
  inputs=[
132
- raw_prompt,
133
- game_stats_template,
134
- template_type,
 
 
135
  max_length,
136
  temperature,
137
  top_p
138
  ],
139
  outputs=[final_prompt_display, response_display]
140
  )
141
-
142
- gr.Markdown("""
143
- ## Tips for Testing
144
-
145
- 1. Start with simple prompts to gauge the model's basic understanding
146
- 2. Gradually increase complexity to find the model's limitations
147
- 3. Try different prompt formats to see which works best
148
- 4. Experiment with temperature and top_p to find optimal settings
149
- 5. Document which prompts work well as candidates for fine-tuning
150
- """)
151
 
152
  # Launch the demo
153
  demo.launch()
 
1
  import gradio as gr
2
  import torch
3
+ import json
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
  # Load the Qwen2 0.5B model
 
13
  trust_remote_code=True
14
  )
15
 
16
+ # Predefined game data in compressed formats
17
+ PREDEFINED_GAMES = {
18
+ "rps_simple": {
19
+ "description": "Rock-Paper-Scissors (Simple Format)",
20
+ "data": {
21
+ "game_type": "rps",
22
+ "encoding": {"rock": 0, "paper": 1, "scissors": 2},
23
+ "result_encoding": {"ai_win": 0, "player_win": 1, "tie": 2},
24
+ "rounds": [
25
+ {"round": 1, "player": 0, "ai": 2, "result": 1},
26
+ {"round": 2, "player": 1, "ai": 1, "result": 2},
27
+ {"round": 3, "player": 2, "ai": 0, "result": 0},
28
+ {"round": 4, "player": 0, "ai": 0, "result": 2},
29
+ {"round": 5, "player": 1, "ai": 0, "result": 1},
30
+ {"round": 6, "player": 2, "ai": 2, "result": 2},
31
+ {"round": 7, "player": 0, "ai": 1, "result": 0},
32
+ {"round": 8, "player": 1, "ai": 2, "result": 0},
33
+ {"round": 9, "player": 2, "ai": 1, "result": 1},
34
+ {"round": 10, "player": 0, "ai": 2, "result": 1}
35
+ ],
36
+ "summary": {"player_wins": 4, "ai_wins": 3, "ties": 3}
37
+ }
38
+ },
39
+ "rps_numeric": {
40
+ "description": "Rock-Paper-Scissors (Compressed Numeric Format)",
41
+ "data": {
42
+ "rules": "RPS: 0=Rock,1=Paper,2=Scissors. Result: 0=AI_win,1=Player_win,2=Tie",
43
+ "rounds": [[1,0,2,1],[2,1,1,2],[3,2,0,0],[4,0,0,2],[5,1,0,1],[6,2,2,2],[7,0,1,0],[8,1,2,0],[9,2,1,1],[10,0,2,1]],
44
+ "score": {"P": 4, "AI": 3, "Tie": 3}
45
+ }
46
+ }
47
+ }
48
+
49
+ # Predefined prompt templates
50
+ PROMPT_TEMPLATES = {
51
+ "basic_analysis": "Who is winning right now? What patterns do you notice in the player's choices?",
52
+ "prediction": "Based on the player's past choices, predict what the player will choose in the next round. Explain your reasoning.",
53
+ "strategy": "What strategy should the AI use to improve its win rate? Provide specific recommendations.",
54
+ "pattern_analysis": "Analyze the frequency of each choice (rock, paper, scissors) made by the player. Is there a dominant pattern?",
55
+ "structured_analysis": "Provide a structured analysis with these sections: 1) Current winner, 2) Player choice patterns, 3) AI performance, 4) Recommended strategy for AI."
56
+ }
57
+
58
+ # Prompt formatters
59
+ def format_rps_simple(game_data):
60
+ """Format the RPS data in a simple way that's easy for small models to understand"""
61
+ game = game_data["data"]
62
+
63
+ # Create a mapping for move names
64
+ move_names = {0: "Rock", 1: "Paper", 2: "Scissors"}
65
+ result_names = {0: "AI wins", 1: "Player wins", 2: "Tie"}
66
+
67
+ # Initialize counters for frequency analysis
68
+ player_moves = {"Rock": 0, "Paper": 0, "Scissors": 0}
69
+
70
+ # Format each round in a simple way
71
+ formatted_data = "Game: Rock-Paper-Scissors\n"
72
+ formatted_data += "Format explanation: [Round#, Player move, AI move, Result]\n"
73
+ formatted_data += "Move codes: 0=Rock, 1=Paper, 2=Scissors\n"
74
+ formatted_data += "Result codes: 0=AI wins, 1=Player wins, 2=Tie\n\n"
75
+
76
+ formatted_data += "Game Data:\n"
77
+ for round_data in game["rounds"]:
78
+ r_num = round_data["round"]
79
+ p_move = round_data["player"]
80
+ ai_move = round_data["ai"]
81
+ result = round_data["result"]
82
+
83
+ # Update player move counter
84
+ player_moves[move_names[p_move]] += 1
85
+
86
+ # Format as [round, player, ai, result]
87
+ formatted_data += f"[{r_num}, {p_move}, {ai_move}, {result}] # R{r_num}: Player {move_names[p_move]}, AI {move_names[ai_move]}, {result_names[result]}\n"
88
+
89
+ # Add summary statistics
90
+ formatted_data += "\nSummary:\n"
91
+ formatted_data += f"Player wins: {game['summary']['player_wins']}\n"
92
+ formatted_data += f"AI wins: {game['summary']['ai_wins']}\n"
93
+ formatted_data += f"Ties: {game['summary']['ties']}\n\n"
94
+
95
+ # Add player move frequencies
96
+ formatted_data += "Player move frequencies:\n"
97
+ for move, count in player_moves.items():
98
+ formatted_data += f"{move}: {count} times ({count*10}%)\n"
99
+
100
+ return formatted_data
101
+
102
+ def format_rps_numeric(game_data):
103
+ """Format the RPS data in a highly compressed numeric format"""
104
+ game = game_data["data"]
105
+
106
+ formatted_data = "RPS Game Data (compressed format)\n"
107
+ formatted_data += f"Rules: {game['rules']}\n\n"
108
+
109
+ # Format all rounds on a single line
110
+ rounds_str = ",".join([str(r) for r in game['rounds']])
111
+ formatted_data += f"Rounds: {rounds_str}\n\n"
112
+
113
+ # Add score summary
114
+ formatted_data += f"Score: Player={game['score']['P']} AI={game['score']['AI']} Ties={game['score']['Tie']}\n"
115
+
116
+ return formatted_data
117
+
118
+ # Format selectors
119
+ FORMAT_FUNCTIONS = {
120
+ "rps_simple": format_rps_simple,
121
+ "rps_numeric": format_rps_numeric
122
+ }
123
+
124
  def generate_response(prompt, max_length=512, temperature=0.7, top_p=0.9):
125
  """Generate a response from the Qwen2 model based on the input prompt."""
126
 
 
147
  return response.strip()
148
 
149
  def process_input(
150
+ game_format,
151
+ prompt_template,
152
+ custom_prompt,
153
+ use_custom_prompt,
154
+ system_prompt,
155
  max_length,
156
  temperature,
157
  top_p
158
  ):
159
+ """Process the input and generate a response from the model."""
160
 
161
+ # Get the selected game data and format it
162
+ game_data = PREDEFINED_GAMES[game_format]
163
+ formatted_game_data = FORMAT_FUNCTIONS[game_format](game_data)
164
 
165
+ # Determine which prompt to use
166
+ prompt_text = custom_prompt if use_custom_prompt else PROMPT_TEMPLATES[prompt_template]
167
+
168
+ # Create the final prompt with optional system prompt
169
+ if system_prompt:
170
+ final_prompt = f"{system_prompt}\n\n{formatted_game_data}\n\n{prompt_text}"
171
+ else:
172
+ final_prompt = f"{formatted_game_data}\n\n{prompt_text}"
173
 
174
  # Generate response from the model
175
  response = generate_response(
 
184
  # Create the Gradio interface
185
  with gr.Blocks() as demo:
186
  gr.Markdown("# Qwen2 0.5B Game Analysis Tester")
187
+ gr.Markdown("Test how the Qwen2 0.5B model responds to different game data formats and prompts")
188
 
189
  with gr.Row():
190
  with gr.Column():
191
+ # Game data selection
192
+ game_format = gr.Dropdown(
193
+ choices=list(PREDEFINED_GAMES.keys()),
194
+ value="rps_simple",
195
+ label="Game Data Format"
196
  )
197
 
198
+ # System prompt (optional)
199
+ system_prompt = gr.Textbox(
200
+ label="System Prompt (Optional)",
201
+ placeholder="e.g., You are an expert game analyzer. Your task is to analyze game patterns and provide insights.",
202
+ lines=2
203
  )
204
 
205
+ # Prompt selection
206
+ with gr.Row():
207
+ prompt_template = gr.Dropdown(
208
+ choices=list(PROMPT_TEMPLATES.keys()),
209
+ value="basic_analysis",
210
+ label="Prompt Template"
211
+ )
212
+ use_custom_prompt = gr.Checkbox(
213
+ label="Use Custom Prompt",
214
+ value=False
215
+ )
216
+
217
+ custom_prompt = gr.Textbox(
218
+ label="Custom Prompt (if enabled above)",
219
+ placeholder="Enter your custom prompt here",
220
+ lines=2
221
  )
222
 
223
+ # Generation parameters
224
  with gr.Row():
225
  max_length = gr.Slider(
226
  minimum=50,
227
+ maximum=512,
228
  value=256,
229
  step=1,
230
  label="Max Response Length"
 
244
  label="Top P"
245
  )
246
 
247
+ # Generate button
248
  submit_btn = gr.Button("Generate Response")
249
 
250
  with gr.Column():
251
+ # Display final prompt and model response
252
  final_prompt_display = gr.Textbox(
253
  label="Final Prompt Sent to Model",
254
+ lines=12
255
  )
256
  response_display = gr.Textbox(
257
  label="Model Response",
258
+ lines=12
259
  )
260
+
261
+ # Tips for using the interface
262
+ gr.Markdown("""
263
+ ## Testing Tips
264
+
265
+ - The **Game Data Format** determines how game information is presented to the model
266
+ - The **System Prompt** can be used to provide context or role instructions
267
+ - **Prompt Templates** offer pre-made queries, or you can use a custom prompt
268
+ - Experiment with **Temperature** (higher = more creative/random, lower = more focused)
269
+ - Document successful prompts for fine-tuning datasets
270
+ """)
271
 
272
+ # Handle button click
273
  submit_btn.click(
274
  process_input,
275
  inputs=[
276
+ game_format,
277
+ prompt_template,
278
+ custom_prompt,
279
+ use_custom_prompt,
280
+ system_prompt,
281
  max_length,
282
  temperature,
283
  top_p
284
  ],
285
  outputs=[final_prompt_display, response_display]
286
  )
 
 
 
 
 
 
 
 
 
 
287
 
288
  # Launch the demo
289
  demo.launch()