Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,127 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
@app.get("/")
|
| 6 |
def greet_json():
|
| 7 |
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
@app.get("/")
|
| 6 |
def greet_json():
|
| 7 |
return {"Hello": "World!"}
|
| 8 |
+
|
| 9 |
+
@app.post("/bingimage")
|
| 10 |
+
async def bing_image(request: Request):
|
| 11 |
+
data = await request.json()
|
| 12 |
+
prompt = data.get("prompt")
|
| 13 |
+
# Placeholder for actual data to return
|
| 14 |
+
import asyncio
|
| 15 |
+
import json
|
| 16 |
+
import re
|
| 17 |
+
from urllib.parse import quote
|
| 18 |
+
gist_url = "https://gist.githubusercontent.com/parasshah10/1519bed2f7f00e399f4a6b9300e84fc1/raw"
|
| 19 |
+
import httpx
|
| 20 |
+
|
| 21 |
+
TIMEOUT = 200
|
| 22 |
+
TOKEN_FILE = "token.json"
|
| 23 |
+
BASE_URL = "https://www.bing.com"
|
| 24 |
+
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"
|
| 25 |
+
|
| 26 |
+
class BingDalle:
|
| 27 |
+
def __init__(self, auth_cookie):
|
| 28 |
+
self.auth_cookie = self._load_config(auth_cookie)
|
| 29 |
+
self.headers = {
|
| 30 |
+
"User-Agent": USER_AGENT,
|
| 31 |
+
"Cookie": f"_U={self.auth_cookie}",
|
| 32 |
+
}
|
| 33 |
+
self.client = httpx.AsyncClient(
|
| 34 |
+
base_url=BASE_URL, headers=self.headers, timeout=TIMEOUT
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
async def __aenter__(self):
|
| 38 |
+
return self
|
| 39 |
+
|
| 40 |
+
async def __aexit__(self, *_):
|
| 41 |
+
await self.client.aclose()
|
| 42 |
+
|
| 43 |
+
def _load_config(self, auth_cookie):
|
| 44 |
+
if auth_cookie:
|
| 45 |
+
return auth_cookie
|
| 46 |
+
try:
|
| 47 |
+
with open(TOKEN_FILE, "r") as file:
|
| 48 |
+
config = json.load(file)
|
| 49 |
+
return config.get("_U")
|
| 50 |
+
except FileNotFoundError:
|
| 51 |
+
raise ValueError("Auth cookie not provided and token file not found.")
|
| 52 |
+
|
| 53 |
+
async def _get_coins(self):
|
| 54 |
+
response = await self.client.get("/images/create")
|
| 55 |
+
if response.is_success:
|
| 56 |
+
coins_match = re.search(r'coins available">(\d+)<', response.text)
|
| 57 |
+
return coins_match[1] if coins_match else None
|
| 58 |
+
return None
|
| 59 |
+
|
| 60 |
+
async def _poll_results(self, prompt, request_id):
|
| 61 |
+
encoded_prompt = quote(prompt)
|
| 62 |
+
result_url = f"/images/create/async/results/{request_id}?q={encoded_prompt}"
|
| 63 |
+
while True:
|
| 64 |
+
response = await self.client.get(result_url)
|
| 65 |
+
if response.is_success and "gir_async" in response.text:
|
| 66 |
+
return response
|
| 67 |
+
await asyncio.sleep(5)
|
| 68 |
+
|
| 69 |
+
def _construct_url(self, coins, prompt):
|
| 70 |
+
encoded_prompt = quote(prompt)
|
| 71 |
+
rt_value = "4" if int(coins) > 0 else "3"
|
| 72 |
+
return f"/images/create?q={encoded_prompt}&rt={rt_value}&FORM=GENCRE"
|
| 73 |
+
|
| 74 |
+
async def _get_request_id(self, prompt, post_url):
|
| 75 |
+
data = {"q": prompt, "qs": "ds"}
|
| 76 |
+
response = await self.client.post(post_url, data=data, follow_redirects=True)
|
| 77 |
+
if response.is_success:
|
| 78 |
+
request_id = re.search(r"id=([^&]+)", str(response.url))
|
| 79 |
+
return request_id[1] if request_id else None
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
def _handle_poll_result(self, poll_results, prompt):
|
| 83 |
+
src_urls = list(
|
| 84 |
+
{
|
| 85 |
+
url.split("?w=")[0]
|
| 86 |
+
for url in re.findall(r'src="([^"]+)"', poll_results.text)
|
| 87 |
+
if url.startswith(("http", "https")) and url.endswith("ImgGn")
|
| 88 |
+
}
|
| 89 |
+
)
|
| 90 |
+
return [{"url": src_url} for src_url in src_urls]
|
| 91 |
+
|
| 92 |
+
async def generate_images(self, prompt):
|
| 93 |
+
coins = await self._get_coins()
|
| 94 |
+
post_url = self._construct_url(coins, prompt)
|
| 95 |
+
request_id = await self._get_request_id(prompt, post_url)
|
| 96 |
+
poll_results = await self._poll_results(prompt, request_id)
|
| 97 |
+
return self._handle_poll_result(poll_results, prompt)
|
| 98 |
+
|
| 99 |
+
async def upload_to_imagebb(image_url, api_key):
|
| 100 |
+
upload_url = "https://api.imgbb.com/1/upload"
|
| 101 |
+
params = {
|
| 102 |
+
"key": api_key,
|
| 103 |
+
"image": image_url,
|
| 104 |
+
}
|
| 105 |
+
async with httpx.AsyncClient() as client:
|
| 106 |
+
response = await client.post(upload_url, params=params)
|
| 107 |
+
if response.is_success:
|
| 108 |
+
return response.json()["data"]["url"]
|
| 109 |
+
return None
|
| 110 |
+
|
| 111 |
+
async def main(prompt, auth_cookie, imagebb_api_key):
|
| 112 |
+
async with BingDalle(auth_cookie) as bing:
|
| 113 |
+
image_urls = await bing.generate_images(prompt)
|
| 114 |
+
uploaded_urls = []
|
| 115 |
+
for image in image_urls:
|
| 116 |
+
uploaded_url = await upload_to_imagebb(image["url"], imagebb_api_key)
|
| 117 |
+
if uploaded_url:
|
| 118 |
+
uploaded_urls.append(uploaded_url)
|
| 119 |
+
return uploaded_urls
|
| 120 |
+
|
| 121 |
+
# Usage example
|
| 122 |
+
auth_cookie = requests.get(gist_url).text
|
| 123 |
+
imagebb_api_key = "769cca89ac6111406b57de8a4fc1e613"
|
| 124 |
+
prompt = "A cute dog"
|
| 125 |
+
|
| 126 |
+
uploaded_urls = asyncio.run(main(prompt, auth_cookie, imagebb_api_key))
|
| 127 |
+
return {"images": uploaded_urls}
|