Spaces:
Sleeping
Sleeping
File size: 4,375 Bytes
94459c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
from json import JSONDecodeError
import gradio as gr
from openai import OpenAI
import os
import json
from typing import *
import sys
class Chat:
chat_history = []
like_history = []
dislike_history = []
max_chat_round = -1
round = 0
def __init__(self):
pass
def init_chat_history(self):
prompt = f'''关于哪吒的提示词'''
self.chat_history = [
{
"role":"system",
"content":prompt
}
]
self.round = 0
def add_to_chat_history(self, chat: dict):
self.chat_history.append(chat)
def remove_from_chat_history(self, chat: dict):
pass
def get_chat_history(self):
return self.chat_history
def add_to_like_history(self, index: List):
self.like_history.append(index)
def remove_from_like_history(self, index: List):
pass
def add_to_dislike_history(self, index: List):
self.dislike_history.append(index)
def remove_from_like_history(self, index: List):
pass
def format(self) -> List[dict]:
result = self.chat_history
for chat in result:
chat["liked"] = 0
for like in self.like_history:
like_index = like[0] * 2 + like[1]
result[like_index]["liked"] = 1
for dislike in self.dislike_history:
dislike_index = dislike[0] * 2 + dislike[1]
result[dislike_index]["liked"] = -1
return result
def save(self, file_path: str):
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(self.format(), file, ensure_ascii=False, indent=4)
file.close()
def round_increase(self):
self.round += 1
if self.max_chat_round == -1:
return True
elif self.round == self.max_chat_round:
return False
chat = Chat()
def save_single_data(instruction: str, input: str, output: str, file_path: str):
data = [
{
"instruction": instruction,
"input": input,
"output": output,
}
]
# 尝试读取现有的JSON数据,如果文件不存在则创建一个空列表
try:
with open(file_path, 'r', encoding='utf-8') as file:
# 读取JSON数据并转换为Python列表
original_data = json.load(file)
print(original_data)
file.close()
except FileNotFoundError:
# 如果文件不存在,初始化一个空列表
original_data = []
except ValueError:
# 文件存在单内容为空,捕获报错
original_data = []
# 将新数据追加到现有的数据列表中
original_data.extend(data)
# 将更新后的数据写回JSON文件
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(original_data, file, ensure_ascii=False, indent=4)
file.close()
# def response(message, history):
# client = OpenAI(
# api_key=os.getenv("KIMI_API_KEY"),
# base_url="https://api.moonshot.cn/v1",
# )
#
# chat.add_to_chat_history({"role": "user", "content": message})
# messages = chat.get_chat_history()
# completion = client.chat.completions.create(
# model="moonshot-v1-8k",
# messages=messages,
# temperature=0.3,
# )
#
# chat.add_to_chat_history({"role": "assistant", "content": completion.choices[0].message.content})
# chat.round_increase()
# return completion.choices[0].message.content
def response(message, history):
return "未接入LLM..."
def vote(data: gr.LikeData):
if data.liked:
chat.add_to_like_history(data.index)
else:
chat.add_to_dislike_history(data.index)
def end():
# 刷新界面 保存数据
path = 'test.json'
chat.save(path)
# chat.init_chat_history()
theme = gr.themes.Base()
with gr.Blocks(theme=theme) as demo:
chatbot = gr.Chatbot(height=500,placeholder="<strong>哪吒-魔童降世</strong><br>Chat with Me", type='tuples')
chatbot.like(vote, None, None)
gr.ChatInterface(fn=response, chatbot=chatbot, show_progress='full',retry_btn=None,undo_btn=None)
end_btn = gr.Button("Upload Chat Data")
end_btn.click(end)
demo.launch()
# if __name__=='__main__':
# path = 'test.json'
# save_single_data('asd','asd','asdd',path)
|