Serg4451D commited on
Commit
e001e01
·
1 Parent(s): aea8c00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -16
app.py CHANGED
@@ -1,30 +1,47 @@
1
  import openai
2
  import streamlit as st
3
 
4
- # Установка ключа API OpenAI
5
  openai.api_key = "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"
6
 
7
  # Функция для генерации ответа
8
  def generate_answer(prompt, model, max_tokens):
9
- response = openai.Completion.create(
10
- engine=model,
11
- prompt=prompt,
12
- max_tokens=max_tokens,
13
- n=1,
14
- stop=None,
15
- temperature=0.7,
 
 
 
 
 
 
 
 
 
 
16
  )
17
 
18
- message = response.choices[0].text
19
  return message.strip()
20
 
21
- # Заголовок страницы
22
- st.title("Привет! Я - ChatGPT")
23
 
24
- # Поле для ввода сообщения
25
- message = st.text_input("Введите ваше сообщение:")
26
 
27
- if message:
 
 
 
 
 
 
 
28
  # Генерация ответа
29
  response = generate_answer(
30
  prompt=message,
@@ -32,5 +49,8 @@ if message:
32
  max_tokens=150,
33
  )
34
 
35
- # Вывод ответа
36
- st.write("Ответ:", response)
 
 
 
 
1
  import openai
2
  import streamlit as st
3
 
4
+ # Устанавливаем ключ API
5
  openai.api_key = "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"
6
 
7
  # Функция для генерации ответа
8
  def generate_answer(prompt, model, max_tokens):
9
+ headers = {
10
+ "Content-Type": "application/json",
11
+ "Authorization": f"Bearer {openai.api_key}",
12
+ }
13
+ data = {
14
+ "prompt": prompt,
15
+ "model": model,
16
+ "max_tokens": max_tokens,
17
+ "n": 1,
18
+ "stop": None,
19
+ "temperature": 0.7,
20
+ }
21
+ response = openai.api_requestor.APIRequestor().request(
22
+ "POST",
23
+ "https://api.openai.com/v1/chat/completions",
24
+ headers=headers,
25
+ json=data
26
  )
27
 
28
+ message = response["choices"][0]["text"]
29
  return message.strip()
30
 
31
+ # Название страницы
32
+ st.set_page_config(page_title="Chatbot with GPT-3.5 Turbo")
33
 
34
+ # Заголовок
35
+ st.title("Chatbot with GPT-3.5 Turbo")
36
 
37
+ # Предупреждение о конфиденциальности
38
+ st.write("This demo uses OpenAI's GPT-3.5 model to generate text. Please keep in mind that the model may generate inappropriate or offensive content.")
39
+
40
+ # Интерфейс ввода сообщения
41
+ message = st.text_input("You: ", "")
42
+
43
+ # Если пользователь отправил сообщение
44
+ if st.button("Send"):
45
  # Генерация ответа
46
  response = generate_answer(
47
  prompt=message,
 
49
  max_tokens=150,
50
  )
51
 
52
+ # Отображение ответа
53
+ st.text_area("Bot: ", value=response, height=200, max_chars=None, key=None)
54
+
55
+ # Инструкции
56
+ st.write("Enter your message in the text box above and click 'Send' to start a conversation with the chatbot. The chatbot will use GPT-3.5 to generate responses to your messages. Have fun chatting!")