Spaces:
Runtime error
Runtime error
import gradio as gr | |
import openai | |
from googletrans import Translator | |
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 = "Imagination is the limit!!" | |
def GPT3_Completion(prompt): | |
# translate the prompt to English | |
translator = Translator() | |
prompt_english = translator.translate(prompt, dest='en').text | |
openai.api_key = "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" | |
response = openai.Completion.create( | |
engine="text-davinci-002", | |
prompt = prompt_english, | |
temperature=0.7, | |
max_tokens=3500 , | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
# translate the response to Spanish | |
response_spanish = translator.translate(response['choices'][0]['text'], dest='es').text | |
return response_spanish | |
def chatgpt_clone(input, history): | |
history = history or [] | |
s = list(sum(history, ())) | |
s.append(input) | |
inp = ' '.join(s) | |
output = GPT3_Completion(inp) | |
history.append((input, output)) | |
return history, history | |
block = gr.Blocks(api_name=True) | |
with block: | |
gr.Markdown("""<h1><center>Chatbot with OpenAI API & Gradio</center></h1> | |
""") | |
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],api_name=True) | |
block.launch() |