File size: 4,078 Bytes
d9c2213 99290a8 d9c2213 7aa112f d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 7aa112f 99290a8 d9c2213 7aa112f 99290a8 d9c2213 7aa112f d9c2213 7aa112f d9c2213 7aa112f d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 99290a8 d9c2213 | 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | import time
import random
import traceback
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
from g4f.client import Client
from g4f.Provider import __providers__
MODEL = "gpt-4o-mini"
MAX_RETRIES = 10
TIMEOUT = 60
SAFE_PROVIDERS = []
for provider in __providers__:
try:
name = provider.__name__.lower()
if not getattr(provider, "working", False):
continue
if getattr(provider, "needs_auth", False):
continue
if getattr(provider, "use_nodriver", False):
continue
blocked = [
"openai",
"qwen",
"copilot",
"gemini",
"claude",
]
if any(x in name for x in blocked):
continue
SAFE_PROVIDERS.append(provider)
except:
pass
random.shuffle(SAFE_PROVIDERS)
print(f"[+] SAFE PROVIDERS: {len(SAFE_PROVIDERS)}")
client = Client()
class SmartG4F:
def __init__(self):
self.good = []
self.bad = {}
def mark_bad(self, provider, cooldown=300):
self.bad[provider.__name__] = time.time() + cooldown
def is_bad(self, provider):
expire = self.bad.get(provider.__name__)
if not expire:
return False
if time.time() > expire:
del self.bad[provider.__name__]
return False
return True
def provider_pool(self):
providers = []
providers.extend(self.good)
for p in SAFE_PROVIDERS:
if p not in providers and not self.is_bad(p):
providers.append(p)
random.shuffle(providers)
return providers
def ask(self, prompt):
errors = []
for attempt in range(MAX_RETRIES):
pool = self.provider_pool()
if not pool:
return {
"success": False,
"error": "No providers available"
}
provider = random.choice(pool)
print(f"[TRY {attempt+1}] {provider.__name__}")
try:
response = client.chat.completions.create(
model=MODEL,
provider=provider,
messages=[
{
"role": "user",
"content": prompt
}
],
timeout=TIMEOUT,
)
text = response.choices[0].message.content
if not text:
raise Exception("Empty response")
if provider not in self.good:
self.good.append(provider)
return {
"success": True,
"provider": provider.__name__,
"response": text
}
except Exception as e:
err = str(e).lower()
traceback.print_exc()
errors.append({
"provider": provider.__name__,
"error": str(e)
})
if "429" in err:
self.mark_bad(provider, 600)
time.sleep(random.randint(10, 20))
elif "cloudflare" in err:
self.mark_bad(provider, 1200)
time.sleep(20)
elif "timeout" in err:
self.mark_bad(provider, 300)
else:
self.mark_bad(provider, 180)
continue
return {
"success": False,
"errors": errors
}
app = FastAPI()
ai = SmartG4F()
class Query(BaseModel):
prompt: str
@app.get("/")
async def home():
return {
"status": "running",
"providers": len(SAFE_PROVIDERS)
}
@app.post("/ask")
async def ask(query: Query):
return ai.ask(query.prompt)
if __name__ == "__main__":
uvicorn.run(
app,
host="0.0.0.0",
port=7860
) |