Spaces:
Runtime error
Runtime error
Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
openai.api_key = os.environ["API_KEY"]
|
6 |
+
messages = [{}]
|
7 |
+
|
8 |
+
def chatgpt(content, initial, temperature=0.8, max_tokens=1000):
|
9 |
+
global messages
|
10 |
+
print(f'ask:{content}')
|
11 |
+
messages[0]={"role": "system", "content": initial}
|
12 |
+
messages.append({"role": "user", "content": content})
|
13 |
+
try:
|
14 |
+
response = openai.ChatCompletion.create(
|
15 |
+
model="gpt-3.5-turbo-0301",
|
16 |
+
messages=messages,
|
17 |
+
temperature=temperature,
|
18 |
+
max_tokens=max_tokens,
|
19 |
+
top_p=1,
|
20 |
+
frequency_penalty=0,
|
21 |
+
presence_penalty=0,
|
22 |
+
)
|
23 |
+
except Exception as e:
|
24 |
+
string = str(e)
|
25 |
+
messages = [{}]
|
26 |
+
return string
|
27 |
+
messages.append(json.loads(str(response.choices[0].message)))
|
28 |
+
print(f'answer:{response.choices[0].message.content}')
|
29 |
+
return response.choices[0].message.content
|
30 |
+
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
import gradio as gr
|
34 |
+
|
35 |
+
|
36 |
+
def send_chatgpt(text, initial, temperature, max_tokens):
|
37 |
+
output = chatgpt(text, initial, temperature, max_tokens)
|
38 |
+
return output
|
39 |
+
|
40 |
+
|
41 |
+
input_initial_text = gr.inputs.Textbox(label="Начальная установка:", placeholder="Например: 'Ты опытный reactJS разработчик.'")
|
42 |
+
input_text = gr.inputs.Textbox(label="Запрос:", placeholder="Например: Нужен ReactJS компонент в виде круглой кнопки, которая по клику меняет цвет на случайный.")
|
43 |
+
output_text = gr.outputs.Textbox(label="Ответ:")
|
44 |
+
input_temp = gr.Slider(label="Температура", minimum=0, maximum=1.5, step=.01, value=1)
|
45 |
+
input_maxTokens = gr.Slider(label="Максимум токенов", minimum=50, maximum=2000, step=50, value=1000)
|
46 |
+
interface = gr.Interface(fn=send_chatgpt, inputs=[input_text, input_initial_text, input_temp, input_maxTokens], outputs=output_text, title="ChatGPT")
|
47 |
+
interface.launch(share=True)
|