Spaces:
Runtime error
Runtime error
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) | |