Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,29 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
# Самый простой и надежный способ вызова модели в Hugging Face Spaces
|
| 4 |
-
# Мы используем встроенный метод загрузки, который сам следит за сессиями
|
| 5 |
def predict(message, history):
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
response =
|
| 15 |
-
|
| 16 |
-
# Безопасное извлечение текста
|
| 17 |
-
if isinstance(response, list) and len(response) > 0:
|
| 18 |
-
res = response[0]
|
| 19 |
-
return res.get("generated_text", str(res)) if isinstance(res, dict) else str(res)
|
| 20 |
-
return str(response)
|
| 21 |
except Exception as e:
|
| 22 |
-
return f"
|
| 23 |
|
| 24 |
-
#
|
| 25 |
custom_theme = gr.themes.Soft(
|
| 26 |
primary_hue="yellow",
|
| 27 |
secondary_hue="blue",
|
|
@@ -30,14 +32,15 @@ custom_theme = gr.themes.Soft(
|
|
| 30 |
block_background_fill="#ffdd00",
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
with gr.Sidebar():
|
| 35 |
gr.Markdown("# **FlareAI**")
|
| 36 |
-
gr.Markdown("
|
| 37 |
gr.LoginButton("Войти")
|
| 38 |
|
| 39 |
-
#
|
| 40 |
gr.ChatInterface(fn=predict)
|
| 41 |
|
| 42 |
-
# Запускаем без лишних параметров, чтобы не было конфликтов с браузером
|
| 43 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
# Создаем клиента. Он сам найдет HF_TOKEN в секретах Space.
|
| 5 |
+
client = InferenceClient("moonshotai/Kimi-K2-Thinking")
|
| 6 |
|
|
|
|
|
|
|
| 7 |
def predict(message, history):
|
| 8 |
+
# Формируем историю для модели
|
| 9 |
+
messages = [{"role": "system", "content": "Тебя зовут Gemini. Ты — ассистент FlareAI."}]
|
| 10 |
+
|
| 11 |
+
# Добавляем прошлые сообщения
|
| 12 |
+
for user_msg, bot_msg in history:
|
| 13 |
+
messages.append({"role": "user", "content": user_msg})
|
| 14 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 15 |
|
| 16 |
+
# Добавляем текущий вопрос
|
| 17 |
+
messages.append({"role": "user", "content": message})
|
| 18 |
+
|
| 19 |
try:
|
| 20 |
+
# Вызываем модель без стриминга для максимальной стабильности
|
| 21 |
+
response = client.chat_completion(messages, max_tokens=512)
|
| 22 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
+
return f"Ошибка: {str(e)}. Попробуй еще раз через пару секунд."
|
| 25 |
|
| 26 |
+
# Сине-желтый дизайн
|
| 27 |
custom_theme = gr.themes.Soft(
|
| 28 |
primary_hue="yellow",
|
| 29 |
secondary_hue="blue",
|
|
|
|
| 32 |
block_background_fill="#ffdd00",
|
| 33 |
)
|
| 34 |
|
| 35 |
+
css = ".gradio-container { background-color: #0057b7 !important; }"
|
| 36 |
+
|
| 37 |
+
with gr.Blocks(theme=custom_theme, css=css) as demo:
|
| 38 |
with gr.Sidebar():
|
| 39 |
gr.Markdown("# **FlareAI**")
|
| 40 |
+
gr.Markdown("Ассистент: Gemini")
|
| 41 |
gr.LoginButton("Войти")
|
| 42 |
|
| 43 |
+
# В Gradio 6.0 лучше не передавать лишних аргументов в ChatInterface
|
| 44 |
gr.ChatInterface(fn=predict)
|
| 45 |
|
|
|
|
| 46 |
demo.launch()
|