File size: 3,448 Bytes
a73f817
50f7f91
6bd3626
 
a73f817
6bd3626
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a73f817
6bd3626
 
 
 
50f7f91
6bd3626
 
 
 
 
50f7f91
6bd3626
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50f7f91
6bd3626
 
 
 
 
50f7f91
6bd3626
 
50f7f91
6bd3626
a73f817
 
6bd3626
50f7f91
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import gradio as gr
from typing import List, Tuple
import os
from gradio_client import Client

def create_chat_app():
    def respond(
        message: str,
        history: List[Tuple[str, str]],
        system_message: str,
        max_tokens: int,
        temperature: float,
        top_p: float,
    ) -> str:
        """
        Process user message and generate a response using the Llama model.
        """
        try:
            # Initialize client for the specific space
            client = Client("1ofteamos/meta-llama-Llama-3.2-1B-Instruct")
            
            # Format the conversation history and current message
            formatted_message = f"{system_message}\n\nConversation history:\n"
            for user, assistant in history:
                if user:
                    formatted_message += f"User: {user}\n"
                if assistant:
                    formatted_message += f"Assistant: {assistant}\n"
            
            formatted_message += f"User: {message}"
            
            # Get response from the model
            response = client.predict(
                message=formatted_message,
                api_name="/chat"
            )
            
            return response
            
        except Exception as e:
            return f"Desculpe, ocorreu um erro: {str(e)}\nPor favor, verifique sua conexão e configurações."

    # Interface configuration
    with gr.Blocks(theme=gr.themes.Soft()) as demo:
        gr.Markdown("""
        # 🤖 Chat com Llama em Português
        
        Este é um chatbot baseado no modelo Llama. Para usar:
        1. Digite sua mensagem no campo abaixo
        2. Ajuste os parâmetros conforme necessário
        3. Pressione Enter para enviar
        """)
        
        chatbot = gr.ChatInterface(
            respond,
            additional_inputs=[
                gr.Textbox(
                    value="Você é um assistente amigável e prestativo que responde em português.",
                    label="Mensagem do Sistema"
                ),
                gr.Slider(
                    minimum=1,
                    maximum=2048,
                    value=512,
                    step=1,
                    label="Máximo de Tokens"
                ),
                gr.Slider(
                    minimum=0.1,
                    maximum=4.0,
                    value=0.7,
                    step=0.1,
                    label="Temperatura"
                ),
                gr.Slider(
                    minimum=0.1,
                    maximum=1.0,
                    value=0.95,
                    step=0.05,
                    label="Top-p (Amostragem Nucleus)"
                ),
            ],
            title="Chat com Llama",
            description="Um chatbot interativo usando o modelo Llama.",
            examples=[
                ["Olá! Como você está?"],
                ["Pode me explicar o que é inteligência artificial?"],
                ["Qual é a capital do Brasil?"]
            ]
        )
        
        gr.Markdown("""
        ### ℹ️ Informações
        - Modelo: Llama 3.2 1B Instruct
        - Idioma: Português
        - Hospedagem: Hugging Face Spaces
        
        Para melhor desempenho, ajuste os parâmetros de acordo com suas necessidades.
        """)
    
    return demo

if __name__ == "__main__":
    demo = create_chat_app()
    demo.launch(share=False)