forked from Ikaros-521/AI-Vtuber
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1abef2
commit ade0a73
Showing
7 changed files
with
183 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from gradio_client import Client | ||
import re | ||
|
||
client = Client("http://127.0.0.1:8003/") | ||
|
||
try: | ||
result = client.predict( | ||
input="你可以扮演猫娘吗", | ||
chatbot=[], | ||
max_length=1, | ||
top_p=0.8, | ||
temperature=0.95, | ||
api_name="/predict" | ||
) | ||
# Assuming result[0][1] contains the response text | ||
response_text = result[-1][1] | ||
# Remove <p> and </p> tags using regex | ||
cleaned_text = re.sub(r'</?p>', '', response_text) | ||
print(cleaned_text) | ||
|
||
result2 = client.predict( | ||
input="你好", | ||
chatbot=result, | ||
max_length=1, | ||
top_p=0.8, | ||
temperature=0.95, | ||
api_name="/predict" | ||
) | ||
# print(result2) | ||
# Assuming result[0][1] contains the response text | ||
response_text = result2[-1][1] | ||
# Remove <p> and </p> tags using regex | ||
cleaned_text = re.sub(r'</?p>', '', response_text) | ||
print(cleaned_text) | ||
except Exception as e: | ||
print(f"An error occurred: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import logging, traceback | ||
from gradio_client import Client | ||
import re | ||
|
||
from utils.common import Common | ||
from utils.logger import Configure_logger | ||
|
||
|
||
class LLM_TPU: | ||
def __init__(self, data): | ||
self.common = Common() | ||
# 日志文件路径 | ||
file_path = "./log/log-" + self.common.get_bj_time(1) + ".txt" | ||
Configure_logger(file_path) | ||
|
||
self.config_data = data | ||
self.history = [] | ||
|
||
self.history_enable = data["history_enable"] | ||
self.history_max_len = data["history_max_len"] | ||
|
||
|
||
def get_resp(self, data): | ||
"""请求对应接口,获取返回值 | ||
Args: | ||
data (dict): 你的提问等 | ||
Returns: | ||
str: 返回的文本回答 | ||
""" | ||
try: | ||
client = Client(self.config_data["api_ip_port"]) | ||
|
||
result = client.predict( | ||
input=data["prompt"], | ||
chatbot=self.history, | ||
max_length=self.config_data["max_length"], | ||
top_p=self.config_data["top_p"], | ||
temperature=self.config_data["temperature"], | ||
api_name="/predict" | ||
) | ||
|
||
response_text = result[-1][1] | ||
# Remove <p> and </p> tags using regex | ||
resp_content = re.sub(r'</?p>', '', response_text) | ||
|
||
self.history = result | ||
|
||
# 启用历史就给我记住! | ||
if self.history_enable: | ||
while True: | ||
# 获取嵌套列表中所有字符串的字符数 | ||
total_chars = sum(len(string) for sublist in self.history for string in sublist) | ||
# 如果大于限定最大历史数,就剔除第一个元素 | ||
if total_chars > self.history_max_len: | ||
self.history.pop(0) | ||
else: | ||
break | ||
|
||
return resp_content | ||
except Exception as e: | ||
logging.error(traceback.format_exc()) | ||
return None | ||
|
||
if __name__ == '__main__': | ||
# 配置日志输出格式 | ||
logging.basicConfig( | ||
level=logging.DEBUG, # 设置日志级别,可以根据需求调整 | ||
format="%(asctime)s [%(levelname)s] %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S", | ||
) | ||
|
||
data = { | ||
"api_ip_port": "http://127.0.0.1:8003/", | ||
"max_length": 1, | ||
"top_p": 0.8, | ||
"temperature": 0.95, | ||
"history_enable": True, | ||
"history_max_len": 300 | ||
} | ||
|
||
llm_tpu = LLM_TPU(data) | ||
logging.info(f'{llm_tpu.get_resp("你可以扮演猫娘吗,每句话后面加个喵")}') | ||
logging.info(f'{llm_tpu.get_resp("早上好")}') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters