hunyuan-t commited on
Commit
38662f5
1 Parent(s): 23c7125

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -56
app.py CHANGED
@@ -1,14 +1,11 @@
1
  import os
2
  import gradio as gr
3
  import json
4
- import types
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
- default_system = 'You are a helpful assistant.'
21
-
22
- messages = [{"Role": "system", "Content": default_system}]
23
-
24
- secret_id = os.getenv('SECRET_ID')
25
- secret_key = os.getenv('SECRET_KEY')
26
-
27
-
28
- cred = credential.Credential(secret_id, secret_key)
29
- httpProfile = HttpProfile()
30
- httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
31
- clientProfile = ClientProfile()
32
- clientProfile.httpProfile = httpProfile
33
- client = hunyuan_client.HunyuanClient(cred, "", clientProfile)
34
- req = models.ChatCompletionsRequest()
35
- print(history)
36
-
37
- for val in history:
38
- if val[0]:
39
- messages.append({"Role": "user", "Content": val[0]})
40
- if val[1]:
41
- messages.append({"Role": "assistant", "Content": val[1]})
42
-
43
- messages.append({"Role": "user", "Content": message})
44
-
45
- params = {
46
- "Model": "hunyuan-large",
47
- "Messages": messages,
48
- "Stream": True,
49
- "StreamModeration": True,
50
- "EnableEnhancement": False,
51
- }
52
- req.from_json_string(json.dumps(params))
53
-
54
- resp = client.ChatCompletions(req)
55
-
56
- response = ""
57
-
58
- for event in resp:
59
- data = json.loads(event['data'])
60
- token = data['Choices'][0]['Delta']['Content']
61
-
62
- response += token
63
- yield response
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()