Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,18 @@
|
|
| 1 |
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 6 |
|
|
|
|
| 7 |
class Conversation:
|
| 8 |
def __init__(self, prompt, num_of_round):
|
| 9 |
self.prompt = prompt
|
| 10 |
self.num_of_round = num_of_round
|
| 11 |
self.messages = []
|
| 12 |
self.messages.append({"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(
|
|
@@ -19,15 +20,53 @@ class Conversation:
|
|
| 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 e
|
| 27 |
-
|
| 28 |
message = response["choices"][0]["message"]["content"]
|
| 29 |
self.messages.append({"role": "assistant", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 6 |
|
| 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 = []
|
| 13 |
self.messages.append({"role": "system", "content": self.prompt})
|
| 14 |
+
|
| 15 |
+
def ask(self, question):
|
| 16 |
try:
|
| 17 |
self.messages.append({"role": "user", "content": question})
|
| 18 |
response = openai.ChatCompletion.create(
|
|
|
|
| 20 |
messages=self.messages,
|
| 21 |
temperature=0.5,
|
| 22 |
max_tokens=2048,
|
| 23 |
+
top_p=1,
|
| 24 |
)
|
| 25 |
except Exception as e:
|
| 26 |
print(e)
|
| 27 |
return e
|
|
|
|
| 28 |
message = response["choices"][0]["message"]["content"]
|
| 29 |
self.messages.append({"role": "assistant", "content": message})
|
| 30 |
+
if len(self.messages) > 3:
|
| 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 answer(question, history=[]):
|
| 43 |
+
history.append(question)
|
| 44 |
+
response = conv.ask(question)
|
| 45 |
+
history.append(response)
|
| 46 |
+
responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
|
| 47 |
+
return responses, history
|
| 48 |
|
| 49 |
+
prompt = """假如你是GPT-4,你可以回答用户提问的任何问题"""
|
| 50 |
+
conv = Conversation(prompt, 10)
|
| 51 |
+
with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as demo:
|
| 52 |
+
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
|
| 53 |
+
state = gr.State()
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column(scale=0.85):
|
| 57 |
+
txt = gr.Textbox(
|
| 58 |
+
show_label=False,
|
| 59 |
+
placeholder="Enter text and press enter, or upload an image",
|
| 60 |
+
).style(container=False)
|
| 61 |
+
with gr.Column(scale=0.15, min_width=0):
|
| 62 |
+
btn = gr.UploadButton(":file_folder:", file_types=["image", "video", "audio"])
|
| 63 |
+
|
| 64 |
+
txt_msg = txt.submit(answer, [txt, state], [chatbot, state], queue=False).then(
|
| 65 |
+
chatbot, chatbot
|
| 66 |
+
)
|
| 67 |
+
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
| 68 |
+
|
| 69 |
+
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], [btn], queue=False).then(
|
| 70 |
+
chatbot, chatbot, chatbot
|
| 71 |
+
)
|
| 72 |
+
demo.launch()
|