File size: 1,781 Bytes
4db43a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from email import message
import gradio as gr
import os
import openai
from create_poll import create_poll
from utils import GPTCompletion
from dotenv import load_dotenv

gr.close_all()

load_dotenv()

openai.api_key = os.environ["OPENAI_API_KEY"]


def chatWithGPT(chatHistory):
    completion = GPTCompletion(system="You are an AI chatting with a human.", max_tokens=2048, temperature=1.5)
    gptResponse = completion.chatComplete(chatHistory)
    chatHistory[-1][1] = gptResponse
    return chatHistory

with gr.Blocks() as demo:
    chatHistory = []

    def generateResponse(message):
        completion = GPTCompletion(system="You are an AI chatting with a human.", max_tokens=2048, temperature=1.5)
        gptResponse = completion.chatComplete(chatHistory,message)
        chatHistory.append((message, gptResponse))
        return chatHistory

    def pollinize():

        chatList = []
        for log in chatHistory:
            chatList.append("User: " + log[0])
            chatList.append("AI: " + log[1])
        chatString = "\n".join(chatList)

        return create_poll(chatString, openai.api_key)

    def uploadApi(apikey):
        openai.api_key = apikey

    chatbot = gr.Chatbot().style(height=460)
    input = gr.Textbox(label="Messeage")
    nextBtn = gr.Button("Next response")
    nextBtn.click(generateResponse, input, chatbot, scroll_to_output=True, show_progress=True)

    debatePoll = gr.Textbox(label="Poll")
    pollinizeButton = gr.Button("Create a poll")
    pollinizeButton.click(pollinize,None, debatePoll, scroll_to_output=True, show_progress=True)

    apikey = gr.Textbox(label="API Key")
    apiUpload = gr.Button("Upload api key")
    apiUpload.click(uploadApi, apikey, None, scroll_to_output=True, show_progress=True)
    
demo.launch()