hunyuan-t commited on
Commit
60b9c68
·
verified ·
1 Parent(s): b5b5c44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -49
app.py CHANGED
@@ -1,11 +1,19 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
9
 
10
  def respond(
11
  message,
@@ -15,50 +23,74 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
 
1
+ import os
2
  import gradio as gr
3
+ import json
4
+ from tencentcloud.common import credential
5
+ from tencentcloud.common.profile.client_profile import ClientProfile
6
+ from tencentcloud.common.profile.http_profile import HttpProfile
7
+ from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
8
+ from tencentcloud.hunyuan.v20230901 import hunyuan_client, models
9
 
10
+ from datetime import datetime
 
 
 
11
 
12
+ def print_now(msg):
13
+ now = datetime.now()
14
+ formatted_time = now.strftime("%Y-%m-%d %H:%M:%S.%f")
15
+ print(f"{msg}:{formatted_time}")
16
+ return formatted_time
17
 
18
  def respond(
19
  message,
 
23
  temperature,
24
  top_p,
25
  ):
26
+ try:
27
+ default_system ='You are a helpful assistant.'
28
+
29
+ messages = [{"Role": "system", "Content": default_system}]
30
+
31
+ secret_id = os.getenv('SECRET_ID')
32
+ secret_key = os.getenv('SECRET_KEY')
33
+
34
+ cred = credential.Credential(secret_id, secret_key)
35
+ httpProfile = HttpProfile()
36
+ httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
37
+ clientProfile= ClientProfile()
38
+ clientProfile.httpProfile = httpProfile
39
+ client = hunyuan_client.HunyuanClient(cred, "", clientProfile)
40
+ req = models.ChatCompletionsRequest()
41
+
42
+ for val in history:
43
+ if val[0] and val[1]:
44
+ messages.append({"Role": "user", "Content": val[0]})
45
+ messages.append({"Role": "assistant", "Content": val[1]})
46
+
47
+ messages.append({"Role": "user", "Content": message})
48
+ params = {
49
+ "Model": "hunyuan-turbos-latest",
50
+ "Messages": messages,
51
+ "Stream": True,
52
+ "StreamModeration": True,
53
+ "EnableEnhancement": False,
54
+ }
55
+ req.from_json_string(json.dumps(params))
56
+
57
+ resp= client.ChatCompletions(req)
58
+
59
+ response = ""
 
 
 
 
 
 
 
 
 
60
 
61
+ for event in resp:
62
+ data = json.loads(event['data'])
63
+ token = data['Choices'][0]['Delta']['Content']
64
+
65
+ response += token
66
+ yield response
67
+
68
+ except TencentCloudSDKException as err:
69
+ raise gr.Error(f"腾讯云SDK异常: {err}")
70
+ except Exception as e:
71
+ raise gr.Error(f"发生错误: {str(e)}")
72
+
73
+ example_prompts = [
74
+ ["How to cook Kung Pao chicken the tastiest?"],
75
+ ["Help me create an email expressing my greetings to an old friend."],
76
+ ["写一篇关于青春的五言绝句"],
77
+ ["一枚反面朝上的硬币,被翻转了15下后,它的上面是正面,这个说法正确吗?"]
78
+ ]
79
+ latex_delimiters = [
80
+ {"left": "$$", "right": "$$", "display": True},
81
+ {"left": "\\[", "right": "\\]", "display": True},{"left": "$", "right": "$", "display": False},
82
+ {"left": "\\(", "right": "\\)", "display": False}
83
+ ]
84
+
85
+
86
+ chatbot = gr.Chatbot(latex_delimiters=latex_delimiters, scale=9)
87
+
88
+ demo = gr.ChatInterface(respond,
89
+ title="Hunyuan-Large",
90
+ examples=example_prompts,
91
+ chatbot=chatbot
92
+ )
93
 
94
  if __name__ == "__main__":
95
+ demo.queue(default_concurrency_limit=40)
96
+ demo.launch(max_threads=40)