File size: 2,470 Bytes
a1655b8
5569e88
 
a1655b8
5569e88
 
 
 
 
a1655b8
 
 
 
 
 
 
 
 
 
 
 
 
5569e88
 
 
a1655b8
5569e88
 
 
 
 
 
 
 
a1655b8
 
5569e88
a1655b8
5569e88
 
 
a1655b8
c042dce
5569e88
a1655b8
 
 
 
6896fe7
4b81aaa
 
a1655b8
 
 
 
 
 
 
c042dce
a1655b8
 
 
 
5569e88
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# Load the model and tokenizer outside the function to avoid
# reloading it on every call.
model_name = "gustavokuklinski/aeon-360m"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

def respond(
    message,
    history: list[dict[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
):
    messages = [{"role": "system", "content": system_message}]
    messages.extend(history)
    messages.append({"role": "user", "content": message})

    # Apply the chat template to format the messages
    # The jinja template from the model card can be used
    input_ids = tokenizer.apply_chat_template(
        messages,
        add_generation_prompt=True,
        return_tensors="pt"
    )

    # Generate the response
    outputs = model.generate(
        input_ids,
        max_new_tokens=max_tokens,
        temperature=temperature,
        top_p=top_p,
    )

    # Decode the generated text and yield the response
    response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
    yield response


# The rest of your Gradio ChatInterface code remains the same
chatbot = gr.ChatInterface(
    respond,
    type="messages",
    additional_inputs=[
        gr.Textbox(value="Your name is Aeon. Answer the user's question concisely using **only** the provided CONTEXT. If the CONTEXT doesn't contain the answer, state: 'I don't know, can we search?'. Do not add any information not present in the CONTEXT. Your responses must be in plain, natural language. **Factual questions:** Use the provided CONTEXT to form a single, comprehensive answer. Do not use information outside the CONTEXT. If the CONTEXT does not contain the answer, state: 'I don't know about it. Can we search?'. **Conversational questions:** Respond naturally and conversationally, without using the CONTEXT. Never echo the user's question or the CONTEXT.", label="System message"),
        gr.Slider(minimum=1, maximum=2048, value=2048, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
        
    ],
)

if __name__ == "__main__":
    chatbot.launch()