File size: 4,831 Bytes
2340e06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash

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