Spaces:
Running
Running
~limit to 5 messages/conversation
Browse files
app.py
CHANGED
|
@@ -7,6 +7,9 @@ api_key = os.environ.get("OPENAI_API_KEY")
|
|
| 7 |
|
| 8 |
def openai_chat(prompt, history):
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
url = "https://api.openai.com/v1/chat/completions"
|
| 11 |
headers = {
|
| 12 |
"Content-Type": "application/json",
|
|
@@ -30,7 +33,11 @@ def openai_chat(prompt, history):
|
|
| 30 |
history.append(prompt_msg)
|
| 31 |
history.append(response)
|
| 32 |
|
| 33 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
css = """
|
| 36 |
#col-container {max-width: 50%; margin-left: auto; margin-right: auto;}
|
|
@@ -43,11 +50,15 @@ with gr.Blocks(css=css) as demo:
|
|
| 43 |
|
| 44 |
with gr.Column(elem_id="col-container"):
|
| 45 |
gr.Markdown("""## OpenAI Chatbot Demo
|
| 46 |
-
Using the ofiicial API and GPT-3.5 Turbo model
|
|
|
|
|
|
|
| 47 |
chatbot = gr.Chatbot(elem_id="chatbox")
|
| 48 |
-
input_message = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
|
|
|
|
| 49 |
|
| 50 |
input_message.submit(openai_chat, [input_message, history], [input_message, chatbot, history])
|
|
|
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
-
demo.launch()
|
|
|
|
| 7 |
|
| 8 |
def openai_chat(prompt, history):
|
| 9 |
|
| 10 |
+
if not prompt:
|
| 11 |
+
return gr.update(value='', visible=len(history) < 10), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], history
|
| 12 |
+
|
| 13 |
url = "https://api.openai.com/v1/chat/completions"
|
| 14 |
headers = {
|
| 15 |
"Content-Type": "application/json",
|
|
|
|
| 33 |
history.append(prompt_msg)
|
| 34 |
history.append(response)
|
| 35 |
|
| 36 |
+
return gr.update(value='', visible=len(history) < 10), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], history
|
| 37 |
+
|
| 38 |
+
def start_new_conversation(history):
|
| 39 |
+
history = []
|
| 40 |
+
return gr.update(value=None, visible=True), gr.update(value=[]), history
|
| 41 |
|
| 42 |
css = """
|
| 43 |
#col-container {max-width: 50%; margin-left: auto; margin-right: auto;}
|
|
|
|
| 50 |
|
| 51 |
with gr.Column(elem_id="col-container"):
|
| 52 |
gr.Markdown("""## OpenAI Chatbot Demo
|
| 53 |
+
Using the ofiicial API and GPT-3.5 Turbo model.
|
| 54 |
+
Current limit is 5 messages per conversation.""",
|
| 55 |
+
elem_id="header")
|
| 56 |
chatbot = gr.Chatbot(elem_id="chatbox")
|
| 57 |
+
input_message = gr.Textbox(show_label=False, placeholder="Enter text and press enter", visible=True).style(container=False)
|
| 58 |
+
btn_start_new_conversation = gr.Button("Start a new conversation")
|
| 59 |
|
| 60 |
input_message.submit(openai_chat, [input_message, history], [input_message, chatbot, history])
|
| 61 |
+
btn_start_new_conversation.click(start_new_conversation, [history], [input_message, chatbot, history])
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
demo.launch(debug=True, height='800px')
|