Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.system("pip install openai==0.28.1")
|
| 3 |
+
import openai
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
| 7 |
+
|
| 8 |
+
def predict(message, history, temperature):
|
| 9 |
+
history_openai_format = []
|
| 10 |
+
for human, assistant in history:
|
| 11 |
+
history_openai_format.append({"role": "user", "content": human })
|
| 12 |
+
history_openai_format.append({"role": "assistant", "content":assistant})
|
| 13 |
+
history_openai_format.append({"role": "user", "content": message})
|
| 14 |
+
|
| 15 |
+
response = openai.ChatCompletion.create(
|
| 16 |
+
model='gpt-3.5-turbo-1106',
|
| 17 |
+
messages= history_openai_format,
|
| 18 |
+
temperature=temperature,
|
| 19 |
+
stream=True
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
partial_message = ""
|
| 23 |
+
for chunk in response:
|
| 24 |
+
if len(chunk['choices'][0]['delta']) != 0:
|
| 25 |
+
partial_message = partial_message + chunk['choices'][0]['delta']['content']
|
| 26 |
+
yield partial_message
|
| 27 |
+
|
| 28 |
+
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()).queue().launch(auth=("rectal", "prolapse"))
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|