-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (84 loc) · 3.19 KB
/
Copy pathmain.cpp
File metadata and controls
103 lines (84 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "TripController.h"
#include "AIController.h"
#include <QStandardPaths>
#include <QDir>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QFileInfo>
#include <QSaveFile>
#include <QPointer>
#include <QtConcurrent>
#include <functional>
#include <iostream>
#include "llama.h"
#include "common.h"
#include "sampling.h"
void handle_ai(AIController *controller, QGuiApplication *app) {
QNetworkAccessManager *nman = new QNetworkAccessManager(app);
QString modelDir = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/models";
QString modelPath = modelDir + "/granite-4.0-micro-Q4_K_M.gguf";
QUrl modelUrl("https://huggingface.co/ibm-granite/granite-4.0-micro-GGUF/resolve/main/granite-4.0-micro-Q4_K_M.gguf?download=true");
controller->ensureDownloaded(
nman,
modelUrl,
modelPath,
/* onProgress */ [controller](qint64 received, qint64 total) {
// Update a QML/Qt progress bar here
// total may be -1 if server doesn't send content-length
controller->setDownloadProgress(received);
},
/* onDone */ [modelPath, controller](bool ok, const QString& err) {
if (!ok) {
qWarning() << "Model download failed:" << err;
return;
}
qDebug() << "Model ready at:" << modelPath;
QMetaObject::invokeMethod(controller, [modelPath, controller]() {
controller->setPageIndex(0);
}, Qt::QueuedConnection);
try {
QtConcurrent::run([modelPath, controller]() {
try {
controller->query_granite(modelPath, "...");
} catch (const std::exception &e) {
qWarning() << "query_granite exception:" << e.what();
}
});
return;
} catch (const std::exception &e) {
qWarning() << "query_granite exception:" << e.what();
} catch (...) {
qWarning() << "query_granite unknown exception";
}
});
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
auto aiController = std::make_unique<AIController>();
engine.rootContext()->setContextProperty("aiController", aiController.get());
TripController tripController;
tripController.setAIController(aiController.get());
engine.rootContext()->setContextProperty("tripController", &tripController);
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreationFailed,
&app,
[]() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.loadFromModule("FuelEfficiencyCoach", "Main");
QObject *root = engine.rootObjects().first();
aiController->setRootObject(root);
handle_ai(aiController.get(), &app);
if (engine.rootObjects().isEmpty())
return -1;
int result = app.exec();
aiController.reset(); // Explicitly delete before app exits
return result;
}