Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import json
|
| 4 |
-
import
|
| 5 |
-
from tencentcloud.common import credential
|
| 6 |
-
from tencentcloud.common.profile.client_profile import ClientProfile
|
| 7 |
from tencentcloud.common.profile.http_profile import HttpProfile
|
| 8 |
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
|
| 9 |
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models
|
| 10 |
|
| 11 |
-
|
| 12 |
def respond(
|
| 13 |
message,
|
| 14 |
history: list[tuple[str, str]],
|
|
@@ -17,59 +14,56 @@ def respond(
|
|
| 17 |
temperature,
|
| 18 |
top_p,
|
| 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 |
-
demo = gr.ChatInterface(
|
| 68 |
-
respond,
|
| 69 |
title="Hunyuan-Large"
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
| 73 |
if __name__ == "__main__":
|
| 74 |
-
demo.launch()
|
| 75 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import json
|
| 4 |
+
from tencentcloud.common import credentialfrom tencentcloud.common.profile.client_profile import ClientProfile
|
|
|
|
|
|
|
| 5 |
from tencentcloud.common.profile.http_profile import HttpProfile
|
| 6 |
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
|
| 7 |
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models
|
| 8 |
|
|
|
|
| 9 |
def respond(
|
| 10 |
message,
|
| 11 |
history: list[tuple[str, str]],
|
|
|
|
| 14 |
temperature,
|
| 15 |
top_p,
|
| 16 |
):
|
| 17 |
+
try:
|
| 18 |
+
default_system ='You are a helpful assistant.'
|
| 19 |
+
|
| 20 |
+
messages = [{"Role": "system", "Content": default_system}]
|
| 21 |
+
|
| 22 |
+
secret_id = os.getenv('SECRET_ID')
|
| 23 |
+
secret_key = os.getenv('SECRET_KEY')
|
| 24 |
+
|
| 25 |
+
cred = credential.Credential(secret_id, secret_key)
|
| 26 |
+
httpProfile = HttpProfile()
|
| 27 |
+
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
|
| 28 |
+
clientProfile= ClientProfile()
|
| 29 |
+
clientProfile.httpProfile = httpProfile
|
| 30 |
+
client = hunyuan_client.HunyuanClient(cred, "", clientProfile)
|
| 31 |
+
req = models.ChatCompletionsRequest()
|
| 32 |
+
|
| 33 |
+
for val in history:
|
| 34 |
+
if val[0] andval[1]:
|
| 35 |
+
messages.append({"Role": "user", "Content": val[0]})
|
| 36 |
+
messages.append({"Role": "assistant", "Content": val[1]})
|
| 37 |
+
|
| 38 |
+
messages.append({"Role": "user", "Content": message})
|
| 39 |
+
params = {
|
| 40 |
+
"Model": "hunyuan-large",
|
| 41 |
+
"Messages": messages,
|
| 42 |
+
"Stream": True,
|
| 43 |
+
"StreamModeration": True,
|
| 44 |
+
"EnableEnhancement": False,
|
| 45 |
+
}
|
| 46 |
+
req.from_json_string(json.dumps(params))
|
| 47 |
+
|
| 48 |
+
resp= client.ChatCompletions(req)
|
| 49 |
+
|
| 50 |
+
response = ""
|
| 51 |
+
|
| 52 |
+
for event in resp:
|
| 53 |
+
data = json.loads(event['data'])
|
| 54 |
+
token = data['Choices'][0]['Delta']['Content']
|
| 55 |
+
|
| 56 |
+
response += token
|
| 57 |
+
yield response
|
| 58 |
+
|
| 59 |
+
except TencentCloudSDKExceptionas err:
|
| 60 |
+
raise gr.Error(f"腾讯云SDK异常: {err}")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
raise gr.Error(f"发生错误: {str(e)}")
|
| 63 |
+
|
| 64 |
+
demo = gr.ChatInterface(respond,
|
|
|
|
| 65 |
title="Hunyuan-Large"
|
| 66 |
)
|
| 67 |
|
|
|
|
| 68 |
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|
|
|