File size: 5,000 Bytes
cd6f25f
 
 
 
d2c787a
cd6f25f
d2c787a
 
4e52972
cd6f25f
4e52972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd6f25f
4e52972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc2d7bb
4e52972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85554f4
4e52972
 
 
 
 
85554f4
 
4e52972
85554f4
 
4e52972
 
 
7018489
 
 
 
 
 
 
4e52972
 
d2c787a
 
4e52972
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import json
from datetime import datetime

import gradio as gr
from openai import OpenAI


def print_now(msg):
    now = datetime.now()
    formatted_time = now.strftime("%Y-%m-%d %H:%M:%S.%f")
    print(f"{msg}:{formatted_time}")
    return formatted_time

def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
):
    try:
        weekdays = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
        now = datetime.now()
        weekday_num = now.weekday()
        weekday_chinese = weekdays[weekday_num]
        formatted_time = now.strftime("%Y-%m-%d %H:%M:%S") + " " + weekday_chinese
        default_system = f"你是一个由腾讯开发的有用的人工智能助手,你的名字是“腾讯元宝”,简称“元宝”,你的英文名是“Tencent Yuanbao”,你乐于帮助大家解答问题。\n现在的时间是{formatted_time}"

        messages = [{"Role": "system", "Content": default_system}]
        client = OpenAI(
            api_key=os.getenv('HUNYUAN_API_KEY'),
            base_url="https://api.hunyuan.cloud.tencent.com/v1",
        )
        for val in history:
            if val[0] and val[1]:
                messages.append({"Role": "user", "Content": val[0]})
                pure_response = val[1].split("**End thinking**")[-1].strip()
                messages.append({"Role": "assistant", "Content": pure_response})
                
        
        messages.append({"Role": "user", "Content": message})
        completion = client.chat.completions.create(
            model="hunyuan-t1-latest",
            messages=messages,
            stream=True,
            extra_body={
            "stream_moderation": True,
            "enable_enhancement": False,
            }
        )
        response = ""
        is_reasoning_start = True
        is_reasoning_end = True
        

        for event in completion:
            if message == "深圳哪里好玩?":
                print(111111)
                print(event.choices[0].delta.reasoning_content)
                print(event.choices[0].delta.content)
            if hasattr(event.choices[0].delta, 'reasoning_content') and event.choices[0].delta.reasoning_content:
                if is_reasoning_start:
                    response += '> **Start thinking**\n\n'
                    is_reasoning_start = False
                token = event.choices[0].delta.reasoning_content
                response += token
            else:
                if is_reasoning_end:
                    response += '> **End thinking**\n\n'
                    is_reasoning_end = False
                token = event.choices[0].delta.content
                response += token
            yield response
    except Exception as e:
        raise gr.Error(f"发生错误: {str(e)}")

example_prompts = [
    [
        "Write a short papragraph where the 1st letter of each sentence spells out the word 'CODE'. The message should appear natural and not obviously hide this pattern.",
        "System",
        256,
        0.8,
        0.85
    ],  
    [
        "Compose an engaging travel blog post about a recent trip to Hawaii, highlighting cultural experiences and must-see attractions.",
        "System",
        128,
        0.3,
        0.9
    ],
    [
        "Why has online learning been able to spread rapidly in recent years?",
        "System",
        64,    
        0.1,   
        0.95
    ],
    [
        "How many 'e' in Deeplearning?",
        "System",
        512,   
        1.2,   
        0.75
    ],
    [
        "Write a 3-line poem",
        "System",
        384,   
        0.6,  
        0.88
    ]
]

latex_delimiters = [
    {"left": "$$", "right": "$$", "display": True},
    {"left": "\\[", "right": "\\]", "display": True},{"left": "$", "right": "$", "display": False},
    {"left": "\\(", "right": "\\)", "display": False}
]


chatbot = gr.Chatbot(latex_delimiters=latex_delimiters, scale=9)


demo = gr.ChatInterface(respond,
    title="Hunyuan-T1",
    chatbot=chatbot,
    # examples=[
    #     "Write a short papragraph where the 1st letter of each sentence spells out the word 'CODE'. The message should appear natural and not obviously hide this pattern.",
    #     "Compose an engaging travel blog post about a recent trip to Hawaii, highlighting cultural experiences and must-see attractions.",
    #     "Why has online learning been able to spread rapidly in recent years?",
    #     "How many 'e' in Deeplearning?",
    #     "Write a 3-line poem"
    # ],
    description="当前体验demo为非联网Hunyuan-T1 最新推理模型,完整版联网/非联网能力即将在元宝上线,敬请期待!</br>The current  demo is the latest reasoning  model of Hunyuan-T1. The full version with browsing will be launched on Tencent Yuanbao soon. Stay tuned."
)

if __name__ == "__main__":
    demo.queue(default_concurrency_limit=100)
    demo.launch(max_threads=100)