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
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
#from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
|
| 8 |
+
class ChatgptAPI:
|
| 9 |
+
def __init__(self, ):
|
| 10 |
+
self.client = OpenAI(
|
| 11 |
+
api_key = os.environ.get("OPENAI_API_KEY"),
|
| 12 |
+
base_url = "https://api.moonshot.cn/v1",
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_summary(self, file_path):
|
| 17 |
+
file_object = self.client.files.create(file=Path(file_path), purpose="file-extract")
|
| 18 |
+
file_content = self.client.files.content(file_id=file_object.id).text
|
| 19 |
+
messages = [
|
| 20 |
+
{
|
| 21 |
+
"role": "system",
|
| 22 |
+
"content": "���� Kimi���� Moonshot AI �ṩ���˹��������֣�����ó����ĺ�Ӣ�ĵĶԻ������Ϊ�û��ṩ��ȫ���а�����ȷ�Ļش�ͬʱ�����ܾ�һ���漰�ֲ����壬�������ӣ���ɫ����������Ļش�Moonshot AI Ϊר�����ʣ����ɷ�����������ԡ�",
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"role": "system",
|
| 26 |
+
"content": file_content,
|
| 27 |
+
},
|
| 28 |
+
{"role": "user", "content": "��д���ܽ�"},
|
| 29 |
+
]
|
| 30 |
+
completion = self.client.chat.completions.create(
|
| 31 |
+
model="moonshot-v1-32k",
|
| 32 |
+
messages=messages,
|
| 33 |
+
temperature=0.3,
|
| 34 |
+
)
|
| 35 |
+
message=completion.choices[0].message.content
|
| 36 |
+
return message
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def get_single_round_completion(self, file_path, prompt, conversation):
|
| 40 |
+
conversation.append_question(prompt)
|
| 41 |
+
file_object = self.client.files.create(file=Path(file_path), purpose="file-extract")
|
| 42 |
+
file_content = self.client.files.content(file_id=file_object.id).text
|
| 43 |
+
messages = [
|
| 44 |
+
{
|
| 45 |
+
"role": "system",
|
| 46 |
+
"content": "���� Kimi���� Moonshot AI �ṩ���˹��������֣�����ó����ĺ�Ӣ�ĵĶԻ������Ϊ�û��ṩ��ȫ���а�����ȷ�Ļش�ͬʱ�����ܾ�һ���漰�ֲ����壬�������ӣ���ɫ����������Ļش�Moonshot AI Ϊר�����ʣ����ɷ�����������ԡ�",
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"role": "system",
|
| 50 |
+
"content": file_content,
|
| 51 |
+
},
|
| 52 |
+
{"role": "user", "content": prompt},
|
| 53 |
+
]
|
| 54 |
+
completion = self.client.chat.completions.create(
|
| 55 |
+
model="moonshot-v1-32k",
|
| 56 |
+
messages=messages,
|
| 57 |
+
temperature=0.3,
|
| 58 |
+
)
|
| 59 |
+
message=completion.choices[0].message.content
|
| 60 |
+
conversation.append_answer(message)
|
| 61 |
+
return message, conversation
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def get_multi_round_completion(self, prompt, conversation, model='gpt-3.5-turbo'):
|
| 65 |
+
conversation.append_question(prompt)
|
| 66 |
+
prompts = conversation.get_prompts()
|
| 67 |
+
|
| 68 |
+
response = openai.ChatCompletion.create(
|
| 69 |
+
model=model,
|
| 70 |
+
messages=prompts,
|
| 71 |
+
temperature=0,
|
| 72 |
+
max_tokens=2048,
|
| 73 |
+
top_p=1,
|
| 74 |
+
)
|
| 75 |
+
message = response.choices[0].message['content']
|
| 76 |
+
conversation.append_answer(message)
|
| 77 |
+
|
| 78 |
+
return message, conversation
|
| 79 |
+
|
| 80 |
+
class Conversation:
|
| 81 |
+
def __init__(self, system_prompt='iii', num_of_round = 5):
|
| 82 |
+
self.num_of_round = num_of_round
|
| 83 |
+
self.history = []
|
| 84 |
+
self.initialized = False
|
| 85 |
+
self.history.append({"role": "system", "content": system_prompt})
|
| 86 |
+
|
| 87 |
+
if len(system_prompt) > 0:
|
| 88 |
+
#logger.info(f'Conversation initialized with system prompt: {system_prompt}')
|
| 89 |
+
self.initialized = True
|
| 90 |
+
|
| 91 |
+
def is_initialized(self):
|
| 92 |
+
return self.initialized
|
| 93 |
+
|
| 94 |
+
def append_question(self, question):
|
| 95 |
+
self.history.append({"role": "user", "content": question})
|
| 96 |
+
|
| 97 |
+
def append_answer(self, answer):
|
| 98 |
+
self.history.append({"role": "assistant", "content": answer})
|
| 99 |
+
|
| 100 |
+
if len(self.history) > self.num_of_round * 2:
|
| 101 |
+
del self.history[1:3]
|
| 102 |
+
|
| 103 |
+
def clear(self):
|
| 104 |
+
self.history.clear()
|
| 105 |
+
self.initialized = False
|
| 106 |
+
|
| 107 |
+
def get_prompts(self):
|
| 108 |
+
return self.history
|
| 109 |
+
|
| 110 |
+
def round_size(self):
|
| 111 |
+
return 0 if len(self.history) < 2 else len(self.hitory) - 1
|
| 112 |
+
|
| 113 |
+
def get_history_messages(self):
|
| 114 |
+
return [(u['content'], b['content']) for u,b in zip(self.history[1::2], self.history[2::2])]
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
chat_api = ChatgptAPI()
|
| 119 |
+
|
| 120 |
+
def predict(password_input, user_in_file):
|
| 121 |
+
if password_input != os.environ.get("USER_KEY"):
|
| 122 |
+
return [(None, "Wrong password!")], conversation, user_input
|
| 123 |
+
|
| 124 |
+
conversation = chat_api.get_summary(user_in_file)
|
| 125 |
+
return conversation
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
with gr.Blocks(css="#chatbot{height:350px} .overflow-y-auto{height:600px}") as demo:
|
| 132 |
+
|
| 133 |
+
with gr.Row():
|
| 134 |
+
system_in_txt = gr.Textbox(lines=1, label="User Name:", placeholder="Enter user name")
|
| 135 |
+
password_in_txt = gr.Textbox(lines=1, label="Password:", placeholder="Enter password")
|
| 136 |
+
|
| 137 |
+
with gr.Row():
|
| 138 |
+
with gr.Column(scale=1, min_width=50):
|
| 139 |
+
user_in_file = gr.File(label="Upload File")
|
| 140 |
+
#submit_button = gr.Button("Submit")
|
| 141 |
+
summary_button = gr.Button("Summary")
|
| 142 |
+
target_button = gr.Button("Target")
|
| 143 |
+
|
| 144 |
+
with gr.Column(scale=2):
|
| 145 |
+
system_in_txt = gr.Textbox(lines=1, label="Output Text:")
|
| 146 |
+
|
| 147 |
+
run_button.click(fn=predict, inputs=[password_in_txt, user_in_file], outputs=[system_in_txt])
|
| 148 |
+
|
| 149 |
+
#submit_button.click(predict, [system_in_txt, password_in_txt, user_in_file, user_in_txt, conversation], [chatbot, conversation, user_in_txt])
|
| 150 |
+
#reset_button.click(clear_history, [conversation], [chatbot, conversation], queue=False)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
if __name__ == "__main__":
|
| 154 |
+
demo.launch()
|
| 155 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio==3.40.0
|
| 3 |
+
huggingface_hub==0.22.2
|