Serg4451D commited on
Commit
5023c48
·
verified ·
1 Parent(s): 4f422b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -76
app.py CHANGED
@@ -1,82 +1,42 @@
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 Models")
12
- st.write("Welcome to the GPT chat interface. Choose a model and start chatting!")
13
-
14
- # Выбор модели
15
- model_options = {
16
- "gpt-4": "gpt-4",
17
- "gpt-4o": "gpt-4o",
18
- "gpt-4o-mini": "gpt-4o-mini",
19
- "gpt-3.5-turbo": "gpt-3.5-turbo",
20
- "gpt-4-turbo": "gpt-4-turbo",
21
- "claude-3.5-sonnet": "claude-3.5-sonnet",
22
- "claude-3-opus": "claude-3-opus",
23
- "claude-3-haiku": "claude-3-haiku"
24
- }
25
-
26
- selected_model = st.selectbox("Choose a model:", list(model_options.keys()))
27
-
28
- # Создание интерфейса для чата
29
- chat_container = st.container()
30
-
31
- # Создание формы для ввода сообщения
32
- with st.form(key='chat_form'):
33
- user_input = st.text_area("You:", height=100)
34
- send_button = st.form_submit_button("Send")
35
-
36
- # Обработка отправки сообщения
37
- if send_button and user_input:
38
- # Отправка запроса к выбранной модели GPT
39
- response = client.chat.completions.create(
40
- model=model_options[selected_model],
41
- messages=[{"role": "user", "content": user_input}],
42
  )
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- # Получение ответа
45
- gpt_response = response.choices[0].message.content
46
-
47
- # Отображение сообщений в чате
48
- with chat_container:
49
- st.write(f"**You:** {user_input}")
50
- st.write(f"**{selected_model}:** {gpt_response}")
51
-
52
- # Пример стилизации с использованием CSS
53
- st.markdown(
54
- """
55
- <style>
56
- .stTextInput > div > div > textarea {
57
- font-size: 18px;
58
- }
59
- .stButton > button {
60
- background-color: #4CAF50;
61
- color: white;
62
- font-size: 16px;
63
- padding: 10px 20px;
64
- border: none;
65
- cursor: pointer;
66
- }
67
- .stButton > button:hover {
68
- background-color: #45a049;
69
- }
70
- </style>
71
- """,
72
- unsafe_allow_html=True
73
- )
74
-
75
- # Пример добавления изображения
76
- st.image("https://via.placeholder.com/150", caption="GPT Models", use_column_width=True)
77
-
78
- # Пример добавления разделителя
79
- st.markdown("---")
80
-
81
- # Пример добавления ссылки
82
- st.markdown("[Learn more about GPT models](https://platform.openai.com/docs/models)")
 
1
  import streamlit as st
2
+ import g4f
3
 
4
+ # Set the title of the app
5
+ st.title("Chat with GPT-4 Free")
6
 
7
+ # Initialize chat history in session state
8
+ if "messages" not in st.session_state:
9
+ st.session_state.messages = []
10
 
11
+ # Function to generate responses from g4f
12
+ def generate_response(prompt):
13
+ response = g4f.ChatCompletion.create(
14
+ model=g4f.models.gpt_4,
15
+ messages=[{"role": "user", "content": prompt}],
16
+ stream=True # Enable streaming for real-time response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  )
18
+ return response
19
+
20
+ # Display chat messages from history
21
+ for message in st.session_state.messages:
22
+ with st.chat_message(message["role"]):
23
+ st.markdown(message["content"])
24
+
25
+ # Accept user input
26
+ if prompt := st.chat_input("Type your message here..."):
27
+ # Display user message in chat message container
28
+ with st.chat_message("user"):
29
+ st.markdown(prompt)
30
 
31
+ # Add user message to chat history
32
+ st.session_state.messages.append({"role": "user", "content": prompt})
33
+
34
+ # Generate and display AI response
35
+ with st.chat_message("assistant"):
36
+ response = generate_response(prompt)
37
+ for msg in response:
38
+ if isinstance(msg, str): # Check if the message is a string
39
+ st.markdown(msg)
40
+
41
+ # Add assistant's response to chat history
42
+ st.session_state.messages.append({"role": "assistant", "content": msg})