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 |
-
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")
|
@@ -27,38 +24,28 @@ def respond(
|
|
27 |
default_system ="You are Tencent's helpful AI assistant Hunyuan."
|
28 |
|
29 |
messages = [{"Role": "system", "Content": default_system}]
|
30 |
-
|
31 |
-
|
32 |
-
|
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 |
-
|
49 |
-
"
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
"
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
resp= client.ChatCompletions(req)
|
58 |
-
|
59 |
response = ""
|
60 |
|
61 |
-
for event in
|
62 |
data = json.loads(event['data'])
|
63 |
if message.find('写一篇关于青春的五言绝句') != -1:
|
64 |
print(11111111111)
|
|
|
1 |
import os
|
|
|
2 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from datetime import datetime
|
4 |
|
5 |
+
import gradio as gr
|
6 |
+
from openai import OpenAI
|
7 |
+
|
8 |
+
|
9 |
def print_now(msg):
|
10 |
now = datetime.now()
|
11 |
formatted_time = now.strftime("%Y-%m-%d %H:%M:%S.%f")
|
|
|
24 |
default_system ="You are Tencent's helpful AI assistant Hunyuan."
|
25 |
|
26 |
messages = [{"Role": "system", "Content": default_system}]
|
27 |
+
client = OpenAI(
|
28 |
+
api_key=os.environ.get("HUNYUAN_API_KEY"), # 混元 APIKey
|
29 |
+
base_url="https://api.hunyuan.cloud.tencent.com/v1", # 混元 endpoint
|
30 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
for val in history:
|
32 |
if val[0] and val[1]:
|
33 |
messages.append({"Role": "user", "Content": val[0]})
|
34 |
messages.append({"Role": "assistant", "Content": val[1]})
|
35 |
|
36 |
messages.append({"Role": "user", "Content": message})
|
37 |
+
completion = client.chat.completions.create(
|
38 |
+
model="hunyuan-turbo",
|
39 |
+
messages=messages,
|
40 |
+
stream=True,
|
41 |
+
extra_body={
|
42 |
+
"stream_moderation": True,
|
43 |
+
"enable_enhancement": False,
|
44 |
+
}
|
45 |
+
)
|
|
|
|
|
46 |
response = ""
|
47 |
|
48 |
+
for event in completion:
|
49 |
data = json.loads(event['data'])
|
50 |
if message.find('写一篇关于青春的五言绝句') != -1:
|
51 |
print(11111111111)
|