szili2011 commited on
Commit
2081e9d
·
verified ·
1 Parent(s): 6f11ba2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -35
app.py CHANGED
@@ -1,45 +1,34 @@
1
- # app.py
2
  import gradio as gr
3
  import tensorflow as tf
4
  import pickle
5
  import numpy as np
6
- import os
7
 
8
  # --- 1. CONFIGURATION & MODEL LOADING ---
9
- # This section loads your trained AI models and the tokenizers needed to understand text.
10
- MAX_SEQ_LENGTH = 30 # Must match the value used during training!
11
-
12
  print("Loading models and tokenizers...")
13
  try:
14
- # Load the "Go Larger" model and its vocabulary
15
  successor_model = tf.keras.models.load_model('successor_model.h5')
16
  with open('successor_model_tokenizers.pkl', 'rb') as f:
17
  successor_tokenizers = pickle.load(f)
18
 
19
- # Load the "Go Smaller" model and its vocabulary
20
  predecessor_model = tf.keras.models.load_model('predecessor_model.h5')
21
  with open('predecessor_model_tokenizers.pkl', 'rb') as f:
22
  predecessor_tokenizers = pickle.load(f)
23
 
24
  print("Models and tokenizers loaded successfully.")
25
  except Exception as e:
26
- # This helps debug issues on Hugging Face Spaces if a file is missing
27
  print(f"FATAL ERROR loading files: {e}")
28
  successor_model, predecessor_model = None, None
29
 
30
  # --- 2. THE CORE PREDICTION LOGIC ---
31
- # This function is the "brain" of the application.
32
  def predict_next_state(direction, current_unit, current_analogy, current_commentary):
33
- # Safety check in case models failed to load
34
  if not all([successor_model, predecessor_model]):
35
  return "Error: Models are not loaded.", "Please check the server logs on Hugging Face.", "---"
36
 
37
- # A. Select the correct AI model and tokenizers based on user's click
38
  model = successor_model if direction == "larger" else predecessor_model
39
  tokenizers = successor_tokenizers if direction == "larger" else predecessor_tokenizers
40
 
