Skip to content

Commit

Permalink
ui good
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHEQIUSHUI committed Apr 24, 2024
1 parent 606d8aa commit 88ed688
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
6 changes: 6 additions & 0 deletions qtproj/LLM-Chat/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ int main(int argc, char *argv[])

{
LLMAttrType attr;
attr.template_filename_axmodel = "tinyllama-bf16/tinyllama_l%d.axmodel";
attr.filename_post_axmodel = "tinyllama-bf16/tinyllama_post.axmodel";
attr.filename_tokenizer_model = "tokenizer.model";
attr.filename_tokens_embed = "tinyllama-bf16/tinyllama.model.embed_tokens.weight.bfloat16.bin";


cmdline::parser cmd;
cmd.add<std::string>("template_filename_axmodel", 0, "axmodel path template", false, attr.template_filename_axmodel);
cmd.add<std::string>("filename_post_axmodel", 0, "post axmodel path", false, attr.filename_post_axmodel);
Expand Down
59 changes: 56 additions & 3 deletions qtproj/LLM-Chat/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,39 @@ void thread_llm_runing(LLM *p_llm, QString *p_str, MainWindow *p_win)
ALOGI("thread_llm_runing end");
}

std::string prompt_complete(std::string prompt, TokenizerType tokenizer_type)
{
std::ostringstream oss_prompt;
switch (tokenizer_type)
{
case TKT_LLaMa:
oss_prompt << "<|user|>\n"
<< prompt << "</s><|assistant|>\n";
break;
case TKT_Qwen:
oss_prompt << "<|im_start|>system\nYou are a helpful assistant.<|im_end|>";
oss_prompt << "\n<|im_start|>user\n"
<< prompt << "<|im_end|>\n<|im_start|>assistant\n";
break;
case TKT_HTTP:
default:
oss_prompt << prompt;
break;
}

return oss_prompt.str();
}

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_vBoxLayout = new QVBoxLayout();
ui->widget->setLayout(m_vBoxLayout);
m_vBoxLayout->addSpacing(1080);

connect(this,&MainWindow::sig_llm_output,this,&MainWindow::on_llm_output);
connect(this,&MainWindow::sig_enable_controls,this,&MainWindow::on_enable_controls);
}

MainWindow::~MainWindow()
Expand All @@ -43,12 +69,25 @@ bool MainWindow::InitLLM(LLMAttrType attr)

void MainWindow::append_textedit(const char *p_str)
{
m_textedit_output_vec.back()->append(p_str);
printf("%s", p_str);
emit sig_llm_output(QString(p_str));
}

void MainWindow::on_llm_output(QString str)
{
auto src = m_textedit_output_vec.back()->toPlainText();//str);
src.append(str);
m_textedit_output_vec.back()->setText(src);
printf("%s", str.toStdString().c_str());
fflush(stdout);
}


void MainWindow::enable_controls(bool b_enable)
{
emit sig_enable_controls(b_enable);
}

void MainWindow::on_enable_controls(bool b_enable)
{
ui->btn_ask->setEnabled(b_enable);
ui->txt_msg->setEnabled(b_enable);
Expand Down Expand Up @@ -81,6 +120,10 @@ QTextEdit *create_textedit(QString rgb_color = "220,127,125")

void MainWindow::on_btn_ask_clicked()
{
if(ui->txt_msg->text().trimmed().isEmpty())
{
return;
}
auto txtedit_input = create_textedit();

QHBoxLayout *hboxlayout = new QHBoxLayout();
Expand All @@ -103,8 +146,12 @@ void MainWindow::on_btn_ask_clicked()

txtedit_input->setText(ui->txt_msg->text());

auto prompt = prompt_complete(ui->txt_msg->text().toStdString(),m_llm.getAttr()->tokenizer_type);
printf("%s\n",prompt.c_str());
fflush(stdout);

// 开线程run
QString *str_msg = new QString(ui->txt_msg->text());
QString *str_msg = new QString(prompt.c_str());
std::thread t(thread_llm_runing, &m_llm, str_msg, this);
t.detach();
ui->txt_msg->clear();
Expand All @@ -128,3 +175,9 @@ void MainWindow::on_txt_msg_returnPressed()
{
on_btn_ask_clicked();
}

void MainWindow::on_btn_stop_clicked()
{
m_llm.Stop();
}

9 changes: 9 additions & 0 deletions qtproj/LLM-Chat/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class MainWindow : public QMainWindow
{
Q_OBJECT

signals:
void sig_llm_output(QString str);
void sig_enable_controls(bool enable);
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
Expand All @@ -27,12 +30,18 @@ class MainWindow : public QMainWindow

void enable_controls(bool b_enable);

private slots:
void on_btn_stop_clicked();

private slots:
void on_btn_ask_clicked();
void resizeEvent(QResizeEvent *event) override;

void on_txt_msg_returnPressed();

void on_llm_output(QString str);
void on_enable_controls(bool b_enable);

private:
Ui::MainWindow *ui;
QVBoxLayout *m_vBoxLayout;
Expand Down
45 changes: 44 additions & 1 deletion qtproj/LLM-Chat/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1,0">
<item>
<widget class="QLineEdit" name="txt_msg">
<property name="minimumSize">
Expand Down Expand Up @@ -87,6 +87,7 @@ background-color: rgb(99, 122, 125);</string>
border: 1px solid #dcdfe6;
padding: 10px;
border-radius: 20px;
background-color: rgb(87, 227, 137);
}

QPushButton:hover {
Expand All @@ -104,6 +105,48 @@ QPushButton:pressed, QPushButton:checked {
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_stop">
<property name="minimumSize">
<size>
<width>100</width>
<height>60</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>150</width>
<height>16777215</height>
</size>
</property>
<property name="font">
<font>
<family>DejaVu Sans</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">QPushButton {
border: 1px solid #dcdfe6;
padding: 10px;
border-radius: 20px;
background-color: rgb(246, 97, 81);
}

QPushButton:hover {
background-color: rgb(224, 27, 36);
}

QPushButton:pressed, QPushButton:checked {
border: 1px solid #3a8ee6;
background-color: rgb(192, 28, 40);
}</string>
</property>
<property name="text">
<string>Stop</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
Expand Down
5 changes: 5 additions & 0 deletions src/runner/LLM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ class LLM
return true;
}

LLMAttrType* getAttr()
{
return &_attr;
}

void Deinit()
{
for (int i = 0; i < _attr.axmodel_num; i++)
Expand Down

0 comments on commit 88ed688

Please sign in to comment.