Skip to content

Commit f04c4da

Browse files
committed
Wip p2p enhancements
1 parent 9280060 commit f04c4da

File tree

12 files changed

+255
-47
lines changed

12 files changed

+255
-47
lines changed

.github/release.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ changelog:
1313
labels:
1414
- bug
1515
- regression
16+
- title: "🖧 P2P area"
17+
labels:
18+
- area/p2p
1619
- title: Exciting New Features 🎉
1720
labels:
1821
- Semver-Minor

core/cli/worker/worker_p2p.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
type P2P struct {
2222
WorkerFlags `embed:""`
23-
Token string `env:"LOCALAI_TOKEN,TOKEN" help:"JSON list of galleries"`
23+
Token string `env:"LOCALAI_TOKEN,LOCALAI_P2P_TOKEN,TOKEN" help:"P2P token to use"`
2424
NoRunner bool `env:"LOCALAI_NO_RUNNER,NO_RUNNER" help:"Do not start the llama-cpp-rpc-server"`
2525
RunnerAddress string `env:"LOCALAI_RUNNER_ADDRESS,RUNNER_ADDRESS" help:"Address of the llama-cpp-rpc-server"`
2626
RunnerPort string `env:"LOCALAI_RUNNER_PORT,RUNNER_PORT" help:"Port of the llama-cpp-rpc-server"`

core/http/endpoints/localai/welcome.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/gofiber/fiber/v2"
55
"github.com/mudler/LocalAI/core/config"
66
"github.com/mudler/LocalAI/core/gallery"
7+
"github.com/mudler/LocalAI/core/p2p"
78
"github.com/mudler/LocalAI/internal"
89
"github.com/mudler/LocalAI/pkg/model"
910
)
@@ -33,6 +34,7 @@ func WelcomeEndpoint(appConfig *config.ApplicationConfig,
3334
"Models": models,
3435
"ModelsConfig": backendConfigs,
3536
"GalleryConfig": galleryConfigs,
37+
"IsP2PEnabled": p2p.IsP2PEnabled(),
3638
"ApplicationConfig": appConfig,
3739
"ProcessingModels": processingModels,
3840
"TaskTypes": taskTypes,

core/http/routes/ui.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/mudler/LocalAI/core/gallery"
1111
"github.com/mudler/LocalAI/core/http/elements"
1212
"github.com/mudler/LocalAI/core/http/endpoints/localai"
13+
"github.com/mudler/LocalAI/core/p2p"
1314
"github.com/mudler/LocalAI/core/services"
1415
"github.com/mudler/LocalAI/internal"
1516
"github.com/mudler/LocalAI/pkg/model"
@@ -53,6 +54,20 @@ func RegisterUIRoutes(app *fiber.App,
5354

5455
app.Get("/", auth, localai.WelcomeEndpoint(appConfig, cl, ml, modelStatus))
5556

57+
if p2p.IsP2PEnabled() {
58+
app.Get("/p2p", auth, func(c *fiber.Ctx) error {
59+
summary := fiber.Map{
60+
"Title": "LocalAI - P2P dashboard",
61+
"Version": internal.PrintableVersion(),
62+
"Nodes": p2p.GetAvailableNodes(),
63+
"IsP2PEnabled": p2p.IsP2PEnabled(),
64+
}
65+
66+
// Render index
67+
return c.Render("views/p2p", summary)
68+
})
69+
}
70+
5671
// Show the Models page (all models)
5772
app.Get("/browse", auth, func(c *fiber.Ctx) error {
5873
term := c.Query("term")
@@ -87,7 +102,9 @@ func RegisterUIRoutes(app *fiber.App,
87102
"AllTags": tags,
88103
"ProcessingModels": processingModelsData,
89104
"AvailableModels": len(models),
90-
"TaskTypes": taskTypes,
105+
"IsP2PEnabled": p2p.IsP2PEnabled(),
106+
107+
"TaskTypes": taskTypes,
91108
// "ApplicationConfig": appConfig,
92109
}
93110

@@ -243,6 +260,7 @@ func RegisterUIRoutes(app *fiber.App,
243260
"ModelsConfig": backendConfigs,
244261
"Model": c.Params("model"),
245262
"Version": internal.PrintableVersion(),
263+
"IsP2PEnabled": p2p.IsP2PEnabled(),
246264
}
247265

248266
// Render index
@@ -261,6 +279,7 @@ func RegisterUIRoutes(app *fiber.App,
261279
"Title": "LocalAI - Talk",
262280
"ModelsConfig": backendConfigs,
263281
"Model": backendConfigs[0].ID,
282+
"IsP2PEnabled": p2p.IsP2PEnabled(),
264283
"Version": internal.PrintableVersion(),
265284
}
266285

@@ -282,6 +301,7 @@ func RegisterUIRoutes(app *fiber.App,
282301
"ModelsConfig": backendConfigs,
283302
"Model": backendConfigs[0].ID,
284303
"Version": internal.PrintableVersion(),
304+
"IsP2PEnabled": p2p.IsP2PEnabled(),
285305
}
286306

287307
// Render index
@@ -296,6 +316,7 @@ func RegisterUIRoutes(app *fiber.App,
296316
"ModelsConfig": backendConfigs,
297317
"Model": c.Params("model"),
298318
"Version": internal.PrintableVersion(),
319+
"IsP2PEnabled": p2p.IsP2PEnabled(),
299320
}
300321

301322
// Render index
@@ -316,6 +337,7 @@ func RegisterUIRoutes(app *fiber.App,
316337
"ModelsConfig": backendConfigs,
317338
"Model": backendConfigs[0].Name,
318339
"Version": internal.PrintableVersion(),
340+
"IsP2PEnabled": p2p.IsP2PEnabled(),
319341
}
320342

321343
// Render index
@@ -330,6 +352,7 @@ func RegisterUIRoutes(app *fiber.App,
330352
"ModelsConfig": backendConfigs,
331353
"Model": c.Params("model"),
332354
"Version": internal.PrintableVersion(),
355+
"IsP2PEnabled": p2p.IsP2PEnabled(),
333356
}
334357

335358
// Render index
@@ -349,6 +372,7 @@ func RegisterUIRoutes(app *fiber.App,
349372
"Title": "LocalAI - Generate audio with " + backendConfigs[0].Name,
350373
"ModelsConfig": backendConfigs,
351374
"Model": backendConfigs[0].Name,
375+
"IsP2PEnabled": p2p.IsP2PEnabled(),
352376
"Version": internal.PrintableVersion(),
353377
}
354378

core/http/views/chat.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<body class="bg-gray-900 text-gray-200" x-data="{ key: $store.chat.key }">
3838
<div class="flex flex-col min-h-screen">
3939

40-
{{template "views/partials/navbar"}}
40+
{{template "views/partials/navbar" .}}
4141
<div class="chat-container mt-2 mr-2 ml-2 mb-2 bg-gray-800 shadow-lg rounded-lg" >
4242
<!-- Chat Header -->
4343
<div class="border-b border-gray-700 p-4" x-data="{ component: 'menu' }">

core/http/views/p2p.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
{{template "views/partials/head" .}}
4+
5+
<body class="bg-gray-900 text-gray-200">
6+
<div class="flex flex-col min-h-screen">
7+
8+
{{template "views/partials/navbar" .}}
9+
<div class="container mx-auto px-4 flex-grow">
10+
11+
<div class="models mt-12">
12+
<h2 class="text-center text-3xl font-semibold text-gray-100">
13+
P2P</h2>
14+
15+
16+
17+
<div class="text-center text-xs font-semibold text-gray-100">
18+
{{ range .Nodes }}
19+
<button hx-post="/browse/search/models" class="text-blue-500" hx-target="#search-results"
20+
hx-vals='{"search": "{{.ID}}"}'
21+
hx-indicator=".htmx-indicator" >{{.ID}} ({{.IsOnline}})</button>
22+
{{ end }}
23+
</div>
24+
25+
26+
</div>
27+
</div>
28+
29+
{{template "views/partials/footer" .}}
30+
</div>
31+
32+
</body>
33+
</html>

core/http/views/partials/navbar.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<a href="/text2image/" class="text-gray-400 hover:text-white px-3 py-2 rounded"><i class="fas fa-image pr-2"></i> Generate images</a>
2222
<a href="/tts/" class="text-gray-400 hover:text-white px-3 py-2 rounded"><i class="fa-solid fa-music pr-2"></i> TTS </a>
2323
<a href="/talk/" class="text-gray-400 hover:text-white px-3 py-2 rounded"><i class="fa-solid fa-phone pr-2"></i> Talk </a>
24+
{{ if .IsP2PEnabled }}
25+
<a href="/p2p/" class="text-gray-400 hover:text-white px-3 py-2 rounded"><i class="fa-solid fa-phone pr-2"></i> P2P </a>
26+
{{ end }}
2427
<a href="/swagger/" class="text-gray-400 hover:text-white px-3 py-2 rounded"><i class="fas fa-code pr-2"></i> API</a>
2528
</div>
2629
</div>
@@ -34,6 +37,9 @@
3437
<a href="/text2image/" class="block text-gray-400 hover:text-white px-3 py-2 rounded mt-1"><i class="fas fa-image pr-2"></i> Generate images</a>
3538
<a href="/tts/" class="block text-gray-400 hover:text-white px-3 py-2 rounded mt-1"><i class="fa-solid fa-music pr-2"></i> TTS </a>
3639
<a href="/talk/" class="block text-gray-400 hover:text-white px-3 py-2 rounded mt-1"><i class="fa-solid fa-phone pr-2"></i> Talk </a>
40+
{{ if .IsP2PEnabled }}
41+
<a href="/p2p/" class="block text-gray-400 hover:text-white px-3 py-2 rounded mt-1"><i class="fa-solid fa-phone pr-2"></i> P2P </a>
42+
{{ end }}
3743
<a href="/swagger/" class="block text-gray-400 hover:text-white px-3 py-2 rounded mt-1"><i class="fas fa-code pr-2"></i> API</a>
3844
</div>
3945
</div>

core/http/views/talk.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<body class="bg-gray-900 text-gray-200" x-data="{ key: $store.chat.key }">
1111
<div class="flex flex-col min-h-screen">
1212

13-
{{template "views/partials/navbar"}}
13+
{{template "views/partials/navbar" .}}
1414
<div class="chat-container mt-2 mr-2 ml-2 mb-2 bg-gray-800 shadow-lg rounded-lg " >
1515
<!-- Chat Header -->
1616
<div class="border-b border-gray-700 p-4" x-data="{ component: 'menu' }">

core/p2p/node.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package p2p
2+
3+
import (
4+
"sync"
5+
"time"
6+
)
7+
8+
type NodeData struct {
9+
Name string
10+
ID string
11+
TunnelAddress string
12+
LastSeen time.Time
13+
}
14+
15+
func (d NodeData) IsOnline() bool {
16+
now := time.Now()
17+
// if the node was seen in the last 40 seconds, it's online
18+
return now.Sub(d.LastSeen) < 40*time.Second
19+
}
20+
21+
var mu sync.Mutex
22+
var nodes = map[string]NodeData{}
23+
24+
func GetAvailableNodes() []NodeData {
25+
mu.Lock()
26+
defer mu.Unlock()
27+
var availableNodes = []NodeData{}
28+
for _, v := range nodes {
29+
availableNodes = append(availableNodes, v)
30+
}
31+
return availableNodes
32+
}
33+
34+
func AddNode(node NodeData) {
35+
mu.Lock()
36+
defer mu.Unlock()
37+
nodes[node.ID] = node
38+
}

0 commit comments

Comments
 (0)