Spaces:
Sleeping
Sleeping
Update main.go
Browse files
main.go
CHANGED
|
@@ -4,13 +4,11 @@ import (
|
|
| 4 |
"bufio"
|
| 5 |
"bytes"
|
| 6 |
"encoding/json"
|
| 7 |
-
"fmt"
|
| 8 |
"io"
|
| 9 |
"log"
|
| 10 |
"net/http"
|
| 11 |
"os"
|
| 12 |
"strings"
|
| 13 |
-
"time"
|
| 14 |
|
| 15 |
"github.com/google/uuid"
|
| 16 |
)
|
|
@@ -62,7 +60,6 @@ type TextConfig struct {
|
|
| 62 |
}
|
| 63 |
|
| 64 |
func main() {
|
| 65 |
-
// Hugging Face 必须监听 7860
|
| 66 |
port := os.Getenv("PORT")
|
| 67 |
if port == "" {
|
| 68 |
port = "7860"
|
|
@@ -81,7 +78,6 @@ func main() {
|
|
| 81 |
}
|
| 82 |
|
| 83 |
func handleResponses(w http.ResponseWriter, r *http.Request) {
|
| 84 |
-
reqID := uuid.New().String()[:8]
|
| 85 |
if r.Method != http.MethodPost {
|
| 86 |
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
| 87 |
return
|
|
@@ -98,9 +94,9 @@ func handleResponses(w http.ResponseWriter, r *http.Request) {
|
|
| 98 |
}
|
| 99 |
|
| 100 |
if stream {
|
| 101 |
-
handleStreamingResponse(w, waveReq
|
| 102 |
} else {
|
| 103 |
-
handleNonStreamingResponse(w, waveReq
|
| 104 |
}
|
| 105 |
}
|
| 106 |
|
|
@@ -131,11 +127,11 @@ func makeWaveRequest(waveReq *WaveRequest) (*http.Response, error) {
|
|
| 131 |
httpReq.Header.Set("X-Wave-Apitype", "openai-responses")
|
| 132 |
httpReq.Header.Set("X-Wave-Clientid", uuid.New().String())
|
| 133 |
|
| 134 |
-
client := &http.Client{
|
| 135 |
return client.Do(httpReq)
|
| 136 |
}
|
| 137 |
|
| 138 |
-
func handleStreamingResponse(w http.ResponseWriter, waveReq *WaveRequest
|
| 139 |
resp, err := makeWaveRequest(waveReq)
|
| 140 |
if err != nil {
|
| 141 |
http.Error(w, err.Error(), http.StatusBadGateway)
|
|
@@ -152,7 +148,7 @@ func handleStreamingResponse(w http.ResponseWriter, waveReq *WaveRequest, reqID
|
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
-
func handleNonStreamingResponse(w http.ResponseWriter, waveReq *WaveRequest
|
| 156 |
resp, _ := makeWaveRequest(waveReq)
|
| 157 |
defer resp.Body.Close()
|
| 158 |
|
|
|
|
| 4 |
"bufio"
|
| 5 |
"bytes"
|
| 6 |
"encoding/json"
|
|
|
|
| 7 |
"io"
|
| 8 |
"log"
|
| 9 |
"net/http"
|
| 10 |
"os"
|
| 11 |
"strings"
|
|
|
|
| 12 |
|
| 13 |
"github.com/google/uuid"
|
| 14 |
)
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
func main() {
|
|
|
|
| 63 |
port := os.Getenv("PORT")
|
| 64 |
if port == "" {
|
| 65 |
port = "7860"
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
func handleResponses(w http.ResponseWriter, r *http.Request) {
|
|
|
|
| 81 |
if r.Method != http.MethodPost {
|
| 82 |
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
| 83 |
return
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
if stream {
|
| 97 |
+
handleStreamingResponse(w, waveReq)
|
| 98 |
} else {
|
| 99 |
+
handleNonStreamingResponse(w, waveReq)
|
| 100 |
}
|
| 101 |
}
|
| 102 |
|
|
|
|
| 127 |
httpReq.Header.Set("X-Wave-Apitype", "openai-responses")
|
| 128 |
httpReq.Header.Set("X-Wave-Clientid", uuid.New().String())
|
| 129 |
|
| 130 |
+
client := &http.Client{}
|
| 131 |
return client.Do(httpReq)
|
| 132 |
}
|
| 133 |
|
| 134 |
+
func handleStreamingResponse(w http.ResponseWriter, waveReq *WaveRequest) {
|
| 135 |
resp, err := makeWaveRequest(waveReq)
|
| 136 |
if err != nil {
|
| 137 |
http.Error(w, err.Error(), http.StatusBadGateway)
|
|
|
|
| 148 |
}
|
| 149 |
}
|
| 150 |
|
| 151 |
+
func handleNonStreamingResponse(w http.ResponseWriter, waveReq *WaveRequest) {
|
| 152 |
resp, _ := makeWaveRequest(waveReq)
|
| 153 |
defer resp.Body.Close()
|
| 154 |
|