Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
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.
|
|
|
|
|
|
| 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)
|