jzhang533 commited on
Commit
050dcec
·
1 Parent(s): 1665267

launch ernie x1 turbo

Browse files

Signed-off-by: Zhang Jun <[email protected]>

Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +64 -0
README.md CHANGED
@@ -8,7 +8,7 @@ sdk_version: 5.26.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
- short_description: ernie x1
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
+ short_description: BAIDU's LLM, https://yiyan.baidu.com/
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+
5
+ title = "ERNIE X1 Turbo: BAIDU's Reasoning LLM"
6
+ description = """
7
+ - Official Website: <https://yiyan.baidu.com/> (UI in Chinese)
8
+ - API services: [Qianfan Large Model Platform](https://cloud.baidu.com/product-s/qianfan_home) (cloud platform providing LLM services, UI in Chinese)
9
+ - [ERNIE 4.5 Turbo Demo](https://huggingface.co/spaces/PaddlePaddle/ernie_demo) | [ERNIE X1 Turbo Demo](https://huggingface.co/spaces/PaddlePaddle/ernie_x1_demo)
10
+ """
11
+
12
+
13
+ qianfan_api_key = os.getenv("QIANFAN_TOKEN")
14
+ qianfan_model = "ernie-x1-turbo-32k"
15
+
16
+
17
+ client = OpenAI(base_url="https://qianfan.baidubce.com/v2", api_key=qianfan_api_key)
18
+
19
+
20
+ def respond(
21
+ message,
22
+ history: list[tuple[str, str]],
23
+ system_message,
24
+ max_tokens,
25
+ ):
26
+ messages = [{"role": "system", "content": system_message}]
27
+ messages.append({"role": "user", "content": message})
28
+
29
+ response = client.chat.completions.create(
30
+ model=qianfan_model,
31
+ messages=messages,
32
+ max_completion_tokens=max_tokens,
33
+ stream=True,
34
+ )
35
+
36
+ reasoning_content = "**Thinking**:\n"
37
+ content = "\n\n**Answer**: \n"
38
+
39
+ for chunk in response:
40
+ if hasattr(chunk.choices[0].delta, 'reasoning_content'):
41
+ token = chunk.choices[0].delta.reasoning_content
42
+ if token:
43
+ reasoning_content += token
44
+ yield reasoning_content
45
+ elif hasattr(chunk.choices[0].delta, 'content'):
46
+ token = chunk.choices[0].delta.content
47
+ if token:
48
+ content += token
49
+ yield reasoning_content + content
50
+
51
+
52
+ demo = gr.ChatInterface(
53
+ respond,
54
+ additional_inputs=[
55
+ gr.Textbox(value="", label="System message"),
56
+ gr.Slider(minimum=2, maximum=16384, value=10240, step=1, label="Max new tokens"),
57
+ ],
58
+ title=title,
59
+ description=description,
60
+ type='messages',
61
+ )
62
+
63
+ if __name__ == "__main__":
64
+ demo.launch()