Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,42 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
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 |
-
|
46 |
-
|
47 |
-
#
|
48 |
-
with
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
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})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|