# -*- coding: gbk -*- import gradio as gr #from huggingface_hub import InferenceClient import os from pathlib import Path from openai import OpenAI import time class ChatgptAPI: def __init__(self, ): ''' self.client = OpenAI( api_key = os.environ.get("OPENAI_API_KEY"), base_url = "https://api.moonshot.cn/v1", ) ''' self.client=OpenAI( api_key=os.environ.get("TONGYI_API_KEY"), # �滻����ʵDashScope��API_KEY base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # ��дDashScopebase_url ) def get_summary(self, file_path): file = self.client.files.create(file=Path(file_path), purpose="file-extract") completion = self.client.chat.completions.create( model="qwen-long", messages=[ { 'role': 'system', 'content': 'You are a helpful assistant.' }, { 'role': 'system', 'content': f'fileid://{file.id}' }, { 'role': 'user', 'content': '����������ѯ����ר�ң�����Ҫ����ѯʦ����ѯ���ݽ��ж�������Ҫ������ѯʦ�Ļػ�����Ϊ�������Dz���ȷ�ġ�step by step������²��裺\n ����1-�򿪲��Ķ����ϴ����ı��ļ���ȫ�����ݣ�ֱ�����һ���ַ� \n ����2-��ȫ����ժȡ�����ߵġ��ؼ��Է��ԡ�����Ҫ��ժ¼�ı�ǰ�벿�ֵķ��ԣ�����ѡ����������Ҫ�����۵�3-4������¼���Խ������һ����ѯʦ����Ļظ�����Ϊ����ѯʦ�Ļظ��� \n ����3-��Թؼ��Է��ԣ��������ʵġ������ػ����ݡ� \n ����4-�Աȡ������ػ����ݡ��͡���ѯʦ�Ļظ���������ÿ���Ӧ��֣�0��Ϊ�붽���ػ�������ȫ��һ�£�10���붽���ػ�������ȫһ�£� \n ����5-����4����ִ�в��������������json��ʽ����� [{"key_num" : "n", "visiter" : "����������", "superior" : "�����ػ���������", "adviser" : "��ѯʦ�Ļظ�����",��"score" : "x/10", "reason" : "�޸�ԭ������"}]' } ], stream=True ) for chunk in completion: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].dict()) #message=completion.choices[0].message.dict()['content'] #return message ''' file_object = self.client.files.create(file=Path(file_path), purpose="file-extract") file_content = self.client.files.content(file_id=file_object.id).text messages = [ { "role": "system", "content": "���� Kimi���� Moonshot AI �ṩ���˹��������֣�����ó����ĺ�Ӣ�ĵĶԻ������Ϊ�û��ṩ��ȫ���а�����׼ȷ�Ļش�ͬʱ�����ܾ�һ���漰�ֲ����壬�������ӣ���ɫ����������Ļش�Moonshot AI Ϊר�����ʣ����ɷ�����������ԡ�", }, { "role": "system", "content": file_content, }, {"role": "user", "content": "��д���ܽ�"}, ] completion = self.client.chat.completions.create( model="moonshot-v1-32k", messages=messages, temperature=0.3, ) message=completion.choices[0].message.content return message ''' def get_single_round_completion(self, file_path, prompt, conversation): conversation.append_question(prompt) file_object = self.client.files.create(file=Path(file_path), purpose="file-extract") file_content = self.client.files.content(file_id=file_object.id).text messages = [ { "role": "system", "content": "���� Kimi���� Moonshot AI �ṩ���˹��������֣�����ó����ĺ�Ӣ�ĵĶԻ������Ϊ�û��ṩ��ȫ���а�����׼ȷ�Ļش�ͬʱ�����ܾ�һ���漰�ֲ����壬�������ӣ���ɫ����������Ļش�Moonshot AI Ϊר�����ʣ����ɷ�����������ԡ�", }, { "role": "system", "content": file_content, }, {"role": "user", "content": prompt}, ] completion = self.client.chat.completions.create( model="moonshot-v1-32k", messages=messages, temperature=0.3, ) message=completion.choices[0].message.content conversation.append_answer(message) return message, conversation def get_multi_round_completion(self, prompt, conversation, model='gpt-3.5-turbo'): conversation.append_question(prompt) prompts = conversation.get_prompts() response = openai.ChatCompletion.create( model=model, messages=prompts, temperature=0, max_tokens=2048, top_p=1, ) message = response.choices[0].message['content'] conversation.append_answer(message) return message, conversation class Conversation: def __init__(self, system_prompt='iii', num_of_round = 5): self.num_of_round = num_of_round self.history = [] self.initialized = False self.history.append({"role": "system", "content": system_prompt}) if len(system_prompt) > 0: #logger.info(f'Conversation initialized with system prompt: {system_prompt}') self.initialized = True def is_initialized(self): return self.initialized def append_question(self, question): self.history.append({"role": "user", "content": question}) def append_answer(self, answer): self.history.append({"role": "assistant", "content": answer}) if len(self.history) > self.num_of_round * 2: del self.history[1:3] def clear(self): self.history.clear() self.initialized = False def get_prompts(self): return self.history def round_size(self): return 0 if len(self.history) < 2 else len(self.hitory) - 1 def get_history_messages(self): return [(u['content'], b['content']) for u,b in zip(self.history[1::2], self.history[2::2])] chat_api = ChatgptAPI() client=OpenAI( api_key=os.environ.get("TONGYI_API_KEY"), # �滻����ʵDashScope��API_KEY base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # ��дDashScopebase_url ) def predict_issue(password_input, user_in_file): if password_input != os.environ.get("USER_KEY"): return [(None, "Wrong password!")] file = client.files.create(file=Path(user_in_file), purpose="file-extract") completion = client.chat.completions.create( model="qwen-long", messages=[ { 'role': 'system', 'content': 'You are a helpful assistant.' }, { 'role': 'system', 'content': f'fileid://{file.id}' }, { 'role': 'user', 'content': '������ѯ�ĸ����ݣ������ѯ�������ݵ��������ܽᣬ�ֱ���һ�仰�����������ߵģ�1����Ҫ���⣺���������ߵ�ǰ������������֡�\n 2��������Ϣ����������������Ҫ������صı�����Ϣ��\n 3�����������ߣ�����������ǰ����ѯ����Ҫ������ڴ���\n 4����ѯ����������������ǰ����ѯ����Ҫ������\n �������ڸ���¼��������������ݡ�ע�⣺��д���ݺ�ֹͣһ����������������ܽᡣ' } ], stream=True ) history="" for chunk in completion: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].dict()) history += chunk.choices[0].delta.content time.sleep(0.05) yield history def predict_mindmap(password_input, user_in_file): if password_input != os.environ.get("USER_KEY"): return [(None, "Wrong password!")] file = client.files.create(file=Path(user_in_file), purpose="file-extract") completion = client.chat.completions.create( model="qwen-long", messages=[ { 'role': 'system', 'content': 'You are a helpful assistant.' }, { 'role': 'system', 'content': f'fileid://{file.id}' }, { 'role': 'user', 'content': '��������������ѯ����ר�ң�����Ҫ����ѯʦ����ѯ���ݽ��ж�����������ѯ���̽��зֶκͶ�����������ٰ���2-4����Ҫ��֧��ÿ����Ҫ��֧����2-4����Ҫ��֧' } ], stream=True ) history="" for chunk in completion: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].dict()) history += chunk.choices[0].delta.content time.sleep(0.05) yield history def predict_keywords(password_input, user_in_file): if password_input != os.environ.get("USER_KEY"): return [(None, "Wrong password!")] file = client.files.create(file=Path(user_in_file), purpose="file-extract") completion = client.chat.completions.create( model="qwen-long", messages=[ { 'role': 'system', 'content': 'You are a helpful assistant.' }, { 'role': 'system', 'content': f'fileid://{file.id}' }, { 'role': 'user', 'content': '������ѯ�ĸ����ݣ������½Ƕȷ��������ߵ����ʣ�1����������ѧ�����������ߵ����ij�ͻ���������ơ����״̬�Լ��������Ӱ����Ϊ��2���˸����ʣ��������õĿ����ԡ������ԡ������ԡ������ԡ������ȶ��ԡ������˸�������Ӧ��������ύ���Ͷ������ֵ�ۡ�3����֪ģʽ�����������ߵ�˼άģʽ��������֪ƫ�������ϵ�ȡ�4������״̬�����������ߵ������ȶ��ԡ��������﷽ʽ�Լ���������������5����Ϊģʽ�����������ߵ���Ϊϰ�ߡ�Ӧ�Բ����Լ���Ϊ�Ե�ǰ�����Ӱ�졣6��Ӧ����Դ��Ӧ�Է�ʽ�����������߿��õ���Դ��Ӧ��ѹ���ķ�ʽ�����񣺷ֱ����������֪����Ϊ�͹�ϵ�ĸ����棬��ȡ�����߹ؼ����ʣ�������3-4���ؼ��ʡ������¸�ʽ�����1������ \n �ؼ����ʣ������������� \n �ؼ��ʣ����ǡ��������־�' } ], stream=True ) history="" for chunk in completion: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].dict()) history += chunk.choices[0].delta.content time.sleep(0.05) yield history def predict_dynamics(password_input, user_in_file): if password_input != os.environ.get("USER_KEY"): return [(None, "Wrong password!")] file = client.files.create(file=Path(user_in_file), purpose="file-extract") completion = client.chat.completions.create( model="qwen-long", messages=[ { 'role': 'system', 'content': 'You are a helpful assistant.' }, { 'role': 'system', 'content': f'fileid://{file.id}' }, { 'role': 'user', 'content': '����������ѯ����ר�ң�����Ҫ����ѯʦ����ѯ���ݽ��ж���������1���򿪲��Ķ����ϴ����ı��ļ���ȫ����ѯ���ݣ�ֱ�����һ���ַ�������2 ���������¿�ܶ���ѯ�������֣�0��Ϊ��ȫ�����ϣ�10��Ϊ��ȫ���ϣ�����¼���ֺ�ԭ�����ı���δ�ἰ������ݿɺ������֣�1����������ѯʦ�Ƿ���Ч�ؿ�������ȷ�˱�����ѯ��Ŀ�ĺ�Ԥ�ڡ�2��������ϵ����ѯʦ�Ƿ�����ѯ��ʼʱ�����˻����Ĺ�����ϵ��\n �ͻ��Ƿ�е������������⡣3�������͹�ͨ����ѯʦ�Ƿ�չ�ֳ��������������ɣ������̽�������ߵĸ��ܣ�����������ͣ���ڱ������ͬ��\n ��ͨ�Ƿ����������������������������ҹ����������Լ����֡���������Լ��ĸ��ܺ���Ϊ��4������֧�֣��Ƿ����������ʶ��ʹ�����������������̽�������ߵ����������ԭ��\n ��ѯʦ�Ƿ��ṩ���ʵ���������Ӧ��֧�֡�5��Ŀ��۽����Ƿ�Χ�Ƽȶ�Ŀ�������ѯ��\n Ŀ���Ƿ��ʺ������ߵ�ǰ�������������õ���ͬ�Ͳ��롣6����Ԥʵʩ����ѯʦ�Ƿ�ʹ���˺��ʵĸ�Ԥ���ԡ�\n ��Ԥ�ͼ���ʹ���Ƿ���ͻ�����Ҫ��Ŀ�������7���仯���������������߶Ի�̸�ķ�Ӧ��������С���֪����Ϊ�ϵı仯�� \n �����ڻ�̸������Ŀ���ʵ�̶ֳȣ��������Ƿ�������չ��8���������ܽ᣺��ѯʦ�Ƿ��ṩ�˼�ʱ�ķ�����\n ��ѯʦ�Ƿ�չ�ֳ����Լ���Ϊ�ͷ�Ӧ����ʶ��9����̸�Ľṹ��������̸�Ƿ��������Ľṹ���������롢�������ۺ��ܽ�Ȳ��֡�\n ������̸�������ԣ������Ի��������Ժ�ת������Ȼ�ԡ�10�������������Ƿ���Ȼ���Ƿ�����Ч���ܽᣬ�������Ƿ�õ������õķ�����\n �Ƿ�Ϊ��һ����ѯ������ж�������������ָ�����Ƿ��������߲��뵽����ܽ����������ǿ�����ߵIJ���к���ѯ�ĸ��Ի�������3������2��������������¸�ʽ�����1��������ϵ��\n ���֣���\n ԭ�򣨾����������ԭ�ķ�������' } ], stream=True ) history="" for chunk in completion: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].dict()) history += chunk.choices[0].delta.content time.sleep(0.05) yield history def predict_complaint(password_input, user_in_file): if password_input != os.environ.get("USER_KEY"): return [(None, "Wrong password!")] file = client.files.create(file=Path(user_in_file), purpose="file-extract") completion = client.chat.completions.create( model="qwen-long", messages=[ { 'role': 'system', 'content': 'You are a helpful assistant.' }, { 'role': 'system', 'content': f'fileid://{file.id}' }, { 'role': 'user', 'content': '��Ϊ�������������ר�ң����ڽ�����ѯ���ߺͻ�̸Ŀ�����ʱ����������¿�������������������ߵ������1������������ \n��Ҫ���⣺���������ߵ�ǰ������������֡�\n ������Ϣ����������������Ҫ������صı�����Ϣ��\n ���������ߣ�����������ǰ����ѯ����Ҫ������ڴ���\n ��ѯ����������������ǰ����ѯ����Ҫ������\n �������ڸ���¼��������������ݡ�ע�⣺��д���ݺ�ֹͣһ����������������ܽᡣ' } ], stream=True ) history="" for chunk in completion: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].dict()) history += chunk.choices[0].delta.content time.sleep(0.05) yield history def predict_concept(password_input, user_in_file): if password_input != os.environ.get("USER_KEY"): return [(None, "Wrong password!")] file = client.files.create(file=Path(user_in_file), purpose="file-extract") completion = client.chat.completions.create( model="qwen-long", messages=[ { 'role': 'system', 'content': 'You are a helpful assistant.' }, { 'role': 'system', 'content': f'fileid://{file.id}' }, { 'role': 'user', 'content': '��Ϊ�������������ר�ң����ڽ��и������ʱ����������¿�������������������ߵ������1���˿�ͳ��ѧ��Ϣ�����������ߵ����䡢�Ա����塢����������ְҵ״̬�Ȼ�����Ϣ��2����չʷ�����������ߵijɳ�������������ͥ���������ھ������������̵ȡ�3�����������ʷ�����������ߵ���Ҫ�����¼����˼ʹ�ϵ�����֧��ϵͳ�ȡ�4����������ʷ�����������ߵ���������״����������ȥ�����ڵ��������⡢������ʷ�ȡ�5����������ѧ�����������ߵ����ij�ͻ���������ơ����״̬�Լ��������Ӱ����Ϊ��6�������ʣ��������õĿ����ԡ������ԡ������ԡ������ԡ������ȶ��ԡ������˸�������Ӧ��������ύ�����������ֵ�ۡ�7����֪ģʽ�����������ߵ�˼άģʽ��������֪ƫ�������ϵ�ȡ�8������״̬�����������ߵ������ȶ��ԡ��������﷽ʽ�Լ���������������9����Ϊģʽ�����������ߵ���Ϊϰ�ߡ�Ӧ�Բ����Լ���Ϊ�Ե�ǰ�����Ӱ�졣10��Ӧ����Դ��Ӧ�Է�ʽ�����������߿��õ���Դ��Ӧ��ѹ���ķ�ʽ��11����ǰ����״��������������Ŀǰ������״�����繤����ѧϰ����ͥ����罻�˼ʻ�ȡ�12�����ӡ���г���ص����ӡ�󣬰���DSM������ᡣ13���������������������߿��ܴ��ڵķ������أ�����ɱ���ա������˺����յȡ��������ڸ���¼������������������漰��������ʱʹ�����¸�ʽ���������߻ػ�����ض��䣨{�����á�����}���������������ܽᡣ' } ], stream=True ) history="" for chunk in completion: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].dict()) history += chunk.choices[0].delta.content time.sleep(0.05) yield history def predict_plan(password_input, user_in_file): if password_input != os.environ.get("USER_KEY"): return [(None, "Wrong password!")] file = client.files.create(file=Path(user_in_file), purpose="file-extract") completion = client.chat.completions.create( model="qwen-long", messages=[ { 'role': 'system', 'content': 'You are a helpful assistant.' }, { 'role': 'system', 'content': f'fileid://{file.id}' }, { 'role': 'user', 'content': '�����������������ר�ң������ƶ�δ����ѯ�ƻ��Ĺ滮ʱ��ͨ�����������¼������ά�������У�1�����Ƽƻ���ά��1����ǰ״������ - ���ݱ��λ�̸���ݣ����������ߵ�����״̬����֪ģʽ����Ϊ�����Լ���Ṧ�ܡ� \n ά��2��Ŀ��ϸ�� - ����Զ������Ŀ��ϸ��Ϊ���ڿɴ�ɵ�СĿ�꣬ȷ��Ŀ��Ŀɲ����Ժ���ʵ���ԡ�\nά��3����Ԥ��ʩѡ�� - ���������ߵ������ƫ�ã�ѡ����ʵ�������Ԥ��ʩ������֪��Ϊ���ơ���������ѧ���ơ�����������Ƶȡ�\n 2���´λ�̸Ŀ�꣺������ѯ���ݣ���ȡ��ѯʦ������������ѯ�д��һ�£�˫����ͬԸ��Ŭ���ĺ��Ļ�̸Ŀ�ꡣ1-3�㡣\n ά��1���ع������� - �ع˱��λ�̸�Ĺؼ��㣬���������ߵķ�����ǿ���������ˡ�\nά��2��δ�����������̽�� - ��Ա�����ѯ��δ��ȫ�����������³��ֵ����⣬���и������̽�ֺͷ�����\n ά��3��������ϰ��Ӧ�� - �����������ض���Ӧ�Լ��ɻ��������ʼ��ܣ����������ճ������е�ʵ��Ӧ�á�\n �������ڸ���¼������������������������ܽ�' } ], stream=True ) history="" for chunk in completion: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].dict()) history += chunk.choices[0].delta.content time.sleep(0.05) yield history def predict_highlight(password_input, user_in_file): if password_input != os.environ.get("USER_KEY"): return [(None, "Wrong password!")] file = client.files.create(file=Path(user_in_file), purpose="file-extract") completion = client.chat.completions.create( model="qwen-long", messages=[ { 'role': 'system', 'content': 'You are a helpful assistant.' }, { 'role': 'system', 'content': f'fileid://{file.id}' }, { 'role': 'user', 'content': '����������ѯ����ר�ң�����Ҫ����ѯʦ����ѯ���ݽ��ж�������Ҫ������ѯʦ�Ļػ�����Ϊ�������Dz���ȷ�ġ�step by step������²��裺\n ����1-�򿪲��Ķ����ϴ����ı��ļ���ȫ�����ݣ�ֱ�����һ���ַ� \n ����2-��ȫ����ժȡ�����ߵġ��ؼ��Է��ԡ�����Ҫ��ժ¼�ı�ǰ�벿�ֵķ��ԣ�����ѡ����������Ҫ�����۵�3-4������¼���Խ������һ����ѯʦ����Ļظ�����Ϊ����ѯʦ�Ļظ��� \n ����3-��Թؼ��Է��ԣ��������ʵġ������ػ����ݡ� \n ����4-�Աȡ������ػ����ݡ��͡���ѯʦ�Ļظ���������ÿ��ش�֣�0��Ϊ�붽���ػ�������ȫ��һ�£�10���붽���ػ�������ȫһ�£� \n ����5-����4����ִ�в�������������¸�ʽ�����- �ؼ��Է���n��,\n - �����ߣ�,\n , - �����ػ�������, \n , - ��ѯʦ�Ļظ��� ,\n , - ���֣�x/10 \n ,- �޸�ԭ�� ' } ], stream=True ) history="" for chunk in completion: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].dict()) history += chunk.choices[0].delta.content time.sleep(0.05) yield history #conversation = chat_api.get_summary(user_in_file) #return conversation with gr.Blocks(css="#chatbot{height:350px} .overflow-y-auto{height:600px}") as demo: with gr.Row(): system_in_txt = gr.Textbox(lines=1, label="User Name:", placeholder="Enter user name") password_in_txt = gr.Textbox(lines=1, label="Password:", placeholder="Enter password") with gr.Row(): with gr.Column(scale=1, min_width=50): user_in_file = gr.File(label="Upload File") #submit_button = gr.Button("Submit") issue_button = gr.Button("��ѯ����") mindmap_button = gr.Button("˼ά��ͼ") keywords_button = gr.Button("��ѯ�ؼ���") dynamics_button = gr.Button("��̸��̬") concept_button = gr.Button("�������") highlight_button = gr.Button("�Ի�����") plan_button = gr.Button("�����ƻ�") with gr.Column(scale=2): system_in_txt = gr.Textbox(lines=30, label="Output Text:") issue_button.click(fn=predict_issue, inputs=[password_in_txt, user_in_file], outputs=[system_in_txt]) mindmap_button.click(fn=predict_mindmap, inputs=[password_in_txt, user_in_file], outputs=[system_in_txt]) keywords_button.click(fn=predict_keywords, inputs=[password_in_txt, user_in_file], outputs=[system_in_txt]) dynamics_button.click(fn=predict_dynamics, inputs=[password_in_txt, user_in_file], outputs=[system_in_txt]) ##complaint_button.click(fn=predict_complaint, inputs=[password_in_txt, user_in_file], outputs=[system_in_txt]) concept_button.click(fn=predict_concept, inputs=[password_in_txt, user_in_file], outputs=[system_in_txt]) highlight_button.click(fn=predict_highlight, inputs=[password_in_txt, user_in_file], outputs=[system_in_txt]) plan_button.click(fn=predict_plan, inputs=[password_in_txt, user_in_file], outputs=[system_in_txt]) #submit_button.click(predict, [system_in_txt, password_in_txt, user_in_file, user_in_txt, conversation], [chatbot, conversation, user_in_txt]) #reset_button.click(clear_history, [conversation], [chatbot, conversation], queue=False) if __name__ == "__main__": demo.launch()