poe router
Browse files- App/Chat/PoeChatrouter.py +45 -34
App/Chat/PoeChatrouter.py
CHANGED
@@ -12,49 +12,53 @@ from ballyregan import ProxyFetcher
|
|
12 |
|
13 |
chat_router = APIRouter(tags=["Chat"])
|
14 |
proxy = ""
|
15 |
-
proxies = [
|
16 |
-
"http://51.89.14.70:80",
|
17 |
-
"http://52.151.210.204:9002",
|
18 |
-
"http://38.180.36.19:80",
|
19 |
-
"http://38.54.79.150:80",
|
20 |
-
"https://80.91.26.137:3128",
|
21 |
-
"http://82.223.102.92:9443",
|
22 |
-
"http://189.240.60.166:9090",
|
23 |
-
"https://189.240.60.168:9090",
|
24 |
-
"http://189.240.60.171:9090",
|
25 |
-
]
|
26 |
|
27 |
|
28 |
class InputData(BaseModel):
|
29 |
input: dict
|
30 |
version: str = "727e49a643e999d602a896c774a0658ffefea21465756a6ce24b7ea4165eba6a"
|
31 |
proxies: list[str] = []
|
|
|
32 |
|
33 |
|
34 |
-
async def fetch_predictions(data):
|
35 |
global proxy, proxies
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
if
|
41 |
-
|
42 |
-
try:
|
43 |
-
async with session.post(
|
44 |
-
"https://replicate.com/api/predictions",
|
45 |
-
json=data,
|
46 |
-
timeout=5,
|
47 |
-
) as response:
|
48 |
-
if str(response.status).startswith("4"):
|
49 |
continue
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
|
60 |
@chat_router.post("/predictions")
|
@@ -62,6 +66,13 @@ async def get_predictions(input_data: InputData):
|
|
62 |
global proxies
|
63 |
if input_data.proxies != []:
|
64 |
proxies = input_data.proxies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
data = {
|
66 |
"input": input_data.input,
|
67 |
"is_training": False,
|
@@ -70,7 +81,7 @@ async def get_predictions(input_data: InputData):
|
|
70 |
"version": input_data.version,
|
71 |
}
|
72 |
try:
|
73 |
-
predictions = await fetch_predictions(data)
|
74 |
return predictions
|
75 |
except Exception as e:
|
76 |
raise HTTPException(status_code=500, detail=f"Internal Server Error: {str(e)}")
|
|
|
12 |
|
13 |
chat_router = APIRouter(tags=["Chat"])
|
14 |
proxy = ""
|
15 |
+
proxies = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
class InputData(BaseModel):
|
19 |
input: dict
|
20 |
version: str = "727e49a643e999d602a896c774a0658ffefea21465756a6ce24b7ea4165eba6a"
|
21 |
proxies: list[str] = []
|
22 |
+
is_proxied: bool = False
|
23 |
|
24 |
|
25 |
+
async def fetch_predictions(data, is_proxied=False):
|
26 |
global proxy, proxies
|
27 |
+
if is_proxied:
|
28 |
+
proxy_set = proxy != ""
|
29 |
+
async with ClientSession() as session:
|
30 |
+
for p in proxies:
|
31 |
+
if proxy_set:
|
32 |
+
if p != proxy:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
continue
|
34 |
+
try:
|
35 |
+
async with session.post(
|
36 |
+
"https://replicate.com/api/predictions",
|
37 |
+
json=data,
|
38 |
+
timeout=5,
|
39 |
+
) as response:
|
40 |
+
if str(response.status).startswith("4"):
|
41 |
+
continue
|
42 |
+
proxy = str(p)
|
43 |
+
temp = await response.json()
|
44 |
+
print(temp)
|
45 |
+
return temp
|
46 |
+
except Exception as e:
|
47 |
+
print("Error fetching", e)
|
48 |
+
pass
|
49 |
+
proxy = ""
|
50 |
+
else:
|
51 |
+
try:
|
52 |
+
async with session.post(
|
53 |
+
"https://replicate.com/api/predictions",
|
54 |
+
json=data,
|
55 |
+
timeout=5,
|
56 |
+
) as response:
|
57 |
+
temp = await response.json()
|
58 |
+
return temp
|
59 |
+
except Exception as e:
|
60 |
+
print("Error fetching", e)
|
61 |
+
pass
|
62 |
|
63 |
|
64 |
@chat_router.post("/predictions")
|
|
|
66 |
global proxies
|
67 |
if input_data.proxies != []:
|
68 |
proxies = input_data.proxies
|
69 |
+
else:
|
70 |
+
|
71 |
+
proxies = [
|
72 |
+
"http://51.89.14.70:80",
|
73 |
+
"http://52.151.210.204:9002",
|
74 |
+
"http://38.180.36.19:80",
|
75 |
+
]
|
76 |
data = {
|
77 |
"input": input_data.input,
|
78 |
"is_training": False,
|
|
|
81 |
"version": input_data.version,
|
82 |
}
|
83 |
try:
|
84 |
+
predictions = await fetch_predictions(data, input_data.is_proxied)
|
85 |
return predictions
|
86 |
except Exception as e:
|
87 |
raise HTTPException(status_code=500, detail=f"Internal Server Error: {str(e)}")
|