File size: 3,466 Bytes
e4932b3
 
 
7e76458
e4932b3
 
 
 
 
 
 
 
a233e6e
e4932b3
 
 
a233e6e
23393aa
e4932b3
 
 
37cbb3c
 
 
 
 
 
 
 
 
 
e4932b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37cbb3c
 
 
e4932b3
37cbb3c
e4932b3
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import spaces

# Load model and tokenizer
model_name_or_path = "tencent/Hunyuan-MT-7B"
print("Loading model... This may take a few minutes.")

tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
model = AutoModelForCausalLM.from_pretrained(
    model_name_or_path,
    torch_dtype=torch.bfloat16,
    device_map="auto"
)

@spaces.GPU(duration=120)
def respond(message, history, system_message=None, max_tokens=None, temperature=None, top_p=None):
    """
    Generate response from Hunyuan-MT model
    """
    # Set default values if None (happens during example caching)
    if system_message is None:
        system_message = "You are a helpful AI assistant."
    if max_tokens is None:
        max_tokens = 512
    if temperature is None:
        temperature = 0.7
    if top_p is None:
        top_p = 0.95
    
    # Build conversation history
    messages = []
    
    # Add system message if provided
    if system_message:
        messages.append({"role": "system", "content": system_message})
    
    # Add conversation history
    for user_msg, assistant_msg in history:
        messages.append({"role": "user", "content": user_msg})
        if assistant_msg:
            messages.append({"role": "assistant", "content": assistant_msg})
    
    # Add current message
    messages.append({"role": "user", "content": message})
    
    # Tokenize the conversation
    tokenized_chat = tokenizer.apply_chat_template(
        messages,
        tokenize=True,
        add_generation_prompt=True,
        return_tensors="pt"
    )
    
    # Generate response
    with torch.no_grad():
        outputs = model.generate(
            tokenized_chat.to(model.device),
            max_new_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            do_sample=True if temperature > 0 else False,
            pad_token_id=tokenizer.eos_token_id
        )
    
    # Decode only the new tokens
    response = tokenizer.decode(outputs[0][tokenized_chat.shape[-1]:], skip_special_tokens=True)
    
    return response

# Create Gradio interface
demo = gr.ChatInterface(
    respond,
    additional_inputs=[
        gr.Textbox(
            value="You are a helpful AI assistant.",
            label="System Message",
            lines=2
        ),
        gr.Slider(
            minimum=1,
            maximum=2048,
            value=512,
            step=1,
            label="Max New Tokens"
        ),
        gr.Slider(
            minimum=0,
            maximum=2,
            value=0.7,
            step=0.1,
            label="Temperature"
        ),
        gr.Slider(
            minimum=0,
            maximum=1,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)"
        ),
    ],
    title="Hunyuan-MT-7B Chatbot",
    description="Chat with Tencent's Hunyuan-MT-7B model. This model is particularly good at translation tasks.",
    examples=[
        ["Translate to Chinese: It's on the house.", "You are a helpful AI assistant.", 512, 0.7, 0.95],
        ["What are the main differences between Python and JavaScript?", "You are a helpful AI assistant.", 512, 0.7, 0.95],
        ["Explain quantum computing in simple terms.", "You are a helpful AI assistant.", 512, 0.7, 0.95],
    ],
    cache_examples=False,
    theme="soft"
)

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