File size: 1,849 Bytes
c5b594e
 
 
 
 
a59b80f
 
 
 
 
 
 
 
 
c5b594e
9a4e67e
c5b594e
 
 
 
 
 
 
 
 
 
cd9add1
c5b594e
 
 
 
 
 
 
 
 
 
 
a59b80f
c5b594e
 
 
 
 
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
import os
os.system("pip install openai==0.28.1")
import openai
import gradio as gr

style = """
div.contain > :first-child > :first-child > :first-child{
	height: 80vh !important;
}
textarea {
	height: 92px!important
}
"""

openai.api_key = os.environ['OPENAI_API_KEY']
openai.api_base = os.environ['API_URL']

def predict(message, history, temperature):
    history_openai_format = []
    for human, assistant in history:
        history_openai_format.append({"role": "user", "content": human })
        history_openai_format.append({"role": "assistant", "content":assistant})
    history_openai_format.append({"role": "user", "content": message})

    response = openai.ChatCompletion.create(
        model='gpt-3.5-turbo-1106',
        max_tokens=4000,
        messages= history_openai_format,
        temperature=temperature,
        stream=True
    )

    partial_message = ""
    for chunk in response:
        if len(chunk['choices'][0]['delta']) != 0:
            partial_message = partial_message + chunk['choices'][0]['delta']['content']
            yield partial_message

gr.ChatInterface(predict, additional_inputs=[gr.Slider(0.0, 2.0, step=0.1, value=1.0, label="Temperature", info="Более низкие значения температуры приводят к более стабильным результатам, а более высокие значения дают более разнообразные и творческие результаты впролоть до шизоидных.")], additional_inputs_accordion_name="задать температуру", autofocus=True, theme=gr.themes.Soft(), css=style, submit_btn="отправить", stop_btn="прервать", retry_btn="⭮ заново", undo_btn="⮌ отменить", clear_btn="новый чат").queue().launch(auth=("rectal", "prolapse"))