Taocan commited on
Commit
211d139
·
1 Parent(s): 94a19ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -23
app.py CHANGED
@@ -31,16 +31,37 @@ class Conversation:
31
  del self.messages[1:3]
32
  return message
33
 
34
- def add_text(history, text):
35
- history = history + [(text, None)]
36
- return history, gr.update(value="", interactive=False)
37
 
38
- def add_file(history, file):
39
- history = history + [((file.name,), None)]
40
- return history
41
 
42
- def bot(history, chatbot):
43
- prompt = ". ".join([m["content"] for m in history if m["role"] == "user"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  response = openai.Completion.create(
45
  engine="davinci",
46
  prompt=prompt,
@@ -49,16 +70,13 @@ def bot(history, chatbot):
49
  top_p=1.0
50
  )
51
  message = response["choices"][0]["text"]
52
- chatbot.add_message(content=message, role="assistant")
 
 
 
53
 
54
- def answer(question, history=None):
55
- if history is None:
56
- history = []
57
- history.append(question)
58
- response = conv.ask(question)
59
- history.append(response)
60
- responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
61
- return responses, history
62
 
63
 
64
  prompt = """假如你是GPT-4,你可以回答用户提问的任何问题"""
@@ -100,12 +118,21 @@ with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as d
100
  with gr.Column(scale=0.15, min_width=0):
101
  btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
102
 
103
- txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
104
- bot, chatbot, chatbot
 
 
 
 
 
 
 
 
 
105
  )
106
- txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
107
- file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
108
- bot, chatbot, chatbot
 
109
  )
110
-
111
  demo.launch()
 
31
  del self.messages[1:3]
32
  return message
33
 
34
+ # def add_text(history, text):
35
+ # history = history + [(text, None)]
36
+ # return history, gr.update(value="", interactive=False)
37
 
38
+ # def add_file(history, file):
39
+ # history = history + [((file.name,), None)]
40
+ # return history
41
 
42
+ # def bot(history, chatbot):
43
+ # prompt = ". ".join([m["content"] for m in history if m["role"] == "user"])
44
+ # response = openai.Completion.create(
45
+ # engine="davinci",
46
+ # prompt=prompt,
47
+ # temperature=0.5,
48
+ # max_tokens=100,
49
+ # top_p=1.0
50
+ # )
51
+ # message = response["choices"][0]["text"]
52
+ # chatbot.add_message(content=message, role="assistant")
53
+
54
+ # def answer(question, history=None):
55
+ # if history is None:
56
+ # history = []
57
+ # history.append(question)
58
+ # response = conv.ask(question)
59
+ # history.append(response)
60
+ # responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
61
+ # return responses, history
62
+
63
+ def on_text_submit(chatbot, txt):
64
+ prompt = ". ".join([m[0] for m in history if m[1] == "user"])
65
  response = openai.Completion.create(
66
  engine="davinci",
67
  prompt=prompt,
 
70
  top_p=1.0
71
  )
72
  message = response["choices"][0]["text"]
73
+ chatbot.add(message, "assistant")
74
+
75
+ def on_file_upload(chatbot, btn):
76
+ chatbot.add((btn.uploaded_file.name,), "user")
77
 
78
+ with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as demo:
79
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
 
 
 
 
 
 
80
 
81
 
82
  prompt = """假如你是GPT-4,你可以回答用户提问的任何问题"""
 
118
  with gr.Column(scale=0.15, min_width=0):
119
  btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
120
 
121
+ # txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
122
+ # bot, chatbot, chatbot
123
+ # )
124
+ # txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
125
+ # file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
126
+ # bot, chatbot, chatbot
127
+ # )
128
+ txt_msg = txt.submit(on_text_submit, [chatbot, txt], [chatbot, txt]).then(
129
+ None,
130
+ None,
131
+ [chatbot]
132
  )
133
+ file_msg = btn.upload(on_file_upload, [chatbot, btn], [chatbot]).then(
134
+ None,
135
+ None,
136
+ [chatbot]
137
  )
 
138
  demo.launch()