Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,14 @@ 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 |
def respond(
|
| 11 |
message,
|
| 12 |
history: list[tuple[str, str]],
|
|
@@ -16,6 +24,7 @@ def respond(
|
|
| 16 |
top_p,
|
| 17 |
):
|
| 18 |
try:
|
|
|
|
| 19 |
default_system ='You are a helpful assistant.'
|
| 20 |
|
| 21 |
messages = [{"Role": "system", "Content": default_system}]
|
|
@@ -30,6 +39,7 @@ def respond(
|
|
| 30 |
clientProfile.httpProfile = httpProfile
|
| 31 |
client = hunyuan_client.HunyuanClient(cred, "", clientProfile)
|
| 32 |
req = models.ChatCompletionsRequest()
|
|
|
|
| 33 |
|
| 34 |
for val in history:
|
| 35 |
if val[0] and val[1]:
|
|
@@ -50,11 +60,16 @@ def respond(
|
|
| 50 |
|
| 51 |
response = ""
|
| 52 |
|
|
|
|
| 53 |
for event in resp:
|
| 54 |
data = json.loads(event['data'])
|
| 55 |
token = data['Choices'][0]['Delta']['Content']
|
| 56 |
|
| 57 |
response += token
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
yield response
|
| 59 |
|
| 60 |
except TencentCloudSDKException as err:
|
|
@@ -62,9 +77,12 @@ def respond(
|
|
| 62 |
except Exception as e:
|
| 63 |
raise gr.Error(f"发生错误: {str(e)}")
|
| 64 |
|
|
|
|
|
|
|
| 65 |
demo = gr.ChatInterface(respond,
|
| 66 |
title="Hunyuan-Large"
|
| 67 |
)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
|
|
|
| 70 |
demo.launch()
|
|
|
|
| 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,
|
| 20 |
history: list[tuple[str, str]],
|
|
|
|
| 24 |
top_p,
|
| 25 |
):
|
| 26 |
try:
|
| 27 |
+
print_now('req')
|
| 28 |
default_system ='You are a helpful assistant.'
|
| 29 |
|
| 30 |
messages = [{"Role": "system", "Content": default_system}]
|
|
|
|
| 39 |
clientProfile.httpProfile = httpProfile
|
| 40 |
client = hunyuan_client.HunyuanClient(cred, "", clientProfile)
|
| 41 |
req = models.ChatCompletionsRequest()
|
| 42 |
+
print_now('res')
|
| 43 |
|
| 44 |
for val in history:
|
| 45 |
if val[0] and val[1]:
|
|
|
|
| 60 |
|
| 61 |
response = ""
|
| 62 |
|
| 63 |
+
isFirst = True
|
| 64 |
for event in resp:
|
| 65 |
data = json.loads(event['data'])
|
| 66 |
token = data['Choices'][0]['Delta']['Content']
|
| 67 |
|
| 68 |
response += token
|
| 69 |
+
if isFirst:
|
| 70 |
+
print(event)
|
| 71 |
+
print_now('first')
|
| 72 |
+
isFirst = False
|
| 73 |
yield response
|
| 74 |
|
| 75 |
except TencentCloudSDKException as err:
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
raise gr.Error(f"发生错误: {str(e)}")
|
| 79 |
|
| 80 |
+
print_now('end')
|
| 81 |
+
|
| 82 |
demo = gr.ChatInterface(respond,
|
| 83 |
title="Hunyuan-Large"
|
| 84 |
)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
+
print_now('init')
|
| 88 |
demo.launch()
|