Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -18,7 +18,6 @@ print("Model loaded successfully.")
|
|
18 |
|
19 |
|
20 |
# --- Generation Function (Returns response and token count) ---
|
21 |
-
# No changes needed here
|
22 |
def generate_response(messages, max_length=512, temperature=0.7, top_p=0.9):
|
23 |
"""Generate a response and return it along with the number of generated tokens."""
|
24 |
num_generated_tokens = 0
|
@@ -59,10 +58,7 @@ def generate_response(messages, max_length=512, temperature=0.7, top_p=0.9):
|
|
59 |
def process_input(
|
60 |
analysis_mode, # Mode selector
|
61 |
player_stats,
|
62 |
-
player_last_move,
|
63 |
-
markov_prediction_text,
|
64 |
system_prompt, # Single system prompt from UI
|
65 |
-
user_query,
|
66 |
max_length,
|
67 |
temperature,
|
68 |
top_p
|
@@ -72,12 +68,9 @@ def process_input(
|
|
72 |
|
73 |
# Construct user content based on mode
|
74 |
if analysis_mode == "Frequency Only":
|
75 |
-
user_content = f"Player Move Frequency Stats (Long-Term):\n{player_stats}
|
76 |
-
user_content += f"User Query:\n{user_query}"
|
77 |
elif analysis_mode == "Markov Prediction Only":
|
78 |
-
user_content =
|
79 |
-
user_content += f"Predicted Next Move (Short-Term Markov Analysis):\n{markov_prediction_text}\n\n"
|
80 |
-
user_content += f"User Query:\n{user_query}"
|
81 |
else:
|
82 |
return "Invalid analysis mode selected.", "", "0 seconds", 0
|
83 |
|
@@ -133,29 +126,11 @@ Step 4: State Final Recommendation.
|
|
133 |
Base your analysis strictly on the provided frequencies and the stated RPS rules.
|
134 |
"""
|
135 |
|
136 |
-
#
|
137 |
-
DEFAULT_SYSTEM_PROMPT_MARKOV = ""
|
138 |
-
Your ONLY task is to recommend the AI move that beats the player's PREDICTED next move. Accuracy is critical.
|
139 |
-
|
140 |
-
Input Information Provided:
|
141 |
-
- Player's Predicted Next Move (from Markov analysis): [This is the key input!]
|
142 |
-
|
143 |
-
Instructions:
|
144 |
-
1. **Identify Prediction:** State the player's PREDICTED next move (Rock, Paper, or Scissors) based *only* on the 'Predicted Next Move' input.
|
145 |
-
2. **Find Counter:** Apply the RPS rules (Paper beats Rock, Rock beats Scissors, Scissors beats Paper). Determine the single move that correctly beats the PREDICTED move from Step 1. State *only* the name of this counter move. Double-check the rules.
|
146 |
-
3. **Recommend:** Clearly state the counter move found in Step 2 as the AI's recommended move.
|
147 |
-
|
148 |
-
Example Output Format:
|
149 |
-
1. Predicted Player Move: [Predicted move name]
|
150 |
-
2. Counter Move: [Counter move name]
|
151 |
-
3. Recommendation: Play [Counter move name].
|
152 |
-
"""
|
153 |
|
154 |
# --- Default Input Values ---
|
155 |
DEFAULT_PLAYER_STATS = "Rock: 40%\nPaper: 30%\nScissors: 30%"
|
156 |
-
DEFAULT_PLAYER_LAST_MOVE = "Rock"
|
157 |
-
DEFAULT_MARKOV_PREDICTION = "Based on the last move (Rock), the player's most likely next move is Paper (60% probability)."
|
158 |
-
DEFAULT_USER_QUERY = "Based on the provided information for the selected analysis mode, what single move should the AI make next? Explain your reasoning step-by-step as instructed."
|
159 |
|
160 |
# --- Gradio Interface ---
|
161 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
@@ -186,29 +161,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
186 |
|
187 |
with gr.Group(visible=False) as markov_inputs: # Hidden by default
|
188 |
gr.Markdown("### Markov Prediction Analysis Inputs")
|
189 |
-
|
190 |
-
label="Player's Last Move", choices=["Rock", "Paper", "Scissors"], value=DEFAULT_PLAYER_LAST_MOVE,
|
191 |
-
info="The player's most recent actual move."
|
192 |
-
)
|
193 |
-
markov_prediction_input = gr.Textbox(
|
194 |
-
label="Predicted Next Move (Short-Term Markov Analysis)", value=DEFAULT_MARKOV_PREDICTION, lines=3,
|
195 |
-
info="Provide the pre-calculated prediction based on the last move (e.g., 'Player likely plays Paper (60%)')."
|
196 |
-
)
|
197 |
|
198 |
# General Inputs / Parameters / Outputs
|
199 |
with gr.Row():
|
200 |
-
|
201 |
-
user_query_input = gr.Textbox(
|
202 |
-
label="Your Query / Instruction", value=DEFAULT_USER_QUERY, lines=3,
|
203 |
-
info="Ask the specific question based on the selected mode's analysis."
|
204 |
-
)
|
205 |
-
with gr.Column(scale=1):
|
206 |
gr.Markdown("#### Generation Parameters")
|
207 |
max_length_slider = gr.Slider(minimum=50, maximum=1024, value=300, step=16, label="Max New Tokens")
|
208 |
temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.4, step=0.05, label="Temperature")
|
209 |
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top P")
|
210 |
|
211 |
-
|
212 |
submit_btn = gr.Button("Generate Response", variant="primary")
|
213 |
|
214 |
with gr.Row():
|
@@ -247,7 +209,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
247 |
return {
|
248 |
frequency_inputs: gr.update(visible=False),
|
249 |
markov_inputs: gr.update(visible=True),
|
250 |
-
system_prompt_input: gr.update(value=DEFAULT_SYSTEM_PROMPT_MARKOV) # Load Markov prompt
|
251 |
}
|
252 |
else: # Default case
|
253 |
return {
|
@@ -269,10 +231,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
269 |
inputs=[
|
270 |
analysis_mode_selector,
|
271 |
player_stats_input,
|
272 |
-
player_last_move_input,
|
273 |
-
markov_prediction_input,
|
274 |
system_prompt_input, # Pass the visible system prompt textbox
|
275 |
-
user_query_input,
|
276 |
max_length_slider,
|
277 |
temperature_slider,
|
278 |
top_p_slider
|
@@ -281,9 +240,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
281 |
final_prompt_display, response_display,
|
282 |
time_output, tokens_output
|
283 |
],
|
284 |
-
api_name="
|
285 |
)
|
286 |
|
287 |
# --- Launch the demo ---
|
288 |
if __name__ == "__main__":
|
289 |
-
demo.launch()
|
|
|
18 |
|
19 |
|
20 |
# --- Generation Function (Returns response and token count) ---
|
|
|
21 |
def generate_response(messages, max_length=512, temperature=0.7, top_p=0.9):
|
22 |
"""Generate a response and return it along with the number of generated tokens."""
|
23 |
num_generated_tokens = 0
|
|
|
58 |
def process_input(
|
59 |
analysis_mode, # Mode selector
|
60 |
player_stats,
|
|
|
|
|
61 |
system_prompt, # Single system prompt from UI
|
|
|
62 |
max_length,
|
63 |
temperature,
|
64 |
top_p
|
|
|
68 |
|
69 |
# Construct user content based on mode
|
70 |
if analysis_mode == "Frequency Only":
|
71 |
+
user_content = f"Player Move Frequency Stats (Long-Term):\n{player_stats}"
|
|
|
72 |
elif analysis_mode == "Markov Prediction Only":
|
73 |
+
user_content = "Analyze based on the system prompt instructions."
|
|
|
|
|
74 |
else:
|
75 |
return "Invalid analysis mode selected.", "", "0 seconds", 0
|
76 |
|
|
|
126 |
Base your analysis strictly on the provided frequencies and the stated RPS rules.
|
127 |
"""
|
128 |
|
129 |
+
# Empty Markov system prompt as requested
|
130 |
+
DEFAULT_SYSTEM_PROMPT_MARKOV = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
# --- Default Input Values ---
|
133 |
DEFAULT_PLAYER_STATS = "Rock: 40%\nPaper: 30%\nScissors: 30%"
|
|
|
|
|
|
|
134 |
|
135 |
# --- Gradio Interface ---
|
136 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
161 |
|
162 |
with gr.Group(visible=False) as markov_inputs: # Hidden by default
|
163 |
gr.Markdown("### Markov Prediction Analysis Inputs")
|
164 |
+
gr.Markdown("*Use the System Prompt field to directly input your Markov analysis instructions.*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
# General Inputs / Parameters / Outputs
|
167 |
with gr.Row():
|
168 |
+
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
169 |
gr.Markdown("#### Generation Parameters")
|
170 |
max_length_slider = gr.Slider(minimum=50, maximum=1024, value=300, step=16, label="Max New Tokens")
|
171 |
temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.4, step=0.05, label="Temperature")
|
172 |
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top P")
|
173 |
|
|
|
174 |
submit_btn = gr.Button("Generate Response", variant="primary")
|
175 |
|
176 |
with gr.Row():
|
|
|
209 |
return {
|
210 |
frequency_inputs: gr.update(visible=False),
|
211 |
markov_inputs: gr.update(visible=True),
|
212 |
+
system_prompt_input: gr.update(value=DEFAULT_SYSTEM_PROMPT_MARKOV) # Load empty Markov prompt
|
213 |
}
|
214 |
else: # Default case
|
215 |
return {
|
|
|
231 |
inputs=[
|
232 |
analysis_mode_selector,
|
233 |
player_stats_input,
|
|
|
|
|
234 |
system_prompt_input, # Pass the visible system prompt textbox
|
|
|
235 |
max_length_slider,
|
236 |
temperature_slider,
|
237 |
top_p_slider
|
|
|
240 |
final_prompt_display, response_display,
|
241 |
time_output, tokens_output
|
242 |
],
|
243 |
+
api_name="generate_rps_selectable_analysis_v3" # Updated api_name
|
244 |
)
|
245 |
|
246 |
# --- Launch the demo ---
|
247 |
if __name__ == "__main__":
|
248 |
+
demo.launch()
|