Spaces:
Runtime error
Runtime error
riabayonaor
commited on
Commit
•
f76f709
1
Parent(s):
080fa4c
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,15 @@
|
|
1 |
#10
|
2 |
-
import
|
3 |
-
from huggingface_hub import InferenceClient
|
4 |
-
import os
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
raise ValueError("Token de Hugging Face no encontrado.")
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
"google/gemma-7b-it",
|
18 |
-
"google/gemma-2b",
|
19 |
-
"google/gemma-2b-it"
|
20 |
-
]
|
21 |
|
22 |
-
|
23 |
-
model = models[int(client_choice)] # Obtener el ID del modelo basado en la elección del usuario
|
24 |
-
try:
|
25 |
-
parameters = {
|
26 |
-
"temperature": temp,
|
27 |
-
"max_new_tokens": tokens,
|
28 |
-
"top_p": top_p,
|
29 |
-
"repetition_penalty": rep_p,
|
30 |
-
"seed": seed,
|
31 |
-
}
|
32 |
-
formatted_prompt = f"{prompt}"
|
33 |
-
# Realiza la solicitud de inferencia especificando el model_id directamente aquí
|
34 |
-
response = client(inputs=formatted_prompt, parameters=parameters, model_id=model, wait_for_model=True)
|
35 |
-
|
36 |
-
output = response[0]["generated_text"] if response else "No response."
|
37 |
-
new_history = history + [(prompt, output)]
|
38 |
-
return new_history, memory
|
39 |
-
except Exception as e:
|
40 |
-
error_message = f"An error occurred: {str(e)}"
|
41 |
-
new_history = history + [(prompt, error_message)]
|
42 |
-
return new_history, memory
|
43 |
-
|
44 |
-
with gr.Blocks() as app:
|
45 |
-
history = gr.State(default=[])
|
46 |
-
memory = gr.State(default=[])
|
47 |
-
|
48 |
-
with gr.Row():
|
49 |
-
inp = gr.Textbox(label="Prompt")
|
50 |
-
client_choice = gr.Dropdown(label="Choose Model", choices={name: i for i, name in enumerate(models)}, value=0)
|
51 |
-
temp = gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, value=0.7)
|
52 |
-
tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=512, value=100)
|
53 |
-
top_p = gr.Slider(label="Top-P", minimum=0.1, maximum=1.0, value=0.9)
|
54 |
-
rep_p = gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, value=1.1)
|
55 |
-
seed = gr.Number(label="Seed", value=42)
|
56 |
-
chat_mem = gr.Slider(label="Chat Memory", minimum=1, maximum=10, value=3)
|
57 |
-
cust_p = gr.Textbox(label="Custom Prompt", value="<start_of_turn>user{prompt}<end_of_turn><start_of_turn>model", visible=False)
|
58 |
-
generate_button = gr.Button("Generate")
|
59 |
-
|
60 |
-
chat = gr.Chatbot()
|
61 |
-
|
62 |
-
generate_button.click(
|
63 |
-
fn=chat_inf,
|
64 |
-
inputs=[inp, history, memory, client_choice, seed, temp, tokens, top_p, rep_p, chat_mem, cust_p],
|
65 |
-
outputs=[chat, memory]
|
66 |
-
)
|
67 |
-
|
68 |
-
app.launch()
|
|
|
1 |
#10
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
|
|
|
3 |
|
4 |
+
# Cargar el tokenizer y el modelo
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("PlanTL-GOB-ES/roberta-base-bne")
|
6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("PlanTL-GOB-ES/roberta-base-bne")
|
|
|
7 |
|
8 |
+
# Inicializar la pipeline de generación de texto
|
9 |
+
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
10 |
|
11 |
+
# Generar una respuesta a una pregunta en español
|
12 |
+
question = "¿Cuál es la capital de España?" # Ejemplo de pregunta
|
13 |
+
response = text_generator(question, max_length=50, do_sample=True)
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
print(response[0]['generated_text'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|