Tricia Nieva
Update app.py
bfeccc6
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import os
import openai
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
openai.organization = "org-orRhfBkKOfOuNACbjPyWKbUt"
openai.api_key = "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"
def predict(input, history=[]):
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
# tokenize the new input sentence
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
# append the new user input tokens to the chat history
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
# generate a response
response = openai.Completion.create(
model="davinci:ft-placeholder:ai-dhd-2022-12-07-10-09-37",
prompt= input,
temperature=0.09,
max_tokens=608,
top_p=1,
frequency_penalty=0,
presence_penalty=0).tolist()
history = response[Completion]
# convert the tokens to text, and then split the responses into lines
response = tokenizer.decode(history[0]).split("<|endoftext|>")
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
return response, history
gr.Interface(fn=predict,
inputs=["text", "state"],
outputs=["chatbot", "state"]).launch()