Serg4451D commited on
Commit
f2f6b48
·
verified ·
1 Parent(s): 608a866

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -66
app.py CHANGED
@@ -1,87 +1,68 @@
1
  import streamlit as st
2
- import g4f
3
 
4
- # Функция для отправки сообщений в GPT
5
- def get_gpt_response(messages):
6
- client = g4f.Client()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  response = client.chat.completions.create(
8
  model="gpt-3.5-turbo",
9
- messages=messages,
10
  )
11
- return response.choices[0].message.content
12
-
13
- # Инициализация списка сообщений
14
- if 'messages' not in st.session_state:
15
- st.session_state['messages'] = [{"role": "user", "content": "Hello!"}]
 
 
 
16
 
17
- # Настройки страницы
18
- st.set_page_config(page_title="GPT Chat", page_icon="🤖", layout="wide")
19
-
20
- # Стили CSS для красивого интерфейса
21
- st.markdown("""
22
  <style>
23
- .main {
24
- background-color: #f0f0f5;
25
- font-family: 'Montserrat', sans-serif;
26
- }
27
- .stTextInput > div > input {
28
- background-color: #e0e0eb;
29
- border: 1px solid #d1d1e0;
30
- border-radius: 10px;
31
- padding: 10px;
32
  font-size: 18px;
33
  }
34
  .stButton > button {
35
  background-color: #4CAF50;
36
  color: white;
37
- border: none;
38
- border-radius: 10px;
39
  padding: 10px 20px;
40
- font-size: 18px;
41
- transition: background-color 0.3s ease;
42
  }
43
  .stButton > button:hover {
44
  background-color: #45a049;
45
  }
46
- .st-chatbox {
47
- background-color: #f7f7f7;
48
- border-radius: 10px;
49
- padding: 10px;
50
- margin: 10px 0;
51
- }
52
- .st-chatbox-user {
53
- text-align: right;
54
- color: blue;
55
- }
56
- .st-chatbox-bot {
57
- text-align: left;
58
- color: green;
59
- }
60
  </style>
61
- """, unsafe_allow_html=True)
62
-
63
- # Заголовок
64
- st.title("Chat with GPT 🤖")
65
-
66
- # Форма для ввода сообщения пользователя
67
- with st.form(key="chat_form", clear_on_submit=True):
68
- user_message = st.text_input("You:", placeholder="Type your message here...")
69
- submit_button = st.form_submit_button(label="Send")
70
-
71
- # Отправка сообщения пользователя
72
- if submit_button and user_message:
73
- # Добавление сообщения пользователя в историю
74
- st.session_state['messages'].append({"role": "user", "content": user_message})
75
 
76
- # Получение ответа от GPT
77
- bot_message = get_gpt_response(st.session_state['messages'])
78
 
79
- # Добавление ответа бота в историю
80
- st.session_state['messages'].append({"role": "assistant", "content": bot_message})
81
 
82
- # Отображение всех сообщений
83
- for message in st.session_state['messages']:
84
- if message['role'] == 'user':
85
- st.markdown(f"<div class='st-chatbox st-chatbox-user'><b>You:</b> {message['content']}</div>", unsafe_allow_html=True)
86
- else:
87
- st.markdown(f"<div class='st-chatbox st-chatbox-bot'><b>GPT:</b> {message['content']}</div>", unsafe_allow_html=True)
 
1
  import streamlit as st
2
+ from g4f.client import Client
3
 
4
+ # Инициализация клиента GPT
5
+ client = Client()
6
+
7
+ # Настройки страницы
8
+ st.set_page_config(page_title="Chat with GPT", page_icon="🤖", layout="wide")
9
+
10
+ # Заголовок и описание
11
+ st.title("Chat with GPT-3.5-turbo")
12
+ st.write("Welcome to the GPT-3.5-turbo chat interface. Ask me anything!")
13
+
14
+ # Создание боковой панели для ввода сообщения
15
+ with st.sidebar:
16
+ user_input = st.text_area("You:", height=100)
17
+ send_button = st.button("Send")
18
+
19
+ # Основной контейнер для чата
20
+ chat_container = st.container()
21
+
22
+ # Обработка отправки сообщения
23
+ if send_button and user_input:
24
+ # Отправка запроса к GPT
25
  response = client.chat.completions.create(
26
  model="gpt-3.5-turbo",
27
+ messages=[{"role": "user", "content": user_input}],
28
  )
29
+
30
+ # Получение ответа
31
+ gpt_response = response.choices[0].message.content
32
+
33
+ # Отображение сообщений в чате
34
+ with chat_container:
35
+ st.write(f"**You:** {user_input}")
36
+ st.write(f"**GPT:** {gpt_response}")
37
 
38
+ # Пример стилизации с использованием CSS
39
+ st.markdown(
40
+ """
 
 
41
  <style>
42
+ .stTextInput > div > div > textarea {
 
 
 
 
 
 
 
 
43
  font-size: 18px;
44
  }
45
  .stButton > button {
46
  background-color: #4CAF50;
47
  color: white;
48
+ font-size: 16px;
 
49
  padding: 10px 20px;
50
+ border: none;
51
+ cursor: pointer;
52
  }
53
  .stButton > button:hover {
54
  background-color: #45a049;
55
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  </style>
57
+ """,
58
+ unsafe_allow_html=True
59
+ )
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ # Пример добавления изображения
62
+ st.image("https://via.placeholder.com/150", caption="GPT-3.5-turbo", use_column_width=True)
63
 
64
+ # Пример добавления разделителя
65
+ st.markdown("---")
66
 
67
+ # Пример добавления ссылки
68
+ st.markdown("[Learn more about GPT-3.5-turbo](https://platform.openai.com/docs/models/gpt-3-5)")