Spaces:
Runtime error
Runtime error
import os | |
import openai | |
import gradio as gr | |
#if you have OpenAI API key as an environment variable, enable the below | |
#openai.api_key = os.getenv("OPENAI_API_KEY") | |
#if you have OpenAI API key as a string, enable the below | |
openai.api_key = "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" | |
start_sequence = "\nAI:" | |
restart_sequence = "\nHuman: " | |
prompt = "Marv is a chatbot that reluctantly answers questions with sarcastic responses:\n\nYou: How many pounds are in a kilogram?\nMarv: This again? There are 2.2 pounds in a kilogram. Please make a note of this.\nYou: What does HTML stand for?\nMarv: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.\nYou: When did the first airplane fly?\nMarv: On December 17, 1903, Wilbur and Orville Wright made the first flights. I wish they’d come and take me away.\nYou: What is the meaning of life?\nMarv: I’m not sure. I’ll ask my friend Google." | |
def openai_create(prompt): | |
response = openai.Completion.create( | |
model="text-davinci-003", | |
prompt="Marv is a chatbot that reluctantly answers questions with sarcastic responses:\n\nYou: How many pounds are in a kilogram?\nMarv: This again? There are 2.2 pounds in a kilogram. Please make a note of this.\nYou: What does HTML stand for?\nMarv: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.\nYou: When did the first airplane fly?\nMarv: On December 17, 1903, Wilbur and Orville Wright made the first flights. I wish they’d come and take me away.\nYou: What is the meaning of life?\nMarv: I’m not sure. I’ll ask my friend Google." + prompt, | |
temperature=0.9, | |
max_tokens=150, | |
top_p=1, | |
frequency_penalty=1, | |
presence_penalty=0.6, | |
stop=[" Human:", " AI:"] | |
) | |
return response.choices[0].text | |
def chatgpt_clone(input, history): | |
history = history or [] | |
s = list(sum(history, ())) | |
s.append(input) | |
inp = ' '.join(s) | |
output = openai_create(inp) | |
history.append((input, output)) | |
return history, history | |
block = gr.Blocks() | |
with block: | |
gr.Markdown("""<h2><center> Talk to Marv</center></h2> | |
""") | |
gr.Markdown("""<h4><center> The Sarcastic Chatbot</center></h4> | |
""") | |
chatbot = gr.Chatbot() | |
message = gr.Textbox(placeholder=prompt) | |
state = gr.State() | |
submit = gr.Button("SEND") | |
submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state]) | |
block.launch(debug = True) |