Download app.py from contatos/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 2.76 kB
-
https://huggingface.co/spaces/contatos/chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/contatos/chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/contatos/chatbot/resolve/main/app.py
2.76 kB
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") | |
| def respond( | |
| message: str, | |
| history: list[tuple[str, str]], | |
| system_message: str, | |
| max_tokens: int, | |
| temperature: float, | |
| top_p: float, | |
| ): | |
| messages = [{"role": "system", "content": system_message}] | |
| messages.extend([{"role": "user", "content": val[0]} for val in history]) | |
| messages.append({"role": "user", "content": message}) # Corrigido colchete | |
| # Validação do top_p | |
| if not (0.0 < top_p < 1.0): | |
| return "Error: top_p must be > 0.0 and < 1.0" | |
| response = "" | |
| try: | |
| # Chamada não streaming | |
| response_data = client.chat_completion( | |
| messages, | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ) | |
| # Logando a resposta da API para depuração | |
| print("Raw API Response:", response_data) # Debugging line | |
| response = response_data.choices[0].message.content | |
| return response | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Criar a interface Gradio | |
| with gr.Blocks() as demo: | |
| gr.Markdown("### QaChat Constantly learning") # Cabeçalho opcional | |
| # Inputs adicionais ocultos | |
| system_message = gr.Textbox(value="You are a friendly QaChat.", label="System message", visible=False) | |
| max_tokens = gr.Slider(minimum=1, maximum=2048, value=2048, step=1, label="Max new tokens", visible=False) # Reduzido max_tokens | |
| temperature = gr.Slider(minimum=0.1, maximum=4.0, value=1.2, step=0.1, label="Temperature", visible=False) # Reduzido para 0.1 | |
| top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.99, step=0.05, label="Top-p (nucleus sampling)", visible=False) # Reduzido para 0.1 | |
| # Adicionar um elemento de chat com o rótulo "QaChat" | |
| chatbot = gr.Chatbot(label="QaChat") # Alterado o label para "QaChat" | |
| # Armazenar o histórico da conversa | |
| chat_history = gr.State([]) # Estado para armazenar o histórico | |
| # Definindo a interação | |
| user_input = gr.Textbox(placeholder="Type your question...") | |
| submit_button = gr.Button("Send") | |
| # Ação de enviar | |
| def send_message(message, history): | |
| response = respond(message, history, system_message.value, max_tokens.value, temperature.value, top_p.value) | |
| history.append((message, response)) # Adiciona a nova mensagem e a resposta ao histórico | |
| return history, "" # Retorna o novo histórico e limpa o input | |
| # Definir as saídas, incluindo o reset do user_input | |
| submit_button.click(send_message, inputs=[user_input, chat_history], outputs=[chatbot, user_input]) | |
| if __name__ == "__main__": | |
| demo.launch(share=True) | |