Spaces:
Runtime error
Runtime error
ArdaSaygan
commited on
Commit
·
4db43a4
1
Parent(s):
259e1bc
added chatbot
Browse files- chatbot.py +57 -0
chatbot.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from email import message
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import openai
|
| 5 |
+
from create_poll import create_poll
|
| 6 |
+
from utils import GPTCompletion
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
gr.close_all()
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def chatWithGPT(chatHistory):
|
| 17 |
+
completion = GPTCompletion(system="You are an AI chatting with a human.", max_tokens=2048, temperature=1.5)
|
| 18 |
+
gptResponse = completion.chatComplete(chatHistory)
|
| 19 |
+
chatHistory[-1][1] = gptResponse
|
| 20 |
+
return chatHistory
|
| 21 |
+
|
| 22 |
+
with gr.Blocks() as demo:
|
| 23 |
+
chatHistory = []
|
| 24 |
+
|
| 25 |
+
def generateResponse(message):
|
| 26 |
+
completion = GPTCompletion(system="You are an AI chatting with a human.", max_tokens=2048, temperature=1.5)
|
| 27 |
+
gptResponse = completion.chatComplete(chatHistory,message)
|
| 28 |
+
chatHistory.append((message, gptResponse))
|
| 29 |
+
return chatHistory
|
| 30 |
+
|
| 31 |
+
def pollinize():
|
| 32 |
+
|
| 33 |
+
chatList = []
|
| 34 |
+
for log in chatHistory:
|
| 35 |
+
chatList.append("User: " + log[0])
|
| 36 |
+
chatList.append("AI: " + log[1])
|
| 37 |
+
chatString = "\n".join(chatList)
|
| 38 |
+
|
| 39 |
+
return create_poll(chatString, openai.api_key)
|
| 40 |
+
|
| 41 |
+
def uploadApi(apikey):
|
| 42 |
+
openai.api_key = apikey
|
| 43 |
+
|
| 44 |
+
chatbot = gr.Chatbot().style(height=460)
|
| 45 |
+
input = gr.Textbox(label="Messeage")
|
| 46 |
+
nextBtn = gr.Button("Next response")
|
| 47 |
+
nextBtn.click(generateResponse, input, chatbot, scroll_to_output=True, show_progress=True)
|
| 48 |
+
|
| 49 |
+
debatePoll = gr.Textbox(label="Poll")
|
| 50 |
+
pollinizeButton = gr.Button("Create a poll")
|
| 51 |
+
pollinizeButton.click(pollinize,None, debatePoll, scroll_to_output=True, show_progress=True)
|
| 52 |
+
|
| 53 |
+
apikey = gr.Textbox(label="API Key")
|
| 54 |
+
apiUpload = gr.Button("Upload api key")
|
| 55 |
+
apiUpload.click(uploadApi, apikey, None, scroll_to_output=True, show_progress=True)
|
| 56 |
+
|
| 57 |
+
demo.launch()
|