Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
|
@@ -15,50 +17,58 @@ def respond(
|
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
for val in history:
|
| 21 |
if val[0]:
|
| 22 |
-
messages.append({"
|
| 23 |
if val[1]:
|
| 24 |
-
messages.append({"
|
| 25 |
|
| 26 |
-
messages.append({"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
|
| 30 |
-
for
|
| 31 |
-
|
| 32 |
-
|
| 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 |
-
|
| 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 |
+
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(
|
|
|
|
| 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 |
|
| 36 |
for val in history:
|
| 37 |
if val[0]:
|
| 38 |
+
messages.append({"Role": "user", "Content": val[0]})
|
| 39 |
if val[1]:
|
| 40 |
+
messages.append({"Role": "assistant", "Content": val[1]})
|
| 41 |
|
| 42 |
+
messages.append({"Role": "user", "Content": message})
|
| 43 |
+
|
| 44 |
+
params = {
|
| 45 |
+
"Model": "hunyuan-large",
|
| 46 |
+
"Messages": messages,
|
| 47 |
+
"Stream": True,
|
| 48 |
+
"StreamModeration": True,
|
| 49 |
+
"EnableEnhancement": False,
|
| 50 |
+
}
|
| 51 |
+
req.from_json_string(json.dumps(params))
|
| 52 |
+
|
| 53 |
+
resp = client.ChatCompletions(req)
|
| 54 |
|
| 55 |
response = ""
|
| 56 |
|
| 57 |
+
for event in resp:
|
| 58 |
+
data = json.loads(event['data'])
|
| 59 |
+
token = data['Choices'][0]['Delta']['Content']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
response += token
|
| 62 |
yield response
|
| 63 |
|
| 64 |
|
| 65 |
+
|
|
|
|
|
|
|
| 66 |
demo = gr.ChatInterface(
|
| 67 |
respond,
|
| 68 |
+
title="Hunyuan-Large"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
demo.launch()
|
| 74 |
+
|