New File
Browse filesNew SoloChat implementation.
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
openai.api_key = "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"
|
| 6 |
+
|
| 7 |
+
start_sequence = "\nAI:"
|
| 8 |
+
restart_sequence = "\nHuman: "
|
| 9 |
+
|
| 10 |
+
prompt = "The following is a conversation with an AI assistant. The assistant is
|
| 11 |
+
helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI
|
| 12 |
+
: I am an AI created by OpenAI. How can I help you today?\nHuman: "
|
| 13 |
+
|
| 14 |
+
def openai_create(prompt):
|
| 15 |
+
|
| 16 |
+
response = openai.Completion.create(
|
| 17 |
+
model="text-davinci-003",
|
| 18 |
+
prompt=prompt,
|
| 19 |
+
temperature=0.9,
|
| 20 |
+
max_tokens=150,
|
| 21 |
+
top_p=1,
|
| 22 |
+
frequency_penalty=0,
|
| 23 |
+
presence_penalty=0.6,
|
| 24 |
+
stop=[" Human:", " AI:"]
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
return response.choices[0].text
|
| 28 |
+
|
| 29 |
+
def chatgpt_clone(input, history):
|
| 30 |
+
history = history or []
|
| 31 |
+
s = list(sum(history, ()))
|
| 32 |
+
s.append(input)
|
| 33 |
+
inp = ' '.join(s)
|
| 34 |
+
output = openai_create(inp)
|
| 35 |
+
history.append((input, output))
|
| 36 |
+
return history, history
|
| 37 |
+
|
| 38 |
+
block = gr.Blocks()
|
| 39 |
+
|
| 40 |
+
with block:
|
| 41 |
+
gr.Markdown("""<H1><center>Welcome to SoloChat. Powered by OpenAI and Gradio
|
| 42 |
+
</center></H1>""")
|
| 43 |
+
chatbot = gr.Chatbot()
|
| 44 |
+
message = gr.Textbox(placeholder=prompt)
|
| 45 |
+
state = gr.State()
|
| 46 |
+
submit = gr.Button("SEND")
|
| 47 |
+
submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state
|
| 48 |
+
])
|
| 49 |
+
|
| 50 |
+
block.launch(debug=True, share=True)
|
| 51 |
+
|