Skip to content

Commit

Permalink
Wip p2p enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
mudler committed Jul 6, 2024
1 parent 9280060 commit f04c4da
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ changelog:
labels:
- bug
- regression
- title: "🖧 P2P area"
labels:
- area/p2p
- title: Exciting New Features 🎉
labels:
- Semver-Minor
Expand Down
2 changes: 1 addition & 1 deletion core/cli/worker/worker_p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

type P2P struct {
WorkerFlags `embed:""`
Token string `env:"LOCALAI_TOKEN,TOKEN" help:"JSON list of galleries"`
Token string `env:"LOCALAI_TOKEN,LOCALAI_P2P_TOKEN,TOKEN" help:"P2P token to use"`
NoRunner bool `env:"LOCALAI_NO_RUNNER,NO_RUNNER" help:"Do not start the llama-cpp-rpc-server"`
RunnerAddress string `env:"LOCALAI_RUNNER_ADDRESS,RUNNER_ADDRESS" help:"Address of the llama-cpp-rpc-server"`
RunnerPort string `env:"LOCALAI_RUNNER_PORT,RUNNER_PORT" help:"Port of the llama-cpp-rpc-server"`
Expand Down
2 changes: 2 additions & 0 deletions core/http/endpoints/localai/welcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/mudler/LocalAI/core/config"
"github.com/mudler/LocalAI/core/gallery"
"github.com/mudler/LocalAI/core/p2p"
"github.com/mudler/LocalAI/internal"
"github.com/mudler/LocalAI/pkg/model"
)
Expand Down Expand Up @@ -33,6 +34,7 @@ func WelcomeEndpoint(appConfig *config.ApplicationConfig,
"Models": models,
"ModelsConfig": backendConfigs,
"GalleryConfig": galleryConfigs,
"IsP2PEnabled": p2p.IsP2PEnabled(),
"ApplicationConfig": appConfig,
"ProcessingModels": processingModels,
"TaskTypes": taskTypes,
Expand Down
26 changes: 25 additions & 1 deletion core/http/routes/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/mudler/LocalAI/core/gallery"
"github.com/mudler/LocalAI/core/http/elements"
"github.com/mudler/LocalAI/core/http/endpoints/localai"
"github.com/mudler/LocalAI/core/p2p"
"github.com/mudler/LocalAI/core/services"
"github.com/mudler/LocalAI/internal"
"github.com/mudler/LocalAI/pkg/model"
Expand Down Expand Up @@ -53,6 +54,20 @@ func RegisterUIRoutes(app *fiber.App,

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

if p2p.IsP2PEnabled() {
app.Get("/p2p", auth, func(c *fiber.Ctx) error {
summary := fiber.Map{
"Title": "LocalAI - P2P dashboard",
"Version": internal.PrintableVersion(),
"Nodes": p2p.GetAvailableNodes(),
"IsP2PEnabled": p2p.IsP2PEnabled(),
}

// Render index
return c.Render("views/p2p", summary)
})
}

// Show the Models page (all models)
app.Get("/browse", auth, func(c *fiber.Ctx) error {
term := c.Query("term")
Expand Down Expand Up @@ -87,7 +102,9 @@ func RegisterUIRoutes(app *fiber.App,
"AllTags": tags,
"ProcessingModels": processingModelsData,
"AvailableModels": len(models),
"TaskTypes": taskTypes,
"IsP2PEnabled": p2p.IsP2PEnabled(),

"TaskTypes": taskTypes,
// "ApplicationConfig": appConfig,
}

Expand Down Expand Up @@ -243,6 +260,7 @@ func RegisterUIRoutes(app *fiber.App,
"ModelsConfig": backendConfigs,
"Model": c.Params("model"),
"Version": internal.PrintableVersion(),
"IsP2PEnabled": p2p.IsP2PEnabled(),
}

// Render index
Expand All @@ -261,6 +279,7 @@ func RegisterUIRoutes(app *fiber.App,
"Title": "LocalAI - Talk",
"ModelsConfig": backendConfigs,
"Model": backendConfigs[0].ID,
"IsP2PEnabled": p2p.IsP2PEnabled(),
"Version": internal.PrintableVersion(),
}

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

// Render index
Expand All @@ -296,6 +316,7 @@ func RegisterUIRoutes(app *fiber.App,
"ModelsConfig": backendConfigs,
"Model": c.Params("model"),
"Version": internal.PrintableVersion(),
"IsP2PEnabled": p2p.IsP2PEnabled(),
}

// Render index
Expand All @@ -316,6 +337,7 @@ func RegisterUIRoutes(app *fiber.App,
"ModelsConfig": backendConfigs,
"Model": backendConfigs[0].Name,
"Version": internal.PrintableVersion(),
"IsP2PEnabled": p2p.IsP2PEnabled(),
}

// Render index
Expand All @@ -330,6 +352,7 @@ func RegisterUIRoutes(app *fiber.App,
"ModelsConfig": backendConfigs,
"Model": c.Params("model"),
"Version": internal.PrintableVersion(),
"IsP2PEnabled": p2p.IsP2PEnabled(),
}

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

Expand Down
2 changes: 1 addition & 1 deletion core/http/views/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<body class="bg-gray-900 text-gray-200" x-data="{ key: $store.chat.key }">
<div class="flex flex-col min-h-screen">

{{template "views/partials/navbar"}}
{{template "views/partials/navbar" .}}
<div class="chat-container mt-2 mr-2 ml-2 mb-2 bg-gray-800 shadow-lg rounded-lg" >
<!-- Chat Header -->
<div class="border-b border-gray-700 p-4" x-data="{ component: 'menu' }">
Expand Down
33 changes: 33 additions & 0 deletions core/http/views/p2p.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
{{template "views/partials/head" .}}

<body class="bg-gray-900 text-gray-200">
<div class="flex flex-col min-h-screen">

{{template "views/partials/navbar" .}}
<div class="container mx-auto px-4 flex-grow">

<div class="models mt-12">
<h2 class="text-center text-3xl font-semibold text-gray-100">
P2P</h2>



<div class="text-center text-xs font-semibold text-gray-100">
{{ range .Nodes }}
<button hx-post="/browse/search/models" class="text-blue-500" hx-target="#search-results"
hx-vals='{"search": "{{.ID}}"}'
hx-indicator=".htmx-indicator" >{{.ID}} ({{.IsOnline}})</button>
{{ end }}
</div>


</div>
</div>

{{template "views/partials/footer" .}}
</div>

</body>
</html>
6 changes: 6 additions & 0 deletions core/http/views/partials/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<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>
<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>
<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>
{{ if .IsP2PEnabled }}
<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>
{{ end }}
<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>
</div>
</div>
Expand All @@ -34,6 +37,9 @@
<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>
<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>
<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>
{{ if .IsP2PEnabled }}
<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>
{{ end }}
<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>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion core/http/views/talk.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<body class="bg-gray-900 text-gray-200" x-data="{ key: $store.chat.key }">
<div class="flex flex-col min-h-screen">

{{template "views/partials/navbar"}}
{{template "views/partials/navbar" .}}
<div class="chat-container mt-2 mr-2 ml-2 mb-2 bg-gray-800 shadow-lg rounded-lg " >
<!-- Chat Header -->
<div class="border-b border-gray-700 p-4" x-data="{ component: 'menu' }">
Expand Down
38 changes: 38 additions & 0 deletions core/p2p/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package p2p

import (
"sync"
"time"
)

type NodeData struct {
Name string
ID string
TunnelAddress string
LastSeen time.Time
}

func (d NodeData) IsOnline() bool {
now := time.Now()
// if the node was seen in the last 40 seconds, it's online
return now.Sub(d.LastSeen) < 40*time.Second
}

var mu sync.Mutex
var nodes = map[string]NodeData{}

func GetAvailableNodes() []NodeData {

Check failure on line 24 in core/p2p/node.go

View workflow job for this annotation

GitHub Actions / build-linux-arm

other declaration of GetAvailableNodes

Check failure on line 24 in core/p2p/node.go

View workflow job for this annotation

GitHub Actions / tests-linux (1.21.x)

other declaration of GetAvailableNodes
mu.Lock()
defer mu.Unlock()
var availableNodes = []NodeData{}
for _, v := range nodes {
availableNodes = append(availableNodes, v)
}
return availableNodes
}

func AddNode(node NodeData) {
mu.Lock()
defer mu.Unlock()
nodes[node.ID] = node
}
Loading

0 comments on commit f04c4da

Please sign in to comment.