Spaces:
Running
Running
access ernie4.5 through qianfan sdk
Browse filesSigned-off-by: Zhang Jun <jzhang533@gmail.com>
- README.md +2 -2
- app.py +31 -26
- requirements.txt +2 -1
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 💬
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: purple
|
|
@@ -8,7 +8,7 @@ sdk_version: 5.0.1
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
-
short_description:
|
| 12 |
---
|
| 13 |
|
| 14 |
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ERNIE 4.5
|
| 3 |
emoji: 💬
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: purple
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
+
short_description: ERNIE 4.5 (BAIDU's LLM), https://yiyan.baidu.com/
|
| 12 |
---
|
| 13 |
|
| 14 |
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
app.py
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
|
@@ -25,40 +38,32 @@ def respond(
|
|
| 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=
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=
|
| 52 |
gr.Slider(
|
| 53 |
minimum=0.1,
|
| 54 |
maximum=1.0,
|
| 55 |
-
value=0.
|
| 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 |
+
from openai import OpenAI
|
| 4 |
|
| 5 |
+
|
| 6 |
+
title = "ERNIE 4.5 : BAIDU's LLM"
|
| 7 |
+
description = '''
|
| 8 |
+
- Official Website: <https://yiyan.baidu.com/> (UI in Chinese)
|
| 9 |
+
- Twitter post: [We've just unveiled ERNIE 4.5 & X1! 🚀](https://x.com/Baidu_Inc/status/1901089355890036897)
|
| 10 |
+
- API services: [Qianfan Large Model Platform](https://cloud.baidu.com/product-s/qianfan_home) (cloud platform providing LLM services, UI in Chinese)
|
| 11 |
+
'''
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
qianfan_api_key = os.getenv("QIANFAN_TOKEN")
|
| 15 |
+
qianfan_model = "ernie-4.5-8k-preview"
|
| 16 |
+
|
| 17 |
+
client = OpenAI(
|
| 18 |
+
base_url='https://qianfan.baidubce.com/v2',
|
| 19 |
+
api_key=qianfan_api_key
|
| 20 |
+
)
|
| 21 |
|
| 22 |
|
| 23 |
def respond(
|
|
|
|
| 38 |
|
| 39 |
messages.append({"role": "user", "content": message})
|
| 40 |
|
| 41 |
+
response = client.chat.completions.create(
|
| 42 |
+
model=qianfan_model,
|
| 43 |
+
messages=messages,
|
| 44 |
+
max_completion_tokens = max_tokens,
|
| 45 |
+
temperature = temperature,
|
| 46 |
+
top_p = top_p
|
| 47 |
+
)
|
| 48 |
+
return response.choices[0].message.content
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
demo = gr.ChatInterface(
|
| 51 |
respond,
|
| 52 |
additional_inputs=[
|
| 53 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 54 |
+
gr.Slider(minimum=2, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 55 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Temperature"),
|
| 56 |
gr.Slider(
|
| 57 |
minimum=0.1,
|
| 58 |
maximum=1.0,
|
| 59 |
+
value=0.7,
|
| 60 |
step=0.05,
|
| 61 |
label="Top-p (nucleus sampling)",
|
| 62 |
),
|
| 63 |
],
|
| 64 |
+
title = title,
|
| 65 |
+
description = description,
|
| 66 |
)
|
| 67 |
|
|
|
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
huggingface_hub
|