Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,59 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from g4f.client import Client
|
| 3 |
|
|
|
|
| 4 |
client = Client()
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
user_prompt = st.chat_input("Ask GPT-4o-mini")
|
| 22 |
-
|
| 23 |
-
if user_prompt:
|
| 24 |
-
st.chat_message("user").markdown(user_prompt)
|
| 25 |
-
st.session_state.chat_history.append({"role": "user", "content": user_prompt})
|
| 26 |
-
|
| 27 |
-
response = client.chat.completions.create(
|
| 28 |
-
model="gpt-4o-mini",
|
| 29 |
-
messages=[
|
| 30 |
-
{"role": "system", "content": "You are a helpful assistant"},
|
| 31 |
-
*st.session_state.chat_history
|
| 32 |
-
]
|
| 33 |
-
)
|
| 34 |
-
final_response = response.choices[0].message.content
|
| 35 |
-
st.session_state.chat_history.append({"role": "assistant", "content": final_response})
|
| 36 |
-
with st.chat_message("assistant"):
|
| 37 |
-
st.markdown(final_response)
|
|
|
|
| 1 |
+
# gradio_g4f_chat.py
|
| 2 |
+
import gradio as gr
|
| 3 |
from g4f.client import Client
|
| 4 |
|
| 5 |
+
# --- Инициализация ---
|
| 6 |
client = Client()
|
| 7 |
+
chat_history = [] # можно использовать st.session_state аналог — через closure или state
|
| 8 |
+
|
| 9 |
+
# --- Настройки ---
|
| 10 |
+
SYSTEM_PROMPT = "You are a helpful assistant."
|
| 11 |
+
|
| 12 |
+
def respond(message, history):
|
| 13 |
+
global chat_history
|
| 14 |
+
|
| 15 |
+
# Формируем полный контекст
|
| 16 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 17 |
+
for human, assistant in history:
|
| 18 |
+
messages.append({"role": "user", "content": human})
|
| 19 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 20 |
+
messages.append({"role": "user", "content": message})
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
response = client.chat.completions.create(
|
| 24 |
+
model="gpt-4o-mini",
|
| 25 |
+
messages=messages
|
| 26 |
+
)
|
| 27 |
+
bot_message = response.choices[0].message.content
|
| 28 |
+
except Exception as e:
|
| 29 |
+
bot_message = f"Ошибка связи с ИИ: {str(e)}"
|
| 30 |
+
|
| 31 |
+
# Добавляем в историю
|
| 32 |
+
chat_history.append((message, bot_message))
|
| 33 |
+
|
| 34 |
+
return bot_message
|
| 35 |
+
|
| 36 |
+
# --- Создание интерфейса ---
|
| 37 |
+
demo = gr.ChatInterface(
|
| 38 |
+
fn=respond,
|
| 39 |
+
title="📝 GPT-4o-mini Chat via g4f",
|
| 40 |
+
description="Общайся с GPT-4o-mini без API-ключа. Powered by g4f.",
|
| 41 |
+
examples=[
|
| 42 |
+
"Привет, кто ты?",
|
| 43 |
+
"Расскажи про искусственный интеллект",
|
| 44 |
+
"Напиши стих про робота"
|
| 45 |
+
],
|
| 46 |
+
retry_btn="🔄 Повторить",
|
| 47 |
+
undo_btn="↩️ Отменить",
|
| 48 |
+
clear_btn="🗑️ Очистить"
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# --- Запуск ---
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
demo.queue() # для асинхронной обработки
|
| 54 |
+
demo.launch(
|
| 55 |
+
server_name="0.0.0.0",
|
| 56 |
+
server_port=7860,
|
| 57 |
+
share=False, # если хочешь публичную ссылку — поставь True
|
| 58 |
+
debug=True
|
| 59 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|