Spaces:
Runtime error

File size: 2,755 Bytes
c20551f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63098c9
c20551f
5b2ba68
 
 
 
c20551f
a1630c6
c20551f
 
 
 
 
 
 
 
 
 
 
5b2ba68
c20551f
5b2ba68
c20551f
 
 
 
 
 
 
d488168
191fca6
d488168
c20551f
 
 
a1630c6
c20551f
 
 
 
 
 
581cf39
c20551f
 
 
 
 
581cf39
c20551f
 
581cf39
c20551f
5b2ba68
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
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)