Libraone commited on
Commit
94459c0
·
1 Parent(s): 076df5a

Add application file

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