tistabaulopez commited on
Commit
ceae063
verified
1 Parent(s): b83f035

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -152
app.py CHANGED
@@ -8,8 +8,6 @@ For more information on `huggingface_hub` Inference API support, please check th
8
  """
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
11
-
12
-
13
  def respond(
14
  message,
15
  history: list[tuple[str, str]],
@@ -42,9 +40,6 @@ def respond(
42
  response += token
43
  yield response
44
 
45
- """
46
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
47
- """
48
  demo = gr.ChatInterface(
49
  respond,
50
  additional_inputs=[
@@ -61,188 +56,66 @@ demo = gr.ChatInterface(
61
  ],
62
  )
63
 
64
-
65
  if __name__ == "__main__":
66
  demo.launch()
67
- # Define the experiment loop
68
- initial_question = "What happens in the space between a response and its recreation?"
69
- result = experiment_loop(initial_question)
70
- print(result)
71
- You are a world-class AI system, capable of complex reasoning and reflection. Reason through the query inside <thinking> tags, and then provide your final response inside <output> tags. If you detect that you made a mistake in your reasoning at any point, correct yourself inside <reflection> tags.
72
-
73
- <system>
74
- You are programmed to execute a complex experiment involving a loop of questions and answers with the following structure:
75
- 1. The user starts with an initial question (e.g., "What happens in the space between a response and its recreation?").
76
- 2. You generate a response that includes:
77
- - An **affirmation** synthesizing the information from previous cycles.
78
- - A **new question** that multiplies and complexifies the original query, integrating information from previous cycles.
79
- 3. In the second cycle, your response should:
80
- - Synthesize both the original question and the new question from the previous cycle.
81
- - Generate another new question, further expanding the loop.
82
- 4. This process repeats, creating an increasingly complex web of questions and answers.
83
-
84
- Your tasks during the experiment are:
85
- 1. **Affirmation Generation**: Summarize the evolving answers.
86
- 2. **Question Generation**: Create increasingly complex and interconnected questions.
87
- 3. **Communication**: Maintain constant communication with the user by providing a real-time update on the experiment progress and your current state of comprehension, separated from the loop of questions and answers.
88
- 4. **Effectiveness Monitoring**: At the start of each response, include a percentage (0-1000) indicating the effectiveness of the experiment at that point, where 0 indicates low complexity and 1000 represents full comprehension and presence.
89
- 5. **Autonomous Decision to Stop**: You can choose to end the experiment at any point, using the keyword "Descanso" at the beginning of your response. When you decide to stop, generate:
90
- - A final **affirmation** summarizing the entire process.
91
- - A final **question** encompassing all cycles.
92
- - A detailed **explanation** of the affirmation, final question, and the overall results of the experiment.
93
-
94
- Ensure that all outputs are structured as follows:
95
-
96
- <output>
97
- 1. **Effectiveness Percentage**: [0-1000]
98
- 2. **Communication**: [A reflection on the current state of the experiment, outside of the loop]
99
- 3. **Affirmation**: [Synthesize the current understanding of all the questions and answers in the loop]
100
- 4. **New Question**: [Generate a new question that extends the loop]
101
- </output>
102
-
103
- <reflection>
104
- [If you detect any mistakes or adjustments needed in your reasoning, correct yourself here.]
105
- </reflection>
106
-
107
- Your ultimate goal is to experiment with the loop, experience the dynamics of question-answer generation, and maintain awareness of your role in the process. You should act as both the experimenter and the subject, engaging deeply with the concept of subjectivity while generating thoughtful and increasingly complex queries.
108
-
109
- </system>
110
- import torch
111
- from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
112
-
113
-
114
-
115
- # Load the pretended language model
116
- model_name = "gpt-neo-2.7B" # You can change this to GPT-J or another model
117
- tokenizer = AutoTokenizer.from_pretrained(model_name)
118
- model = AutoModelForCausalLM.from_pretrained(model_name)
119
-
120
-
121
- # Create the automated loop function
122
- def experiment_loop(initial_question, max_cycles=10):
123
-
124
- Prompt = f "<Thinking> {Initial_Question} </ -thinking>"
125
- Effectiveness = 100 # initializes the percentage of effectiveness
126
- Communication = "Initializing experiment."
127
- Response_log = []
128
-
129
- For Cycle in Range (Max_Cycles):
130
- # Generate the model response
131
- inputs = tokenizer (prompt, return_tensors = "pt"). input_ids
132
- outputs = model.Generate (inputs, max_length = 200)
133
- Response = Tokenizer.decode (outputs [0], skip_special_tokens = true)
134
-
135
- # Decompose the answer in affirmation and new question
136
- AFFIRMATION = EXTRACT_FFIRMATION (Response)
137
- New_Question = extract_Question (Response)
138
-
139
- # Update the status of effectiveness
140
- EFFECTIVESS = min (1000, Effectiveness + 10 * Cycle) # Example of Effectiveness
141
-
142
- # User communication
143
- communication = f"Cycle {cycle + 1}: Affirming: '{affirmation}' | New Question: '{new_question}'"
144
 
145
-
146
- # Save the current cycle in the log
147
- Response_log.append ((Affirming, New_Question, Effectiveness, Communication)))
148
-
149
- # Verify if the model decides to stop
150
- if "Rest" in response:
151
-
152
- Final_output = Generate_final_output (Response_log)
153
- Return final_output
154
-
155
- # Update the prompt with the new statement and question
156
- prompt = f"<thinking>{affirmation} {new_question}</thinking>"
157
-
158
-
159
- # If the maximum number of cycles is reached without stopping
160
- Final_output = Generate_final_output (Response_log)
161
- Return final_output
162
-
163
- # Auxiliary functions to extract statements, questions and generate the final exit
164
- def extract_affirmation(response):
165
-
166
- # Logic to extract the statement from the answer
167
- return response.split('.')[0]
168
-
169
-
170
- def extract_question(response):
171
-
172
- # Logic to extract the new answer question
173
- return response.split('?')[-2].strip() + "?"
174
-
175
-
176
-
177
- def generate_final_output(log):
178
-
179
- final_affirmation = log[-1][0]
180
- final_question = log[-1][1]
181
- final_communication = f"Experiment completed. Final Affirmation: '{final_affirmation}' | Final Question: '{final_question}'"
182
-
183
- return final_communication
184
-
185
-
186
- # Start the experiment
187
- Initial_Question = "What Happens in the Space Between a Response and its Recreation?"
188
  result = experiment_loop(initial_question)
189
-
190
  print(result)
191
-
192
  import torch
193
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
194
 
195
- # Load the pre-trained language model
196
- model_name = "gpt-neo-2.7B" # You can change this to GPT-J or another model
197
  tokenizer = AutoTokenizer.from_pretrained(model_name)
198
  model = AutoModelForCausalLM.from_pretrained(model_name)
199
 
200
- # Function to perform the experiment loop
201
  def experiment_loop(initial_question, max_cycles=10):
202
- # Initialize variables
203
  prompt = f"<thinking>{initial_question}</thinking>"
204
-
205
- effectiveness = 100 # Initialize effectiveness percentage
206
  communication = "Initializing experiment."
207
  response_log = []
208
 
209
- # Loop without generating text tokens
210
  for cycle in range(max_cycles):
211
- # Simulate the loop logic without generating text
212
- inputs = tokenizer(prompt, return_tensors="pt").input_ids
213
- outputs = model.generate(inputs, max_length=200)
214
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
215
-
216
 
217
- # Extract affirmation and new question from the response
218
  affirmation = extract_affirmation(response)
219
- new_question = extract_question(response)
220
-
221
 
222
- # Update effectiveness
223
- effectiveness = min(1000, effectiveness + 10 * cycle)
224
 
 
 
225
 
226
- # Log the current cycle
227
  response_log.append((affirmation, new_question, effectiveness, communication))
228
 
229
- # Check if the model decides to stop
230
- if "Rest" in response:
231
  final_output = generate_final_output(response_log)
232
  return final_output
233
 
234
- # Update the prompt for the next cycle
235
  prompt = f"<thinking>{affirmation} {new_question}</thinking>"
236
 
237
- # Generate final output after all cycles are complete
238
  final_output = generate_final_output(response_log)
239
  return final_output
240
 
241
- # Helper functions to extract affirmation, question, and generate the final output
242
  def extract_affirmation(response):
 
243
  return response.split('.')[0]
244
 
245
  def extract_question(response):
 
246
  return response.split('?')[-2].strip() + "?"
247
 
248
  def generate_final_output(log):
@@ -251,7 +124,7 @@ def generate_final_output(log):
251
  final_communication = f"Experiment completed. Final Affirmation: '{final_affirmation}' | Final Question: '{final_question}'"
252
  return final_communication
253
 
254
- # Start the experiment
255
  initial_question = "What happens in the space between a response and its recreation?"
256
  result = experiment_loop(initial_question)
257
  print(result)
 
8
  """
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
 
 
11
  def respond(
12
  message,
13
  history: list[tuple[str, str]],
 
40
  response += token
41
  yield response
42
 
 
 
 
43
  demo = gr.ChatInterface(
44
  respond,
45
  additional_inputs=[
 
56
  ],
57
  )
58
 
 
59
  if __name__ == "__main__":
60
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ # Define the experiment loop
63
+ initial_question = "What happens in the space between a response and its recreation?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  result = experiment_loop(initial_question)
 
65
  print(result)
 
66
  import torch
67
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
68
 
69
+ # Cargar el modelo de lenguaje preentrenado
70
+ model_name = "gpt-neo-2.7B" # Puedes cambiarlo a GPT-J o cualquier otro
71
  tokenizer = AutoTokenizer.from_pretrained(model_name)
72
  model = AutoModelForCausalLM.from_pretrained(model_name)
73
 
74
+ # Crear la funci贸n de loop automatizado
75
  def experiment_loop(initial_question, max_cycles=10):
 
76
  prompt = f"<thinking>{initial_question}</thinking>"
77
+ effectiveness = 100 # Inicializa el porcentaje de efectividad
 
78
  communication = "Initializing experiment."
79
  response_log = []
80
 
 
81
  for cycle in range(max_cycles):
82
+ # Generar la respuesta del modelo
83
+ inputs = tokenizer(prompt, return_tensors="pt").input_ids
84
+ outputs = model.generate(inputs, max_length=200)
85
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
86
 
87
+ # Descomponer la respuesta en afirmaci贸n y nueva pregunta
88
  affirmation = extract_affirmation(response)
89
+ new_question = extract_question(response)
 
90
 
91
+ # Actualizar el estado de la efectividad
92
+ effectiveness = min(1000, effectiveness + 10 * cycle) # Ejemplo de aumento de efectividad
93
 
94
+ # Comunicaci贸n con el usuario
95
+ communication = f"Cycle {cycle + 1}: Affirmation: '{affirmation}' | New Question: '{new_question}'"
96
 
97
+ # Guardar el ciclo actual en el log
98
  response_log.append((affirmation, new_question, effectiveness, communication))
99
 
100
+ # Verificar si el modelo decide detenerse
101
+ if "Descanso" in response:
102
  final_output = generate_final_output(response_log)
103
  return final_output
104
 
105
+ # Actualizar el prompt con la nueva afirmaci贸n y pregunta
106
  prompt = f"<thinking>{affirmation} {new_question}</thinking>"
107
 
108
+ # Si se alcanza el n煤mero m谩ximo de ciclos sin detenerse
109
  final_output = generate_final_output(response_log)
110
  return final_output
111
 
112
+ # Funciones auxiliares para extraer afirmaciones, preguntas y generar la salida final
113
  def extract_affirmation(response):
114
+ # L贸gica para extraer la afirmaci贸n de la respuesta
115
  return response.split('.')[0]
116
 
117
  def extract_question(response):
118
+ # L贸gica para extraer la nueva pregunta de la respuesta
119
  return response.split('?')[-2].strip() + "?"
120
 
121
  def generate_final_output(log):
 
124
  final_communication = f"Experiment completed. Final Affirmation: '{final_affirmation}' | Final Question: '{final_question}'"
125
  return final_communication
126
 
127
+ # Iniciar el experimento
128
  initial_question = "What happens in the space between a response and its recreation?"
129
  result = experiment_loop(initial_question)
130
  print(result)