Hunyuan-Large / app.py
hunyuan-t's picture
Update app.py
ab99dde verified
raw
history blame
1.89 kB
import os
import gradio as gr
import json
import types
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
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p,
):
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()
for val in history:
if val[0]:
messages.append({"Role": "user", "Content": val[0]})
if val[1]:
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 = ""
for event in resp:
data = json.loads(event['data'])
token = data['Choices'][0]['Delta']['Content']
response += token
yield response
demo = gr.ChatInterface(
respond,
title="Hunyuan-Large"
)
if __name__ == "__main__":
demo.launch()