41
- # B. Prepare the input data for the model
42
- # The input text must be converted to numbers exactly as it was during training.
43
  input_data = {
44
  'current_unit_name': [current_unit],
45
  'current_analogy': [current_analogy],
@@ -52,32 +41,25 @@ def predict_next_state(direction, current_unit, current_analogy, current_comment
52
  padded_sequences = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=MAX_SEQ_LENGTH, padding='post')
53
  processed_input[col] = padded_sequences
54
 
55
- # C. Get the AI's prediction
56
  predictions = model.predict(processed_input)
57
 
58
- # D. Decode the prediction from numbers back to human-readable text
59
  target_texts = {}
60
  output_cols = ['target_unit_name', 'target_analogy', 'target_commentary']
61
 
62
  for i, col in enumerate(output_cols):
63
- # The model outputs probabilities; we take the most likely token (word) at each step.
64
  pred_indices = np.argmax(predictions[i], axis=-1)
65
- # Use the tokenizer to convert the sequence of indices back into a sentence.
66
  predicted_sequence = tokenizers[col].sequences_to_texts(pred_indices)[0]
67
- # Clean up padding and unknown words
68
- target_texts[col] = predicted_sequence.replace('<oov>', '').replace(' end', '').strip()
 
69
 
70
- # E. Handle the "Infinity" Sentinel
71
- # Check if the AI returned our special signal.
72
  if "end of knowledge" in target_texts['target_unit_name'].lower():
73
- # If so, switch to the simple rule-based procedural engine.
74
  prefix = "Giga-" if direction == "larger" else "pico-"
75
  new_unit = f"{prefix}{current_unit}"
76
  new_analogy = "A procedurally generated unit beyond the AI's known universe."
77
  new_commentary = "This represents a step into true infinity, where rules replace learned knowledge."
78
  return new_unit, new_analogy, new_commentary
79
  else:
80
- # Otherwise, return the AI's generated response.
81
  return target_texts['target_unit_name'], target_texts['target_analogy'], target_texts['target_commentary']
82
 
83
  # Wrapper functions for the buttons
@@ -88,40 +70,34 @@ def go_smaller(unit, analogy, commentary):
88
  return predict_next_state("smaller", unit, analogy, commentary)
89
 
90
  # --- 3. THE GRADIO USER INTERFACE ---
91
- # This section defines the layout and interactivity of the web page.
92
  initial_unit = "Byte"
93
- initial_analogy = "A single character of text, like 'R'"
94
  initial_commentary = "From binary choices, a building block is formed, ready to hold a single, recognizable symbol."
95
 
96
- # Use gr.Blocks for a custom layout
97
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky")) as demo:
98
  gr.Markdown("# 🤖 Digital Scale Explorer AI")
99
  gr.Markdown("An AI trained from scratch to explore the infinite ladder of data sizes. Click the buttons to traverse the universe of data!")
100
 
101
  with gr.Row():
102
- # Define the output text boxes
103
- unit_name_out = gr.Textbox(value=initial_unit, label="Unit Name", interactive=False, elem_id="unit_name_style")
104
- analogy_out = gr.Textbox(value=initial_analogy, label="Analogy", lines=4, interactive=False, elem_id="analogy_style")
105
- commentary_out = gr.Textbox(value=initial_commentary, label="AI Commentary", lines=3, interactive=False, elem_id="commentary_style")
106
 
107
  with gr.Row():
108
- # Define the buttons
109
  smaller_btn = gr.Button("Go Smaller ⬇️", variant="secondary", size="lg")
110
  larger_btn = gr.Button("Go Larger ⬆️", variant="primary", size="lg")
111
 
112
- # Connect the "Go Larger" button to its function
113
  larger_btn.click(
114
  fn=go_larger,
115
  inputs=[unit_name_out, analogy_out, commentary_out],
116
  outputs=[unit_name_out, analogy_out, commentary_out]
117
  )
118
- # Connect the "Go Smaller" button to its function
119
  smaller_btn.click(
120
- fn=go_smaller,
121
  inputs=[unit_name_out, analogy_out, commentary_out],
122
  outputs=[unit_name_out, analogy_out, commentary_out]
123
  )
124
 
125
- # Launch the app when the script is run
126
  if __name__ == "__main__":
127
  demo.launch()
 
1
+ # app.py (Corrected Version)
2
  import gradio as gr
3
  import tensorflow as tf
4
  import pickle
5
  import numpy as np
 
6
 
7
  # --- 1. CONFIGURATION & MODEL LOADING ---
8
+ MAX_SEQ_LENGTH = 30
 
 
9
  print("Loading models and tokenizers...")
10
  try:
 
11
  successor_model = tf.keras.models.load_model('successor_model.h5')
12
  with open('successor_model_tokenizers.pkl', 'rb') as f:
13
  successor_tokenizers = pickle.load(f)
14
 
 
15
  predecessor_model = tf.keras.models.load_model('predecessor_model.h5')
16
  with open('predecessor_model_tokenizers.pkl', 'rb') as f:
17
  predecessor_tokenizers = pickle.load(f)
18
 
19
  print("Models and tokenizers loaded successfully.")
20
  except Exception as e:
 
21
  print(f"FATAL ERROR loading files: {e}")
22
  successor_model, predecessor_model = None, None
23
 
24
  # --- 2. THE CORE PREDICTION LOGIC ---
 
25
  def predict_next_state(direction, current_unit, current_analogy, current_commentary):
 
26
  if not all([successor_model, predecessor_model]):
27
  return "Error: Models are not loaded.", "Please check the server logs on Hugging Face.", "---"
28
 
 
29
  model = successor_model if direction == "larger" else predecessor_model
30
  tokenizers = successor_tokenizers if direction == "larger" else predecessor_tokenizers
31
 
 
 
32
  input_data = {
33
  'current_unit_name': [current_unit],
34
  'current_analogy': [current_analogy],
 
41
  padded_sequences = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=MAX_SEQ_LENGTH, padding='post')
42
  processed_input[col] = padded_sequences
43
 
 
44
  predictions = model.predict(processed_input)
45
 
 
46
  target_texts = {}
47
  output_cols = ['target_unit_name', 'target_analogy', 'target_commentary']
48
 
49
  for i, col in enumerate(output_cols):
 
50
  pred_indices = np.argmax(predictions[i], axis=-1)
 
51
  predicted_sequence = tokenizers[col].sequences_to_texts(pred_indices)[0]
52
+ # More robust cleaning
53
+ clean_text = ' '.join([word for word in predicted_sequence.split() if word not in ['<oov>', 'end']])
54
+ target_texts[col] = clean_text.strip()
55
 
 
 
56
  if "end of knowledge" in target_texts['target_unit_name'].lower():
 
57
  prefix = "Giga-" if direction == "larger" else "pico-"
58
  new_unit = f"{prefix}{current_unit}"
59
  new_analogy = "A procedurally generated unit beyond the AI's known universe."
60
  new_commentary = "This represents a step into true infinity, where rules replace learned knowledge."
61
  return new_unit, new_analogy, new_commentary
62
  else:
 
63
  return target_texts['target_unit_name'], target_texts['target_analogy'], target_texts['target_commentary']
64
 
65
  # Wrapper functions for the buttons
 
70
  return predict_next_state("smaller", unit, analogy, commentary)
71
 
72
  # --- 3. THE GRADIO USER INTERFACE ---
 
73
  initial_unit = "Byte"
74
+ initial_analogy = "a single character of text, like 'R'"
75
  initial_commentary = "From binary choices, a building block is formed, ready to hold a single, recognizable symbol."
76
 
 
77
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky")) as demo:
78
  gr.Markdown("# 🤖 Digital Scale Explorer AI")
79
  gr.Markdown("An AI trained from scratch to explore the infinite ladder of data sizes. Click the buttons to traverse the universe of data!")
80
 
81
  with gr.Row():
82
+ unit_name_out = gr.Textbox(value=initial_unit, label="Unit Name", interactive=False)
83
+ analogy_out = gr.Textbox(value=initial_analogy, label="Analogy", lines=4, interactive=False)
84
+ commentary_out = gr.Textbox(value=initial_commentary, label="AI Commentary", lines=3, interactive=False)
 
85
 
86
  with gr.Row():
 
87
  smaller_btn = gr.Button("Go Smaller ⬇️", variant="secondary", size="lg")
88
  larger_btn = gr.Button("Go Larger ⬆️", variant="primary", size="lg")
89
 
 
90
  larger_btn.click(
91
  fn=go_larger,
92
  inputs=[unit_name_out, analogy_out, commentary_out],
93
  outputs=[unit_name_out, analogy_out, commentary_out]
94
  )
95
+
96
  smaller_btn.click(
97
+ fn=go_smaller, # Corrected from go_larger to go_smaller
98
  inputs=[unit_name_out, analogy_out, commentary_out],
99
  outputs=[unit_name_out, analogy_out, commentary_out]
100
  )
101
 
 
102
  if __name__ == "__main__":
103
  demo.launch()