Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.environ["OPENMIND_HUB_ENDPOINT"]="https://telecom.openmind.cn"
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from openmind import AutoModelForCausalLM, AutoTokenizer
|
6 |
+
from transformers import StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
|
7 |
+
from threading import Thread
|
8 |
+
from huaweicloudsdkcore.auth.credentials import BasicCredentials
|
9 |
+
from huaweicloudsdkmoderation.v2.region.moderation_region import ModerationRegion
|
10 |
+
from huaweicloudsdkcore.exceptions import exceptions
|
11 |
+
from huaweicloudsdkmoderation.v2 import *
|
12 |
+
|
13 |
+
|
14 |
+
ak = __import__('os').getenv("CLOUD_SDK_AK")
|
15 |
+
sk = __import__('os').getenv("CLOUD_SDK_SK")
|
16 |
+
|
17 |
+
|
18 |
+
def text_moderate(unfiltered_text: str, rigion: str):
|
19 |
+
"""Content Moderation api of HuaweiCloud.
|
20 |
+
:param unfiltered_text: The text to be moderated.
|
21 |
+
:param rigion: The region that provides content moderation APIs.
|
22 |
+
"""
|
23 |
+
# The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks.
|
24 |
+
# It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
|
25 |
+
# In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
|
26 |
+
|
27 |
+
|
28 |
+
credentials = BasicCredentials(ak, sk) \
|
29 |
+
|
30 |
+
client = ModerationClient.new_builder() \
|
31 |
+
.with_credentials(credentials) \
|
32 |
+
.with_region(ModerationRegion.value_of(rigion)) \
|
33 |
+
.build()
|
34 |
+
|
35 |
+
try:
|
36 |
+
request = RunTextModerationRequest()
|
37 |
+
listItemsbody = [
|
38 |
+
TextDetectionItemsReq(
|
39 |
+
text=unfiltered_text
|
40 |
+
)
|
41 |
+
]
|
42 |
+
request.body = TextDetectionReq(
|
43 |
+
items=listItemsbody
|
44 |
+
)
|
45 |
+
response = client.run_text_moderation(request)
|
46 |
+
return response
|
47 |
+
except exceptions.ClientRequestException as e:
|
48 |
+
print(e.status_code)
|
49 |
+
print(e.request_id)
|
50 |
+
print(e.error_code)
|
51 |
+
print(e.error_msg)
|
52 |
+
raise e("Please make sure that you have subscribe to the content moderation service\
|
53 |
+
and export the correct access key and secret key as environment variables.")
|
54 |
+
|
55 |
+
tokenizer = AutoTokenizer.from_pretrained("openmind/qwen1.5_7b_chat_pt")
|
56 |
+
model = AutoModelForCausalLM.from_pretrained("openmind/qwen1.5_7b_chat_pt", torch_dtype=torch.bfloat16)
|
57 |
+
model.to("npu:0")
|
58 |
+
|
59 |
+
|
60 |
+
class StopOnTokens(StoppingCriteria):
|
61 |
+
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
|
62 |
+
stop_ids = [2]
|
63 |
+
for stop_id in stop_ids:
|
64 |
+
if input_ids[0][-1] == stop_id:
|
65 |
+
return True
|
66 |
+
return False
|
67 |
+
|
68 |
+
|
69 |
+
def predict(message, history):
|
70 |
+
stop = StopOnTokens()
|
71 |
+
conversation = []
|
72 |
+
|
73 |
+
for user, assistant in history:
|
74 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
75 |
+
|
76 |
+
conversation.append({"role": "user", "content": message})
|
77 |
+
print(f'>>>conversation={conversation}', flush=True)
|
78 |
+
prompt = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
79 |
+
model_inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
80 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=100., skip_prompt=True, skip_special_tokens=True)
|
81 |
+
generate_kwargs = dict(
|
82 |
+
model_inputs,
|
83 |
+
streamer=streamer,
|
84 |
+
max_new_tokens=1024,
|
85 |
+
do_sample=True,
|
86 |
+
top_p=0.95,
|
87 |
+
top_k=50,
|
88 |
+
temperature=0.7,
|
89 |
+
repetition_penalty=1.0,
|
90 |
+
num_beams=1,
|
91 |
+
stopping_criteria=StoppingCriteriaList([stop])
|
92 |
+
)
|
93 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
94 |
+
t.start()
|
95 |
+
partial_message = ""
|
96 |
+
for new_token in streamer:
|
97 |
+
partial_message += new_token
|
98 |
+
if '</s>' in partial_message:
|
99 |
+
break
|
100 |
+
if all([ak, sk]):
|
101 |
+
res = text_moderate(partial_message, "cn-north-4")
|
102 |
+
if res.result.suggestion != "pass":
|
103 |
+
partial_message = "抱歉,这个问题我无法回答!"
|
104 |
+
return partial_message
|
105 |
+
|
106 |
+
|
107 |
+
# Setting up the Gradio chat interface.
|
108 |
+
gr.ChatInterface(predict,
|
109 |
+
title="Qwen1.5 7B 对话",
|
110 |
+
description="警告:所有答案都是AI生成的,可能包含不准确的信息。",
|
111 |
+
examples=['杭州有哪些著名的旅游景点?', '海钓有哪些要领?']
|
112 |
+
).launch()
|
113 |
+
|