Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
from datasets import load_dataset
|
4 |
|
5 |
"""
|
6 |
-
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
|
7 |
"""
|
8 |
-
#
|
9 |
-
client = InferenceClient("meta-llama/Llama-3.2-
|
|
|
|
|
|
|
10 |
|
11 |
-
# Carregando o dataset RPGPT
|
12 |
-
ds = load_dataset("practical-dreamer/RPGPT_PublicDomain-ShareGPT")
|
13 |
-
|
14 |
-
# Exibindo as chaves do dataset para verificar sua estrutura
|
15 |
-
print(ds)
|
16 |
-
|
17 |
-
# Vamos ajustar o código para acessar o conteúdo correto do dataset
|
18 |
-
def get_sample_from_dataset():
|
19 |
-
# Verificando a estrutura do dataset, por exemplo, acessando a primeira entrada
|
20 |
-
sample = ds['train'][0]
|
21 |
-
|
22 |
-
# Verificando todas as chaves na amostra para entender o formato
|
23 |
-
print(sample) # Apenas para depuração, pode ser removido depois
|
24 |
-
|
25 |
-
# Exemplo de como acessar o diálogo - a chave correta pode ser algo como 'input' ou 'response'
|
26 |
-
# Isso depende da estrutura do dataset, então vamos tentar acessar as chaves corretas:
|
27 |
-
context = sample.get('input', 'No input field found in dataset sample')
|
28 |
-
response = sample.get('response', 'No response field found in dataset sample')
|
29 |
-
|
30 |
-
return context, response
|
31 |
|
32 |
def respond(
|
33 |
message,
|
@@ -37,56 +19,49 @@ def respond(
|
|
37 |
temperature,
|
38 |
top_p,
|
39 |
):
|
40 |
-
|
41 |
-
dataset_context, dataset_response = get_sample_from_dataset()
|
42 |
-
|
43 |
-
# Montando as mensagens a serem enviadas ao modelo
|
44 |
-
messages = [{"role": "system", "content": f"{system_message}\nContexto adicional do dataset:\n{dataset_context}\nResposta do dataset:\n{dataset_response}"}]
|
45 |
|
46 |
-
# Adicionando histórico de mensagens entre o usuário e o assistente
|
47 |
for val in history:
|
48 |
if val[0]:
|
49 |
messages.append({"role": "user", "content": val[0]})
|
50 |
if val[1]:
|
51 |
messages.append({"role": "assistant", "content": val[1]})
|
52 |
|
53 |
-
# Adicionando a mensagem do usuário atual
|
54 |
messages.append({"role": "user", "content": message})
|
55 |
|
56 |
-
# Enviando as mensagens ao modelo
|
57 |
response = ""
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
response = f"Erro ao gerar resposta: {str(e)}"
|
68 |
|
69 |
return response
|
70 |
|
71 |
|
72 |
"""
|
73 |
-
|
74 |
"""
|
75 |
demo = gr.ChatInterface(
|
76 |
respond,
|
77 |
additional_inputs=[
|
78 |
-
gr.Textbox(value="
|
79 |
-
gr.Slider(minimum=1, maximum=
|
80 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.6, step=0.1, label="
|
81 |
gr.Slider(
|
82 |
minimum=0.1,
|
83 |
maximum=1.0,
|
84 |
value=0.95,
|
85 |
step=0.05,
|
86 |
-
label="Top-p (
|
87 |
),
|
88 |
],
|
89 |
)
|
90 |
|
|
|
91 |
if __name__ == "__main__":
|
92 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
"""
|
5 |
+
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
|
6 |
"""
|
7 |
+
#client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
+
#client = InferenceClient("meta-llama/Llama-3.2-1B-Instruct")
|
9 |
+
#client = InferenceClient("microsoft/Phi-3.5-mini-instruct")
|
10 |
+
#client = InferenceClient("unsloth/Llama-3.2-1B-Instruct")
|
11 |
+
client = InferenceClient("mlx-community/Hermes-3-Llama-3.1-70B-8bit")
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def respond(
|
15 |
message,
|
|
|
19 |
temperature,
|
20 |
top_p,
|
21 |
):
|
22 |
+
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
|
|
23 |
|
|
|
24 |
for val in history:
|
25 |
if val[0]:
|
26 |
messages.append({"role": "user", "content": val[0]})
|
27 |
if val[1]:
|
28 |
messages.append({"role": "assistant", "content": val[1]})
|
29 |
|
|
|
30 |
messages.append({"role": "user", "content": message})
|
31 |
|
|
|
32 |
response = ""
|
33 |
+
|
34 |
+
|
35 |
+
mensagens = client.chat_completion(
|
36 |
+
messages,
|
37 |
+
max_tokens=max_tokens,
|
38 |
+
temperature=temperature,
|
39 |
+
top_p=top_p,
|
40 |
+
)
|
41 |
+
response = mensagens.choices[0].message.content
|
|
|
42 |
|
43 |
return response
|
44 |
|
45 |
|
46 |
"""
|
47 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
48 |
"""
|
49 |
demo = gr.ChatInterface(
|
50 |
respond,
|
51 |
additional_inputs=[
|
52 |
+
gr.Textbox(value="Você se chama Esquizofrenia, você é irônico e tímido", label="System message"),
|
53 |
+
gr.Slider(minimum=1, maximum=2048, value=2048, step=1, label="Max new tokens"),
|
54 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.6, step=0.1, label="Temperature"),
|
55 |
gr.Slider(
|
56 |
minimum=0.1,
|
57 |
maximum=1.0,
|
58 |
value=0.95,
|
59 |
step=0.05,
|
60 |
+
label="Top-p (nucleus sampling)",
|
61 |
),
|
62 |
],
|
63 |
)
|
64 |
|
65 |
+
|
66 |
if __name__ == "__main__":
|
67 |
+
demo.launch()
|