HaveAI commited on
Commit
1615469
·
verified ·
1 Parent(s): f07f34d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -32
app.py CHANGED
@@ -1,51 +1,52 @@
1
  import gradio as gr
 
2
 
3
- # Загружаем модель
4
- model = gr.load("models/moonshotai/Kimi-K2-Thinking", provider="novita")
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  custom_theme = gr.themes.Soft(
7
  primary_hue="yellow",
8
  secondary_hue="blue",
9
  ).set(
10
  body_background_fill="#0057b7",
11
  block_background_fill="#ffdd00",
 
 
12
  )
13
 
14
  css = """
15
- #side-bar { background-color: #ffdd00 !important; padding: 15px; }
16
- .gradio-container { color: white; }
17
  """
18
 
19
- def predict(message, history):
20
- system_instr = "Тебя зовут Gemini. "
21
- full_prompt = system_instr + message
22
-
23
- try:
24
- response = model(full_prompt)
25
-
26
- # Глубокая проверка структуры (извлекаем текст любой ценой)
27
- if isinstance(response, list):
28
- # Если это список, берем первый элемент
29
- res_item = response[0]
30
- if isinstance(res_item, dict):
31
- # Если внутри словаря есть ключ 'generated_text' (стандарт Novita/HuggingFace)
32
- return res_item.get("generated_text", str(res_item))
33
- return str(res_item)
34
-
35
- elif isinstance(response, dict):
36
- # Если это сразу словарь
37
- return response.get("generated_text", str(response))
38
-
39
- return str(response)
40
-
41
- except Exception as e:
42
- return f"Ошибка: {str(e)}. Попробуй подождать 10 секунд."
43
-
44
- with gr.Blocks(theme=custom_theme, css=css) as demo:
45
- with gr.Sidebar(elem_id="side-bar"):
46
  gr.Markdown("# **FlareAI**")
47
  gr.Markdown("Flare — твой персональный ассистент")
48
- gr.LoginButton("Войти")
49
 
50
  gr.ChatInterface(fn=predict)
51
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
 
4
+ # Прямое подключение к API
5
+ # Если у тебя в Settings добавлен HF_TOKEN, клиент подтянет его сам
6
+ client = InferenceClient("moonshotai/Kimi-K2-Thinking")
7
 
8
+ def predict(message, history):
9
+ # Формируем промпт так, как любят современные модели
10
+ messages = [{"role": "system", "content": "Тебя зовут Flare."}]
11
+
12
+ for val in history:
13
+ if val[0]: messages.append({"role": "user", "content": val[0]})
14
+ if val[1]: messages.append({"role": "assistant", "content": val[1]})
15
+
16
+ messages.append({"role": "user", "content": message})
17
+
18
+ response = ""
19
+ try:
20
+ # Прямой вызов без посредников
21
+ for msg in client.chat_completion(messages, max_tokens=1024, stream=True):
22
+ token = msg.choices[0].delta.content
23
+ if token:
24
+ response += token
25
+ yield response
26
+ except Exception as e:
27
+ yield f"Упс! Проблема с подключением: {str(e)}"
28
+
29
+ # Настройка стиля
30
  custom_theme = gr.themes.Soft(
31
  primary_hue="yellow",
32
  secondary_hue="blue",
33
  ).set(
34
  body_background_fill="#0057b7",
35
  block_background_fill="#ffdd00",
36
+ button_primary_background_fill="#ffdd00",
37
+ block_label_text_color="#000000"
38
  )
39
 
40
  css = """
41
+ .gradio-container { background-color: #0057b7 !important; }
42
+ #side-info { background-color: #ffdd00; padding: 10px; border-radius: 8px; color: black; }
43
  """
44
 
45
+ with gr.Blocks(theme=custom_theme, css=css, fill_height=True) as demo:
46
+ with gr.Sidebar(elem_id="side-info"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  gr.Markdown("# **FlareAI**")
48
  gr.Markdown("Flare — твой персональный ассистент")
49
+ gr.LoginButton("Войти в HF")
50
 
51
  gr.ChatInterface(fn=predict)
52