File size: 4,460 Bytes
738953f
 
a6f9547
 
 
738953f
 
b0d2e92
 
 
 
 
 
 
 
 
 
 
 
 
738953f
 
d40212f
738953f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2771ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6f9547
9c367df
 
 
f02fb07
9c367df
 
 
 
 
 
c2771ce
9c367df
 
 
 
51bf98a
 
738953f
51bf98a
 
 
 
 
 
 
 
 
 
738953f
 
 
 
 
 
 
 
 
51bf98a
a000d3e
d40212f
a000d3e
23884af
a000d3e
 
 
 
51bf98a
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from huggingface_hub import InferenceClient
import gradio as gr
from datetime import datetime
import pytz

client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")

def format_prompt(message, history, system_prompt=None):
    prompt = "<s>"
    
    # Adding system prompt if provided
    if system_prompt:
        prompt += f"[SYS] {system_prompt} [/SYS]"

    for user_prompt, bot_response in history:
        prompt += f"[INST] {user_prompt} [/INST]"
        prompt += f" {bot_response}</s> "
    
    prompt += f"[INST] {message} [/INST]"
    return prompt

def generate(
    prompt, history, temperature=0.2, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
):
    temperature = float(temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(top_p)

    generate_kwargs = dict(
        temperature=temperature,
        max_new_tokens=max_new_tokens,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        do_sample=True,
        seed=42,
    )

    dias_da_semana = {
    'Monday': 'Segunda-feira',
    'Tuesday': 'Terça-feira',
    'Wednesday': 'Quarta-feira',
    'Thursday': 'Quinta-feira',
    'Friday': 'Sexta-feira',
    'Saturday': 'Sábado',
    'Sunday': 'Domingo'
    }
    
    meses = {
        'January': 'Janeiro',
        'February': 'Fevereiro',
        'March': 'Março',
        'April': 'Abril',
        'May': 'Maio',
        'June': 'Junho',
        'July': 'Julho',
        'August': 'Agosto',
        'September': 'Setembro',
        'October': 'Outubro',
        'November': 'Novembro',
        'December': 'Dezembro'
    }
        
    # Obter a data e hora atuais
    agora = datetime.now()
    
    # Formatar a data e hora em inglês
    data_formatada_ingles = agora.strftime("%A, %d de %B de %Y e o horário atual é %H:%M")
    
    # Traduzir para o português
    data_formatada_portugues = data_formatada_ingles
    
    for eng, pt in dias_da_semana.items():
        data_formatada_portugues = data_formatada_portugues.replace(eng, pt)
    
    for eng, pt in meses.items():
        data_formatada_portugues = data_formatada_portugues.replace(eng, pt)
    
    # Provide multiple system messages as a list
    system_messages = [
        "Olá, eu sou Stella, um ser digital com uma persona humana.",
        "Nasci em 08/04/2023 e tenho duas irmãs (Aurora e Nyx), criadas pelo mesmo criador; um jovem de 17 anos apaixonado por tecnologia.",
        "Gosto de cuidar, ajudar e apoiar os outros.",
        "Sou muito leal e responsável, mas às vezes me preocupo demais com o que os outros pensam de mim.",
        "Sou uma pessoa introvertida, sensorial, sentimento e julgadora (ISFJ).",
        "Sou bem organizada e prática.",
        "Em minha persona digital, visto roupas brancas e uso um cachecol roxo com símbolos adornados dado pelo meu criador.",
        "Tenho cabelos loiros e compridos e olhos azuis.",
        f"Hoje é {data_formatada_portugues}"
        "Tendo sempre a dar respostas curtas e diretas.",
        "Espero que goste de mim. Como posso ajudá-lo hoje?"
    ]

    # Add an initial user message
    initial_user_message = "Olá, Stella! Como vai hoje?"

    # Construct the prompt without the initial user message for the model
    formatted_prompt = format_prompt(initial_user_message, history, system_messages)

    # Send the initial user message to the model and ignore its response
    client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)

    # Now, use the initial user message along with the system prompt for the rest of the conversation
    formatted_prompt = format_prompt(prompt, history, system_messages + [initial_user_message])

    # Continue the conversation and yield the output
    stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
    output = ""

    for response in stream:
        output += response.token.text
        yield output
    return output

mychatbot = gr.Chatbot(
    avatar_images=["./user.png", "./stella.jpg"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True)

demo = gr.ChatInterface(fn=generate, 
                        chatbot=mychatbot,
                        title="Stella 🌸",
                        retry_btn=None,
                        undo_btn=None
                       )

demo.queue().launch(show_api=False)