kanli commited on
Commit
d5b8a16
·
1 Parent(s): 8a47f68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -58
app.py CHANGED
@@ -1,61 +1,56 @@
1
- # from transformers import AutoModelForCausalLM, AutoTokenizer
2
- # import gradio as gradio
3
- # import torch
4
- # title = "🤖AI ChatBot"
5
- # description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
6
- # examples = [["How are you?"]]
7
- # tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
8
- # model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
9
- # def predict(input, history=[]):
10
- # # tokenize the new input sentence
11
- # new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt" )
12
- # # append the new user input tokens to the chat history
13
- # bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
14
- # # generate a response
15
- # history = model.generate(bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id).tolist()
16
- # # convert the tokens to text, and then split the responses into lines
17
- # response = tokenizer.decode(history[0]).split("<|endoftext|>")
18
- # # print('decoded_response-->>'+str(response))
19
- # response = [(response[i], response[i + 1]) for i in range(0,len(response) - 1, 2)]
20
- # # convert to tuples of list
21
- # # print('response-->>'+str(response))
22
- # return response, history
23
- # gradio.Interface(fn=predict,title=title,description=description,examples=examples,inputs=["text", "state"],outputs=["chatbot", "state"],theme="finlaymacklon/boxy_violet",).launch(share=True)
24
-
25
-
26
-
27
- import gradio as gr
28
- def add_text(history, text):
29
- global messages #message[list] is defined globally
30
- history = history + [(text,'')]
31
- messages = messages + [{"role":'user', 'content': text}]
32
- return history, ""
33
- def generate_response(history, model):
34
- global messages
35
-
36
- response = openai.ChatCompletion.create(
37
- model = model,
38
- messages=messages,
39
- temperature=0.2,
40
- )
41
-
42
- response_msg = response.choices[0].message.content
43
- messages = messages + [{"role":'assistant', 'content': response_msg}]
44
 
45
- for char in response_msg:
46
- history[-1][1] += char
47
- #time.sleep(0.05)
48
- yield history
49
-
50
-
51
- with gr.Blocks() as demo:
52
- radio = gr.Radio(value='gpt-3.5-turbo', choices=['gpt-3.5-turbo','gpt-4'], label='models')
53
- chatbot = gr.Chatbot(value=[],elem_id="chatbot")
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  with gr.Row():
55
- with gr.Column(scale=1):
56
- txt = gr.Textbox(show_label=False,placeholder="Enter text and press enter",)
 
 
 
57
 
58
- txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
59
- generate_response, inputs =[chatbot,radio],outputs = chatbot,)
60
-
61
- demo.queue()
 
1
+ import openai
2
+ import os
3
+ import gradio as gr
4
+
5
+ # 设置 OpenAI API 密钥
6
+ openai.api_key = "sk-NYsoG3VBKDiTuvdtC969F95aFc4f45379aD3854a93602327"
7
+
8
+ class Conversation:
9
+ def __init__(self, prompt, num_of_round):
10
+ self.prompt = prompt
11
+ self.num_of_round = num_of_round
12
+ self.messages = [{"role": "system", "content": self.prompt}]
13
+
14
+ def ask(self, question):
15
+ try:
16
+ self.messages.append({"role": "user", "content": question})
17
+ response = openai.ChatCompletion.create(
18
+ model="gpt-3.5-turbo",
19
+ messages=self.messages,
20
+ temperature=0.5,
21
+ max_tokens=2048,
22
+ top_p=1,
23
+ )
24
+ except Exception as e:
25
+ print(e)
26
+ return str(e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ message = response["choices"][0]["message"]["content"]
29
+ self.messages.append({"role": "assistant", "content": message})
30
+
31
+ if len(self.messages) > self.num_of_round * 2 + 1:
32
+ del self.messages[1:3] # 移除第一轮对话
33
+ return message
34
+
35
+
36
+ prompt = """你是一个大数据和AI领域的专家,用中文回答大数据和AI的相关问题。你的回答需要满足以下要求:
37
+ 1. 你的回答必须是中文
38
+ 2. 回答限制在100个字以内"""
39
+ conv = Conversation(prompt, 6)
40
+ def answer(question, history=[]):
41
+ history.append(question)
42
+ message = conv.ask(question)
43
+ history.append(message)
44
+ responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
45
+ print(responses)
46
+ return responses, history
47
+ with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as rxbot:
48
+ chatbot = gr.Chatbot(elem_id="chatbot")
49
+ state = gr.State([])
50
  with gr.Row():
51
+ txt = gr.Textbox(show_label=False, placeholder="请输入你的问题").style(container=False)
52
+ txt.submit(answer, [txt, state], [chatbot, state])
53
+
54
+ rxbot.launch()
55
+
56