Spaces:
Sleeping
Sleeping
echo "Starting MVP setup for Proto-Peanut System (Go-based)" | |
# Create directory structure | |
mkdir -p proto_peanut_mvp/{server,client,logs,tools,weaviate,node_red} | |
cd proto_peanut_mvp | |
# Step 1: Install Go if not installed | |
echo "Checking for Go installation..." | |
if ! [ -x "$(command -v go)" ]; then | |
echo 'Error: Go is not installed. Installing Go...' | |
wget https://golang.org/dl/go1.19.4.linux-amd64.tar.gz | |
tar -xvf go1.19.4.linux-amd64.tar.gz | |
sudo mv go /usr/local | |
export GOROOT=/usr/local/go | |
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH | |
echo 'Go installed successfully.' | |
else | |
echo 'Go is already installed.' | |
fi | |
# Step 2: Set up Go module for the server | |
echo "Setting up Go server..." | |
cd server | |
go mod init proto_peanut_server | |
go get -u github.com/gin-gonic/gin | |
go get github.com/weaviate/weaviate-go-client/v4/weaviate | |
go get github.com/sirupsen/logrus | |
# Step 3: Create the Go server | |
cat > main.go <<EOL | |
package main | |
import ( | |
"github.com/gin-gonic/gin" | |
"time" | |
"github.com/sirupsen/logrus" | |
"github.com/weaviate/weaviate-go-client/v4/weaviate" | |
"fmt" | |
"os" | |
) | |
// Message structure | |
type Message struct { | |
ID int64 \`json:"id"\` | |
SenderName string \`json:"senderName"\` | |
SenderRole string \`json:"senderRole"\` | |
Type string \`json:"type"\` | |
Content string \`json:"content"\` | |
ChainID int64 \`json:"chainId"\` | |
Tags []string \`json:"tags"\` | |
Params map[string]string \`json:"params"\` | |
} | |
var chatroom []Message | |
var log = logrus.New() | |
func main() { | |
r := gin.Default() | |
// Load weaviate client | |
weaviateClient := initWeaviate() | |
// Shared chatroom route | |
r.GET("/getMessages", func(c *gin.Context) { | |
lastKnownId := c.Query("lastKnownId") | |
newMessages := filterMessages(lastKnownId) | |
c.JSON(200, gin.H{"messages": newMessages}) | |
}) | |
// Route to send message | |
r.POST("/sendMessage", func(c *gin.Context) { | |
var msg Message | |
if err := c.ShouldBindJSON(&msg); err == nil { | |
msg.ID = time.Now().UnixNano() / 1e6 // Timestamp in milliseconds as unique ID | |
chatroom = append(chatroom, msg) | |
log.Infof("New message received from %s: %s", msg.SenderName, msg.Content) | |
c.JSON(200, gin.H{"status": "Message received", "messageId": msg.ID}) | |
} else { | |
c.JSON(400, gin.H{"error": err.Error()}) | |
} | |
}) | |
// Start the server | |
r.Run(":8080") | |
} | |
// Filter messages after a certain last known ID | |
func filterMessages(lastKnownId string) []Message { | |
var newMessages []Message | |
for _, msg := range chatroom { | |
if fmt.Sprintf("%d", msg.ID) > lastKnownId { | |
newMessages = append(newMessages, msg) | |
} | |
} | |
return newMessages | |
} | |
// Initialize Weaviate | |
func initWeaviate() *weaviate.Client { | |
config := weaviate.Config{ | |
Scheme: "http", | |
Host: "localhost:8080", | |
} | |
client := weaviate.New(config) | |
return client | |
} | |
EOL | |
# Step 4: Install Docker and Set up Weaviate (run in Docker container) | |
echo "Setting up Weaviate using Docker..." | |
cd ../weaviate | |
cat > docker-compose.yml <<EOL | |
version: '3' | |
services: | |
weaviate: | |
image: semitechnologies/weaviate:latest | |
ports: | |
- "8080:8080" | |
environment: | |
- QUERY_DEFAULTS_LIMIT=20 | |
- AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED=true | |
- PERSISTENCE_DATA_PATH=/var/lib/weaviate | |
volumes: | |
- ./data:/var/lib/weaviate | |
restart: always | |
EOL | |
docker-compose up -d | |
cd .. | |
# Step 5: Set up Gradio Chatbot on Hugging Face Spaces | |
echo "Setting up Gradio Chatbot on Hugging Face Spaces..." | |
cd client | |
cat > chatbot_gradio.py <<EOL | |
import gradio as gr | |
from huggingface_hub import InferenceApi | |
# Hugging Face GPT-based API for proto-peanut | |
model = InferenceApi(repo_id="gpt2", token="YOUR_HUGGING_FACE_TOKEN") | |
def chatbot(input_text): | |
response = model(inputs=input_text) | |
return response["generated_text"] | |
iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Proto-Peanut Chatbot") | |
iface.launch(share=True) | |
EOL | |
# Step 6: Install Node-RED for Workflow Automation | |
echo "Installing Node-RED for workflow automation..." | |
cd ../node_red | |
npm install -g --unsafe-perm node-red | |
cat > start_node_red.sh <<EOL | |
#!/bin/bash | |
echo "Starting Node-RED server..." | |
node-red | |
EOL | |
chmod +x start_node_red.sh | |
cd .. | |
# Step 7: Create a script to run everything | |
echo "Creating run script..." | |
cat > run_mvp.sh <<EOL | |
#!/bin/bash | |
# Run Go Server | |
cd server | |
go run main.go & | |
# Run Weaviate using Docker Compose | |
cd ../weaviate | |
docker-compose up -d & | |
# Run Node-RED | |
cd ../node_red | |
./start_node_red.sh & | |
# Start Gradio Chatbot on Hugging Face Space | |
cd ../client | |
python3 chatbot_gradio.py & | |
EOL | |
chmod +x run_mvp.sh |