Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,61 +2,15 @@ from flask import Flask, request, Response
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import time
|
| 5 |
-
import random
|
| 6 |
-
import socket
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
-
class ProxyPool:
|
| 11 |
-
def __init__(self, proxy_data):
|
| 12 |
-
self.proxies = self.parse_proxies(proxy_data)
|
| 13 |
-
self.proxies.append({"type": "LOCAL", "host": self.get_local_ip(), "port": None})
|
| 14 |
-
|
| 15 |
-
def parse_proxies(self, proxy_data):
|
| 16 |
-
proxies = []
|
| 17 |
-
for country, proxy_list in proxy_data['proxy_list'].items():
|
| 18 |
-
for proxy in proxy_list:
|
| 19 |
-
proxies.append({
|
| 20 |
-
"type": proxy['type'],
|
| 21 |
-
"host": proxy['host'],
|
| 22 |
-
"port": proxy['port'],
|
| 23 |
-
"country": country
|
| 24 |
-
})
|
| 25 |
-
return proxies
|
| 26 |
-
|
| 27 |
-
def get_local_ip(self):
|
| 28 |
-
try:
|
| 29 |
-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 30 |
-
s.connect(("8.8.8.8", 80))
|
| 31 |
-
local_ip = s.getsockname()[0]
|
| 32 |
-
s.close()
|
| 33 |
-
return local_ip
|
| 34 |
-
except:
|
| 35 |
-
return "127.0.0.1"
|
| 36 |
-
|
| 37 |
-
def get_proxy(self):
|
| 38 |
-
return random.choice(self.proxies)
|
| 39 |
-
|
| 40 |
-
# 加载代理数据
|
| 41 |
-
with open('proxy_data.json', 'r') as f:
|
| 42 |
-
proxy_data = json.load(f)
|
| 43 |
-
|
| 44 |
-
proxy_pool = ProxyPool(proxy_data)
|
| 45 |
-
|
| 46 |
def get_token():
|
| 47 |
url = "https://fluxaiweb.com/flux/getToken"
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
else:
|
| 53 |
-
proxy_url = f"{proxy['type'].lower()}://{proxy['host']}:{proxy['port']}"
|
| 54 |
-
response = requests.get(url, proxies={"http": proxy_url, "https": proxy_url}, timeout=10)
|
| 55 |
-
if response.status_code == 200:
|
| 56 |
-
response_json = response.json()
|
| 57 |
-
return response_json.get("data", {}).get("token")
|
| 58 |
-
except requests.exceptions.RequestException as e:
|
| 59 |
-
print(f"Error with proxy {proxy}: {e}")
|
| 60 |
return None
|
| 61 |
|
| 62 |
def req_flux(token, prompt_value, aspect_ratio="1:1", output_format="webp", num_outputs=1, output_quality=90):
|
|
@@ -72,18 +26,13 @@ def req_flux(token, prompt_value, aspect_ratio="1:1", output_format="webp", num_
|
|
| 72 |
'Content-Type': 'application/json',
|
| 73 |
'token': token
|
| 74 |
}
|
| 75 |
-
proxy = proxy_pool.get_proxy()
|
| 76 |
try:
|
| 77 |
-
|
| 78 |
-
response = requests.post(url, headers=headers, json=payload, timeout=30)
|
| 79 |
-
else:
|
| 80 |
-
proxy_url = f"{proxy['type'].lower()}://{proxy['host']}:{proxy['port']}"
|
| 81 |
-
response = requests.post(url, headers=headers, json=payload, proxies={"http": proxy_url, "https": proxy_url}, timeout=30)
|
| 82 |
response.raise_for_status()
|
| 83 |
data = response.json()
|
| 84 |
return data.get("data", {}).get("image")
|
| 85 |
except requests.exceptions.RequestException as e:
|
| 86 |
-
print(f"Error
|
| 87 |
return None
|
| 88 |
|
| 89 |
def generate_fake_stream(image_url):
|
|
@@ -106,6 +55,7 @@ def chat_completions():
|
|
| 106 |
messages = data.get('messages', [])
|
| 107 |
stream = data.get('stream', False)
|
| 108 |
|
|
|
|
| 109 |
prompt = next((msg['content'] for msg in reversed(messages) if msg['role'] == 'user'), None)
|
| 110 |
|
| 111 |
if not prompt:
|
|
@@ -139,7 +89,7 @@ def chat_completions():
|
|
| 139 |
],
|
| 140 |
"usage": {
|
| 141 |
"prompt_tokens": len(prompt.split()),
|
| 142 |
-
"completion_tokens": 20,
|
| 143 |
"total_tokens": len(prompt.split()) + 20
|
| 144 |
}
|
| 145 |
}
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import time
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def get_token():
|
| 9 |
url = "https://fluxaiweb.com/flux/getToken"
|
| 10 |
+
response = requests.get(url)
|
| 11 |
+
if response.status_code == 200:
|
| 12 |
+
response_json = response.json()
|
| 13 |
+
return response_json.get("data", {}).get("token")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return None
|
| 15 |
|
| 16 |
def req_flux(token, prompt_value, aspect_ratio="1:1", output_format="webp", num_outputs=1, output_quality=90):
|
|
|
|
| 26 |
'Content-Type': 'application/json',
|
| 27 |
'token': token
|
| 28 |
}
|
|
|
|
| 29 |
try:
|
| 30 |
+
response = requests.post(url, headers=headers, json=payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
response.raise_for_status()
|
| 32 |
data = response.json()
|
| 33 |
return data.get("data", {}).get("image")
|
| 34 |
except requests.exceptions.RequestException as e:
|
| 35 |
+
print(f"Error making request: {e}")
|
| 36 |
return None
|
| 37 |
|
| 38 |
def generate_fake_stream(image_url):
|
|
|
|
| 55 |
messages = data.get('messages', [])
|
| 56 |
stream = data.get('stream', False)
|
| 57 |
|
| 58 |
+
# Extract the prompt from the last user message
|
| 59 |
prompt = next((msg['content'] for msg in reversed(messages) if msg['role'] == 'user'), None)
|
| 60 |
|
| 61 |
if not prompt:
|
|
|
|
| 89 |
],
|
| 90 |
"usage": {
|
| 91 |
"prompt_tokens": len(prompt.split()),
|
| 92 |
+
"completion_tokens": 20, # Approximate
|
| 93 |
"total_tokens": len(prompt.split()) + 20
|
| 94 |
}
|
| 95 |
}
|