AugustLight commited on
Commit
0062f54
·
verified ·
1 Parent(s): 501365f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from huggingface_hub import hf_hub_download
3
  from llama_cpp import Llama
4
 
 
5
  model = None
6
 
7
  def load_model():
@@ -44,33 +45,28 @@ def respond(message, history, system_message, max_new_tokens, temperature, top_p
44
 
45
  print(f"Генерируем ответ для контекста длиной {len(context)} символов")
46
 
47
- # Используем потоковый вывод
48
  response = model(
49
  prompt=context,
50
  max_tokens=max_new_tokens,
51
  temperature=temperature,
52
  top_p=top_p,
53
  stop=["User:", "\n\n", "<|endoftext|>"],
54
- echo=False,
55
- stream=True # Включаем потоковое отображение
56
  )
57
 
58
- # Генерация ответа с использованием yield
59
- generated_text = ""
60
- for token in response:
61
- generated_text += token["text"]
62
- yield generated_text.strip()
63
 
64
  except Exception as e:
65
  error_msg = f"Произошла ошибка: {str(e)}"
66
  print(error_msg)
67
- yield error_msg
68
 
69
- demo = gr.Interface(
70
- fn=respond,
71
- inputs=[
72
- gr.Textbox(lines=2, label="Сообщение пользователя"),
73
- gr.State(),
74
  gr.Textbox(
75
  value="Ты дружелюбный и полезный ассистент. Отвечай обдуманно и по делу.",
76
  label="System message"
@@ -97,10 +93,14 @@ demo = gr.Interface(
97
  label="Top-p (nucleus sampling)"
98
  ),
99
  ],
100
- outputs="text",
101
  title="GGUF Chat Model",
102
  description="Чат с GGUF моделью (LLight-3.2-3B-Instruct)",
103
- live=True # Включаем потоковую генерацию ответа
 
 
 
 
 
104
  )
105
 
106
  # Запускаем приложение
@@ -112,4 +112,4 @@ if __name__ == "__main__":
112
  except Exception as e:
113
  print(f"Ошибка при инициализации: {str(e)}")
114
 
115
- demo.launch()
 
2
  from huggingface_hub import hf_hub_download
3
  from llama_cpp import Llama
4
 
5
+ # Так надо
6
  model = None
7
 
8
  def load_model():
 
45
 
46
  print(f"Генерируем ответ для контекста длиной {len(context)} символов")
47
 
 
48
  response = model(
49
  prompt=context,
50
  max_tokens=max_new_tokens,
51
  temperature=temperature,
52
  top_p=top_p,
53
  stop=["User:", "\n\n", "<|endoftext|>"],
54
+ echo=False # Не возвращать промпт в ответе
 
55
  )
56
 
57
+ generated_text = response['choices'][0]['text']
58
+ print(f"Ответ сгенерирован успешно, длина: {len(generated_text)}")
59
+ return generated_text.strip()
 
 
60
 
61
  except Exception as e:
62
  error_msg = f"Произошла ошибка: {str(e)}"
63
  print(error_msg)
64
+ return error_msg
65
 
66
+
67
+ demo = gr.ChatInterface(
68
+ respond,
69
+ additional_inputs=[
 
70
  gr.Textbox(
71
  value="Ты дружелюбный и полезный ассистент. Отвечай обдуманно и по делу.",
72
  label="System message"
 
93
  label="Top-p (nucleus sampling)"
94
  ),
95
  ],
 
96
  title="GGUF Chat Model",
97
  description="Чат с GGUF моделью (LLight-3.2-3B-Instruct)",
98
+ examples=[
99
+ ["Привет! Как дела?"],
100
+ ["Расскажи мне о себе"],
101
+ ["Что ты умеешь делать?"]
102
+ ],
103
+ cache_examples=False
104
  )
105
 
106
  # Запускаем приложение
 
112
  except Exception as e:
113
  print(f"Ошибка при инициализации: {str(e)}")
114
 
115
+ demo.launch()