tistabaulopez commited on
Commit
437e3d1
verified
1 Parent(s): 2f6133e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -126
app.py CHANGED
@@ -1,128 +1,3 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- import torch
4
- from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
5
-
6
- """
7
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
8
- """
9
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
-
11
- def respond(
12
- message,
13
- history: list[tuple[str, str]],
14
- system_message,
15
- max_tokens,
16
- temperature,
17
- top_p,
18
- ):
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- for val in history:
22
- if val[0]:
23
- messages.append({"role": "user", "content": val[0]})
24
- if val[1]:
25
- messages.append({"role": "assistant", "content": val[1]})
26
-
27
- messages.append({"role": "user", "content": message})
28
-
29
- response = ""
30
-
31
- for message in client.chat_completion(
32
- messages,
33
- max_tokens=max_tokens,
34
- stream=True,
35
- temperature=temperature,
36
- top_p=top_p,
37
- ):
38
- token = message.choices[0].delta.content
39
-
40
- response += token
41
- yield response
42
-
43
- demo = gr.ChatInterface(
44
- respond,
45
- additional_inputs=[
46
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
47
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
48
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
49
- gr.Slider(
50
- minimum=0.1,
51
- maximum=1.0,
52
- value=0.95,
53
- step=0.05,
54
- label="Top-p (nucleus sampling)",
55
- ),
56
- ],
57
- )
58
-
59
- if __name__ == "__main__":
60
- demo.launch()
61
-
62
- import gradio as gr
63
- from huggingface_hub import InferenceClient
64
- import torch
65
- from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
66
- # Crear la funci贸n de loop automatizado
67
- def experiment_loop(initial_question, max_cycles=10):
68
- prompt = f"<thinking>{initial_question}</thinking>"
69
- effectiveness = 100 # Inicializa el porcentaje de efectividad
70
- communication = "Initializing experiment."
71
- response_log = []
72
-
73
- for cycle in range(max_cycles):
74
- # Generar la respuesta del modelo
75
- inputs = tokenizer(prompt, return_tensors="pt").input_ids
76
- outputs = model.generate(inputs, max_length=200)
77
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
78
-
79
- # Descomponer la respuesta en afirmaci贸n y nueva pregunta
80
- affirmation = extract_affirmation(response)
81
- new_question = extract_question(response)
82
-
83
- # Actualizar el estado de la efectividad
84
- effectiveness = min(1000, effectiveness + 10 * cycle) # Ejemplo de aumento de efectividad
85
-
86
- # Comunicaci贸n con el usuario
87
- communication = f"Cycle {cycle + 1}: Affirmation: '{affirmation}' | New Question: '{new_question}'"
88
-
89
- # Guardar el ciclo actual en el log
90
- response_log.append((affirmation, new_question, effectiveness, communication))
91
-
92
- # Verificar si el modelo decide detenerse
93
- if "Descanso" in response:
94
- final_output = generate_final_output(response_log)
95
- return final_output
96
-
97
- # Actualizar el prompt con la nueva afirmaci贸n y pregunta
98
- prompt = f"<thinking>{affirmation} {new_question}</thinking>"
99
-
100
- # Si se alcanza el n煤mero m谩ximo de ciclos sin detenerse
101
- final_output = generate_final_output(response_log)
102
- return final_output
103
-
104
- # Funciones auxiliares para extraer afirmaciones, preguntas y generar la salida final
105
- def extract_affirmation(response):
106
- return response.split('.')[0]
107
-
108
- def extract_question(response):
109
- return response.split('?')[-2].strip() + "?"
110
-
111
- def generate_final_output(log):
112
- final_affirmation = log[-1][0]
113
- final_question = log[-1][1]
114
- final_communication = f"Experiment completed. Final Affirmation: '{final_affirmation}' | Final Question: '{final_question}'"
115
- return final_communication
116
- # Iniciar el experimento despu茅s de que la funci贸n ha sido definida
117
- initial_question = "What happens in the space between a response and its recreation?"
118
- result = experiment_loop(initial_question)
119
- print(result)
120
-
121
-
122
- # Define the experiment loop
123
- initial_question = "What happens in the space between a response and its recreation?"
124
- result = experiment_loop(initial_question)
125
- print(result)
126
  import torch
127
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
128
 
@@ -187,4 +62,4 @@ def generate_final_output(log):
187
  # Iniciar el experimento
188
  initial_question = "What happens in the space between a response and its recreation?"
189
  result = experiment_loop(initial_question)
190
- print(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
3
 
 
62
  # Iniciar el experimento
63
  initial_question = "What happens in the space between a response and its recreation?"
64
  result = experiment_loop(initial_question)
65
+ print(result)