File size: 4,630 Bytes
a66eb4e 87a9162 a66eb4e 87a9162 a66eb4e 87a9162 a66eb4e 2c45733 87a9162 a66eb4e 87a9162 28b188b 87a9162 28b188b 5d6a72c 28b188b 211d139 28b188b 211d139 94a19ca 211d139 5d6a72c 211d139 9b1476f a66eb4e 87a9162 e80c871 8b63e4b 2cc1d0a 723f60f e80c871 87a9162 e80c871 87a9162 4db7343 e80c871 723f60f fe3bb6d 8fbe34b 723f60f fe3bb6d e80c871 94a19ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import openai
import os
import gradio as gr
openai.api_key = os.environ.get("OPENAI_API_KEY")
class Conversation:
def __init__(self, prompt, num_of_round):
self.prompt = prompt
self.num_of_round = num_of_round
self.messages = []
self.messages.append({"role": "system", "content": self.prompt})
def ask(self, question):
try:
self.messages.append({"role": "user", "content": question})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=self.messages,
temperature=0.5,
max_tokens=256,
top_p=1,
)
except Exception as e:
print(e)
return e
message = response["choices"][0]["message"]["content"]
self.messages.append({"role": "assistant", "content": message})
if len(self.messages) > 3:
del self.messages[1:3]
return message
def add_text(history, text):
history = history + [(text, None)]
return history, gr.update(value="", interactive=False)
def add_file(history, file):
history = history + [((file.name,), None)]
return history
def bot(history, chatbot):
prompt = ". ".join([m["content"] for m in history if m["role"] == "user"])
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
temperature=0.5,
max_tokens=100,
top_p=1.0
)
message = response["choices"][0]["text"]
chatbot.add_message(content=message, role="assistant")
def answer(question, history=None):
if history is None:
history = []
history.append(question)
response = conv.ask(question)
history.append(response)
responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
return responses, history
def on_text_submit(chatbot, txt):
prompt = ". ".join([m[0] for m in history if m[1] == "user"])
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
temperature=0.5,
max_tokens=100,
top_p=1.0
)
message = response["choices"][0]["text"]
chatbot.add(message, "assistant")
def on_file_upload(chatbot, btn):
chatbot.add((btn.uploaded_file.name,), "user")
with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as demo:
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
prompt = """假如你是GPT-4,你可以回答用户提问的任何问题"""
conv = Conversation(prompt, 10)
# with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as demo:
# chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
# state = gr.State()
# with gr.Row():
# with gr.Column(scale=0.85):
# txt = gr.Textbox(
# show_label=False,
# placeholder="Enter text and press enter, or upload an image",
# ).style(container=False)
# with gr.Column(scale=0.15, min_width=0):
# btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
# txt_msg = txt.submit(answer, [txt, state], chatbot, state, queue=False).then(
# lambda x: chatbot.add(*x),
# None,
# [chatbot])
# txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
# file_msg = btn.upload(add_file, chatbot, btn, chatbot, btn, queue=False).then(
# lambda x: chatbot.add(*x),
# None,
# [chatbot]
# )
# demo.launch()
# with gr.Blocks(css="#chatbot{height:800px} .overflow-y-auto{height:500px}") as demo:
# chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
with gr.Blocks(css="#chatty{height:800px} .overflow-y-auto{height:500px}") as demo:
chatty = gr.Chatbot([], name="Chatty", elem_id="chatbot").style(height=800, width=800)
with gr.Row():
with gr.Column(scale=0.85):
txt = gr.Textbox(
show_label=False,
placeholder="Enter text and press enter, or upload an image",
).style(container=False)
with gr.Column(scale=0.15, min_width=0):
btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
txt_msg = txt.submit(add_text, [chatty, txt], [chatty, txt], queue=False).then(
bot, chatty, chatty
)
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
file_msg = btn.upload(add_file, [chatty, btn], [chatty], queue=False).then(
bot, chatty, chatty
)
demo.launch()
|