Spaces:
Sleeping
Sleeping
Thomas Manning commited on
Commit ·
48217bb
1
Parent(s): bde2157
Add app
Browse files- app.py +81 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
import openai
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
alice_prefix = 'Alice: '
|
| 8 |
+
bob_prefix = 'Bob: '
|
| 9 |
+
|
| 10 |
+
def seed_submit(seed, history):
|
| 11 |
+
if seed == '':
|
| 12 |
+
seed = 'Hi Bob!'
|
| 13 |
+
return "", history + [[f'{alice_prefix}{seed}', None]]
|
| 14 |
+
|
| 15 |
+
def bot_update(history, key, alice_system, alice_temp, alice_model, bob_system, bob_temp, bob_model):
|
| 16 |
+
|
| 17 |
+
if key:
|
| 18 |
+
os.environ['OPENAI_API_KEY'] = key
|
| 19 |
+
|
| 20 |
+
while True:
|
| 21 |
+
if history[-1][1] is None: # Bob's turn
|
| 22 |
+
messages = [
|
| 23 |
+
{"role": "system", "content": bob_system},
|
| 24 |
+
]
|
| 25 |
+
for [msg, resp] in history:
|
| 26 |
+
messages.append({"role": "user", "content": msg.replace(alice_prefix, '', 1)})
|
| 27 |
+
if resp:
|
| 28 |
+
messages.append({"role": "assistant", "content": resp.replace(bob_prefix, '', 1)})
|
| 29 |
+
resp = openai.ChatCompletion.create(
|
| 30 |
+
model=bob_model,
|
| 31 |
+
messages=messages,
|
| 32 |
+
temperature=bob_temp
|
| 33 |
+
)
|
| 34 |
+
bob_response = resp['choices'][0]['message']['content']
|
| 35 |
+
history[-1][1] = f'{bob_prefix}{bob_response}'
|
| 36 |
+
else: # Alice's turn
|
| 37 |
+
messages = [
|
| 38 |
+
{"role": "system", "content": alice_system},
|
| 39 |
+
]
|
| 40 |
+
for n, [msg, resp] in enumerate(history):
|
| 41 |
+
if n > 0: # skip the conversation opener
|
| 42 |
+
messages.append({"role": "assistant", "content": msg.replace(alice_prefix, '', 1)})
|
| 43 |
+
if resp:
|
| 44 |
+
messages.append({"role": "user", "content": resp.replace(bob_prefix, '', 1)})
|
| 45 |
+
resp = openai.ChatCompletion.create(
|
| 46 |
+
model=alice_model,
|
| 47 |
+
messages=messages,
|
| 48 |
+
temperature=alice_temp
|
| 49 |
+
)
|
| 50 |
+
alice_response = resp['choices'][0]['message']['content']
|
| 51 |
+
history.append([f'{alice_prefix}{alice_response}', None])
|
| 52 |
+
yield history
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
key = gr.Textbox(placeholder='Enter your OpenAI API Key')
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column():
|
| 61 |
+
alice_system = gr.Textbox(label='Alice\'s Prompt', value='Your name is Alice, you love to talk, and give your opinion', lines=5, interactive=True)
|
| 62 |
+
alice_temp = gr.Slider(value=1, minimum=0, maximum=1, step=0.1, label='Temperature')
|
| 63 |
+
alice_model = gr.Radio(choices=['gpt-4', 'gpt-3.5-turbo'], value='gpt-4')
|
| 64 |
+
|
| 65 |
+
with gr.Row():
|
| 66 |
+
with gr.Column():
|
| 67 |
+
bob_system = gr.Textbox(label='Bob\'s Prompt', value='Your name is Bob, you love to listen, and ask for opinions', lines=5, interactive=True)
|
| 68 |
+
bob_temp = gr.Slider(value=1, minimum=0, maximum=1, step=0.1, label='Temperature')
|
| 69 |
+
bob_model = gr.Radio(choices=['gpt-4', 'gpt-3.5-turbo'], value='gpt-4')
|
| 70 |
+
|
| 71 |
+
with gr.Column():
|
| 72 |
+
chatbot = gr.Chatbot()
|
| 73 |
+
seed = gr.Textbox(placeholder="How does Alice open the conversation?")
|
| 74 |
+
start = gr.Button('Start conversation')
|
| 75 |
+
|
| 76 |
+
start.click(seed_submit, [seed, chatbot], [seed, chatbot], queue=False).then(
|
| 77 |
+
bot_update, [chatbot, key, alice_system, alice_temp, alice_model, bob_system, bob_temp, bob_model], [chatbot]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
demo.queue(concurrency_count=1)
|
| 81 |
+
demo.launch(show_error=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai==0.27.2
|
| 2 |
+
gradio==3.23.0
|