Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,23 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import spaces
|
3 |
-
import torch
|
4 |
from threading import Thread
|
5 |
|
|
|
|
|
6 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
7 |
|
8 |
|
9 |
-
TITLE = "<center>Chat with Llama3-8B-Chinese</center>"
|
10 |
|
11 |
DESCRIPTION = "<h3><center>Visit <a href='https://huggingface.co/shenzhi-wang/Llama3-8B-Chinese-Chat' target='_blank'>our model page</a> for details.</center></h3>"
|
12 |
|
13 |
TOOL_EXAMPLE = '''You have access to the following tools:
|
14 |
```python
|
15 |
-
def
|
16 |
"""
|
17 |
-
|
18 |
-
|
19 |
Args:
|
20 |
-
|
|
|
21 |
"""
|
22 |
pass
|
23 |
```
|
@@ -34,6 +34,15 @@ Action:
|
|
34 |
```
|
35 |
'''
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
tokenizer = AutoTokenizer.from_pretrained("shenzhi-wang/Llama3-8B-Chinese-Chat")
|
39 |
model = AutoModelForCausalLM.from_pretrained("shenzhi-wang/Llama3-8B-Chinese-Chat", device_map="auto")
|
@@ -47,7 +56,9 @@ def stream_chat(message: str, history: list, system: str, temperature: float, ma
|
|
47 |
|
48 |
conversation.append({"role": "user", "content": message})
|
49 |
|
50 |
-
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(
|
|
|
|
|
51 |
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
52 |
|
53 |
generate_kwargs = dict(
|
@@ -69,7 +80,10 @@ def stream_chat(message: str, history: list, system: str, temperature: float, ma
|
|
69 |
yield output
|
70 |
|
71 |
|
72 |
-
with gr.Blocks(
|
|
|
|
|
|
|
73 |
gr.ChatInterface(
|
74 |
fn=stream_chat,
|
75 |
fill_height=True,
|
@@ -99,13 +113,14 @@ with gr.Blocks(fill_height=True) as demo:
|
|
99 |
],
|
100 |
examples=[
|
101 |
["我的蓝牙耳机坏了,我该去看牙科还是耳鼻喉科?", "You are a helpful assistant."],
|
102 |
-
[
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
105 |
],
|
106 |
cache_examples=False,
|
107 |
-
title=TITLE,
|
108 |
-
description=DESCRIPTION,
|
109 |
)
|
110 |
|
111 |
|
|
|
|
|
|
|
|
|
1 |
from threading import Thread
|
2 |
|
3 |
+
import gradio as gr
|
4 |
+
import spaces
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
6 |
|
7 |
|
8 |
+
TITLE = "<h1><center>Chat with Llama3-8B-Chinese</center></h1>"
|
9 |
|
10 |
DESCRIPTION = "<h3><center>Visit <a href='https://huggingface.co/shenzhi-wang/Llama3-8B-Chinese-Chat' target='_blank'>our model page</a> for details.</center></h3>"
|
11 |
|
12 |
TOOL_EXAMPLE = '''You have access to the following tools:
|
13 |
```python
|
14 |
+
def generate_password(length: int, include_symbols: Optional[bool]):
|
15 |
"""
|
16 |
+
Generate a random password.
|
17 |
+
|
18 |
Args:
|
19 |
+
length (int): The length of the password
|
20 |
+
include_symbols (Optional[bool]): Include symbols in the password
|
21 |
"""
|
22 |
pass
|
23 |
```
|
|
|
34 |
```
|
35 |
'''
|
36 |
|
37 |
+
CSS = """
|
38 |
+
.duplicate-button {
|
39 |
+
margin: auto !important;
|
40 |
+
color: white !important;
|
41 |
+
background: black !important;
|
42 |
+
border-radius: 100vh !important;
|
43 |
+
}
|
44 |
+
"""
|
45 |
+
|
46 |
|
47 |
tokenizer = AutoTokenizer.from_pretrained("shenzhi-wang/Llama3-8B-Chinese-Chat")
|
48 |
model = AutoModelForCausalLM.from_pretrained("shenzhi-wang/Llama3-8B-Chinese-Chat", device_map="auto")
|
|
|
56 |
|
57 |
conversation.append({"role": "user", "content": message})
|
58 |
|
59 |
+
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(
|
60 |
+
model.device
|
61 |
+
)
|
62 |
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
63 |
|
64 |
generate_kwargs = dict(
|
|
|
80 |
yield output
|
81 |
|
82 |
|
83 |
+
with gr.Blocks(css=CSS) as demo:
|
84 |
+
gr.HTML(TITLE)
|
85 |
+
gr.HTML(DESCRIPTION)
|
86 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
|
87 |
gr.ChatInterface(
|
88 |
fn=stream_chat,
|
89 |
fill_height=True,
|
|
|
113 |
],
|
114 |
examples=[
|
115 |
["我的蓝牙耳机坏了,我该去看牙科还是耳鼻喉科?", "You are a helpful assistant."],
|
116 |
+
[
|
117 |
+
"在一道没有余数的除法算式里,被除数(不为零)加上除数和商的积,再除以被除数,所得的商是多少?",
|
118 |
+
"You are a helpful assistant.",
|
119 |
+
],
|
120 |
+
["我的笔记本找不到了。", "扮演诸葛亮和我对话。"],
|
121 |
+
["我想要一个新的密码。", "现在你是西游记里的孙悟空。" + TOOL_EXAMPLE],
|
122 |
],
|
123 |
cache_examples=False,
|
|
|
|
|
124 |
)
|
125 |
|
126 |
|