Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,24 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
return e
|
26 |
-
|
27 |
-
self.messages.append({"role": "assistant", "content": message})
|
28 |
-
|
29 |
-
if len(self.messages) > self.round * 2 + 1:
|
30 |
-
text = self._build_message(self.messages)
|
31 |
-
self.messages = []
|
32 |
-
self.messages.append({"role": "system", "content": text})
|
33 |
-
return message
|
34 |
-
|
35 |
-
def _build_message(self, messages):
|
36 |
-
text = ""
|
37 |
-
for message in messages:
|
38 |
-
if message["role"] == "user":
|
39 |
-
text += "User : " + message["content"] + "\n\n"
|
40 |
-
if message["role"] == "assistant":
|
41 |
-
text += "Assistant : " + message["content"] + "\n\n"
|
42 |
-
return text
|
43 |
-
|
44 |
-
prompt = """你是一个大数据和AI领域的专家,用中文回答大数据和AI的相关问题。你的回答需要满足以下要求:
|
45 |
-
1. 你的回答必须是中文
|
46 |
-
2. 回答限制在200个字以内
|
47 |
-
3. 拒绝回答违反社会道德和法律的问题"""
|
48 |
-
|
49 |
-
conv = Conversation(prompt, 3)
|
50 |
-
|
51 |
-
def answer(question, history=[]):
|
52 |
-
history.append(question)
|
53 |
-
message = conv.ask(question)
|
54 |
-
history.append(message)
|
55 |
-
responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
|
56 |
-
print(responses)
|
57 |
-
return responses, history
|
58 |
-
|
59 |
-
with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as rxbot:
|
60 |
-
chatbot = gr.Chatbot(elem_id="chatbot")
|
61 |
-
state = gr.State([])
|
62 |
-
|
63 |
-
with gr.Row():
|
64 |
-
txt = gr.Textbox(show_label=False, placeholder="请输入你的问题").style(container=False)
|
65 |
-
|
66 |
-
txt.submit(answer, [txt, state], [chatbot, state])
|
67 |
-
|
68 |
-
|
69 |
-
rxbot.launch()
|
|
|
1 |
+
from transformers import AutoTokenizer
|
2 |
+
import transformers
|
3 |
+
import torch
|
4 |
+
|
5 |
+
model = "meta-llama/Llama-2-7b-chat-hf"
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model)
|
8 |
+
pipeline = transformers.pipeline(
|
9 |
+
"text-generation",
|
10 |
+
model=model,
|
11 |
+
torch_dtype=torch.float16,
|
12 |
+
device_map="auto",
|
13 |
+
)
|
14 |
+
|
15 |
+
sequences = pipeline(
|
16 |
+
'I liked "Breaking Bad" and "Band of Brothers". Do you have any recommendations of other shows I might like?\n',
|
17 |
+
do_sample=True,
|
18 |
+
top_k=10,
|
19 |
+
num_return_sequences=1,
|
20 |
+
eos_token_id=tokenizer.eos_token_id,
|
21 |
+
max_length=200,
|
22 |
+
)
|
23 |
+
for seq in sequences:
|
24 |
+
print(f"Result: {seq['generated_text']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|