Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,120 +1,105 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
import os
|
4 |
from typing import List, Tuple
|
5 |
-
import
|
6 |
-
|
7 |
-
# Configuração do cliente
|
8 |
-
HF_TOKEN = os.getenv("HF_TOKEN") # Token deve ser configurado como variável de ambiente
|
9 |
-
MODEL_ID = "models/meta-llama/Meta-Llama-3.1-405B-FP8"
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
max_tokens: int,
|
24 |
-
temperatura: float,
|
25 |
-
top_p: float,
|
26 |
-
) -> str:
|
27 |
-
"""
|
28 |
-
Processa a mensagem do usuário e gera uma resposta.
|
29 |
-
"""
|
30 |
-
try:
|
31 |
-
# Formata as mensagens no formato correto
|
32 |
-
messages = [{"role": "system", "content": mensagem_sistema}]
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
messages,
|
47 |
-
max_tokens=max_tokens,
|
48 |
-
stream=True,
|
49 |
-
temperature=temperatura,
|
50 |
-
top_p=top_p,
|
51 |
-
):
|
52 |
-
if hasattr(chunk.choices[0].delta, 'content'):
|
53 |
-
token = chunk.choices[0].delta.content
|
54 |
-
if token:
|
55 |
-
response += token
|
56 |
-
yield response
|
57 |
-
|
58 |
-
except Exception as e:
|
59 |
-
yield f"Desculpe, ocorreu um erro: {str(e)}\nPor favor, verifique sua conexão e configurações."
|
60 |
-
|
61 |
-
# Configuração da interface
|
62 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
63 |
-
gr.Markdown("""
|
64 |
-
# 🤖 Chat com Llama em Português
|
65 |
-
|
66 |
-
Este é um chatbot baseado no modelo Llama. Para usar:
|
67 |
-
1. Configure seu token HF como variável de ambiente
|
68 |
-
2. Ajuste os parâmetros conforme necessário
|
69 |
-
3. Digite sua mensagem e pressione Enter
|
70 |
-
""")
|
71 |
-
|
72 |
-
chatbot = gr.ChatInterface(
|
73 |
-
respond,
|
74 |
-
additional_inputs=[
|
75 |
-
gr.Textbox(
|
76 |
-
value="Você é um assistente amigável e prestativo que responde em português.",
|
77 |
-
label="Mensagem do Sistema"
|
78 |
-
),
|
79 |
-
gr.Slider(
|
80 |
-
minimum=1,
|
81 |
-
maximum=2048,
|
82 |
-
value=512,
|
83 |
-
step=1,
|
84 |
-
label="Máximo de Tokens"
|
85 |
-
),
|
86 |
-
gr.Slider(
|
87 |
-
minimum=0.1,
|
88 |
-
maximum=4.0,
|
89 |
-
value=0.7,
|
90 |
-
step=0.1,
|
91 |
-
label="Temperatura"
|
92 |
-
),
|
93 |
-
gr.Slider(
|
94 |
-
minimum=0.1,
|
95 |
-
maximum=1.0,
|
96 |
-
value=0.95,
|
97 |
-
step=0.05,
|
98 |
-
label="Top-p (Amostragem Nucleus)"
|
99 |
-
),
|
100 |
-
],
|
101 |
-
title="Chat com Llama",
|
102 |
-
description="Um chatbot interativo usando o modelo Llama.",
|
103 |
-
examples=[
|
104 |
-
["Olá! Como você está?"],
|
105 |
-
["Pode me explicar o que é inteligência artificial?"],
|
106 |
-
["Qual é a capital do Brasil?"]
|
107 |
-
]
|
108 |
-
)
|
109 |
-
|
110 |
-
gr.Markdown("""
|
111 |
-
### ℹ️ Informações
|
112 |
-
- Modelo: Llama
|
113 |
-
- Idioma: Português
|
114 |
-
- Stream: Ativado
|
115 |
|
116 |
-
|
117 |
-
""")
|
118 |
|
119 |
if __name__ == "__main__":
|
|
|
120 |
demo.launch(share=False)
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from typing import List, Tuple
|
3 |
+
import os
|
4 |
+
from gradio_client import Client
|
|
|
|
|
|
|
5 |
|
6 |
+
def create_chat_app():
|
7 |
+
def respond(
|
8 |
+
message: str,
|
9 |
+
history: List[Tuple[str, str]],
|
10 |
+
system_message: str,
|
11 |
+
max_tokens: int,
|
12 |
+
temperature: float,
|
13 |
+
top_p: float,
|
14 |
+
) -> str:
|
15 |
+
"""
|
16 |
+
Process user message and generate a response using the Llama model.
|
17 |
+
"""
|
18 |
+
try:
|
19 |
+
# Initialize client for the specific space
|
20 |
+
client = Client("1ofteamos/meta-llama-Llama-3.2-1B-Instruct")
|
21 |
+
|
22 |
+
# Format the conversation history and current message
|
23 |
+
formatted_message = f"{system_message}\n\nConversation history:\n"
|
24 |
+
for user, assistant in history:
|
25 |
+
if user:
|
26 |
+
formatted_message += f"User: {user}\n"
|
27 |
+
if assistant:
|
28 |
+
formatted_message += f"Assistant: {assistant}\n"
|
29 |
+
|
30 |
+
formatted_message += f"User: {message}"
|
31 |
+
|
32 |
+
# Get response from the model
|
33 |
+
response = client.predict(
|
34 |
+
message=formatted_message,
|
35 |
+
api_name="/chat"
|
36 |
+
)
|
37 |
+
|
38 |
+
return response
|
39 |
+
|
40 |
+
except Exception as e:
|
41 |
+
return f"Desculpe, ocorreu um erro: {str(e)}\nPor favor, verifique sua conexão e configurações."
|
42 |
|
43 |
+
# Interface configuration
|
44 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
45 |
+
gr.Markdown("""
|
46 |
+
# 🤖 Chat com Llama em Português
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
Este é um chatbot baseado no modelo Llama. Para usar:
|
49 |
+
1. Digite sua mensagem no campo abaixo
|
50 |
+
2. Ajuste os parâmetros conforme necessário
|
51 |
+
3. Pressione Enter para enviar
|
52 |
+
""")
|
53 |
|
54 |
+
chatbot = gr.ChatInterface(
|
55 |
+
respond,
|
56 |
+
additional_inputs=[
|
57 |
+
gr.Textbox(
|
58 |
+
value="Você é um assistente amigável e prestativo que responde em português.",
|
59 |
+
label="Mensagem do Sistema"
|
60 |
+
),
|
61 |
+
gr.Slider(
|
62 |
+
minimum=1,
|
63 |
+
maximum=2048,
|
64 |
+
value=512,
|
65 |
+
step=1,
|
66 |
+
label="Máximo de Tokens"
|
67 |
+
),
|
68 |
+
gr.Slider(
|
69 |
+
minimum=0.1,
|
70 |
+
maximum=4.0,
|
71 |
+
value=0.7,
|
72 |
+
step=0.1,
|
73 |
+
label="Temperatura"
|
74 |
+
),
|
75 |
+
gr.Slider(
|
76 |
+
minimum=0.1,
|
77 |
+
maximum=1.0,
|
78 |
+
value=0.95,
|
79 |
+
step=0.05,
|
80 |
+
label="Top-p (Amostragem Nucleus)"
|
81 |
+
),
|
82 |
+
],
|
83 |
+
title="Chat com Llama",
|
84 |
+
description="Um chatbot interativo usando o modelo Llama.",
|
85 |
+
examples=[
|
86 |
+
["Olá! Como você está?"],
|
87 |
+
["Pode me explicar o que é inteligência artificial?"],
|
88 |
+
["Qual é a capital do Brasil?"]
|
89 |
+
]
|
90 |
+
)
|
91 |
|
92 |
+
gr.Markdown("""
|
93 |
+
### ℹ️ Informações
|
94 |
+
- Modelo: Llama 3.2 1B Instruct
|
95 |
+
- Idioma: Português
|
96 |
+
- Hospedagem: Hugging Face Spaces
|
97 |
|
98 |
+
Para melhor desempenho, ajuste os parâmetros de acordo com suas necessidades.
|
99 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
return demo
|
|
|
102 |
|
103 |
if __name__ == "__main__":
|
104 |
+
demo = create_chat_app()
|
105 |
demo.launch(share=False)
|