File size: 2,658 Bytes
ab99dde
6e3654b
ab99dde
8fe1510
 
ab99dde
 
 
6e3654b
a10fc1a
 
 
 
 
 
 
 
6e3654b
 
 
 
 
 
 
 
38662f5
a10fc1a
38662f5
 
 
 
 
 
 
 
 
 
 
 
 
 
a10fc1a
38662f5
 
0aa29a4
38662f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a10fc1a
38662f5
 
 
 
 
a10fc1a
 
 
 
38662f5
 
5c05d36
38662f5
 
 
 
a10fc1a
 
38662f5
ab99dde
6e3654b
 
 
a10fc1a
99f8eb2
b682dbc
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
import os
import gradio as gr
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models

from datetime import datetime

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:
        print_now('req')
        default_system ='You are a helpful assistant.'

        messages = [{"Role": "system", "Content": default_system}]

        secret_id = os.getenv('SECRET_ID')
        secret_key = os.getenv('SECRET_KEY')

        cred = credential.Credential(secret_id, secret_key)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
        clientProfile= ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = hunyuan_client.HunyuanClient(cred, "", clientProfile)
        req = models.ChatCompletionsRequest()
        print_now('res')

        for val in history:
            if val[0] and val[1]:
                messages.append({"Role": "user", "Content": val[0]})
                messages.append({"Role": "assistant", "Content": val[1]})
        
        messages.append({"Role": "user", "Content": message})
        params = {
            "Model": "hunyuan-large",
            "Messages": messages,
            "Stream": True,
            "StreamModeration": True,
            "EnableEnhancement": False,
        }
        req.from_json_string(json.dumps(params))

        resp= client.ChatCompletions(req)

        response = ""

        isFirst = True
        for event in resp:
            data = json.loads(event['data'])
            token = data['Choices'][0]['Delta']['Content']

            response += token
            if isFirst:
                print(event)
                print_now('first')
                isFirst = False
            yield response

    except TencentCloudSDKException as err:
        raise gr.Error(f"腾讯云SDK异常: {err}")
    except Exception as e:
        raise gr.Error(f"发生错误: {str(e)}")

    print_now('end')

demo = gr.ChatInterface(respond,
    title="Hunyuan-Large"
)

if __name__ == "__main__":
    print_now('init')
    demo.queue(default_concurrency_limit=40)
    demo.launch(max_threads=40)