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