-
Notifications
You must be signed in to change notification settings - Fork 89
better results with "https://cn.bing.com" #31
Comments
in fact, sometimes .... the redirect_url, returned by the first post, can be an absolute or relative one. |
@acheong08 ... using just httpx, it could be written as : import httpx,asyncio,re,json
HEADERS= {} # doesn't seem to be needed (?!)
COOKIES={}
for cookie in json.loads(open('bing.json', encoding="utf-8").read()):
COOKIES[ cookie.get("name") ] = cookie.get("value")
async def get_images(prompt:str) -> list:
async with httpx.AsyncClient(base_url="https://cn.bing.com", headers=HEADERS, cookies=COOKIES) as bing:
r = await bing.post("/images/create",data={"q":prompt,"qs":"ds"},params={"q":prompt,"rt":4,"FORM":"GENCRE"})
assert r.status_code==302, f"not a redirect: http/{r.status_code} ?!"
request_id = r.headers.get("location","").split("id=")[-1]
assert request_id.isalnum(),f"the redirect location doesn't contain 'id' {r.headers.get('location')}?!"
await asyncio.sleep(5) # seems it takes always more than 5sec
for nb_retry in range(30):
await asyncio.sleep(1)
r=await bing.get(f"/images/create/async/results/{request_id}",params={"q":prompt})
assert r.status_code==200,f"the polling url returns http/{r.status_code} ?!"
if image_links := re.findall(r'src="([^"]+)"', r.text):
return [ x.split("?")[0] for x in image_links ]
return [] # 30 poll calls, and nothing ;-(
ll=asyncio.run( get_images("cat with a ball") )
print( ll ) I'm from France ... but I need to post to "cn.bing.com" to have a good redirection (which contains an id). It's same behaviour with your code ... perhaps you know why ?! |
This is extremely weird. I've never had this issue despite being in China (using a VPN). The fact that it tries to redirect you to China suggests that the server sees a Chinese IP rather than French |
You ran this project on a Chinese machine, or your |
no ... it's a hosted linux/vm on a french hosting service ( https://www.alwaysdata.com/ )
no, it's clearly france |
Sorry. I can't figure it out yet. 🤔 |
@manatlan this is the code of my bing image generator ai- to run this i ahve to tun this command - python -m BingImageCreator -U <YOUR_TOKEN> --prompt "YOUR_IMAGE_PROMPT" --output-dir "OUTPUT_PATH" now, i want to run it just by giving prompt like a chatbot to generate images code - import argparse import httpx if os.environ.get("BING_URL") == None: Generate random IP between range 13.104.0.0/14FORWARDED_IP = ( Error messageserror_timeout = "Your request has timed out." sending_message = "Sending request..." def debug(debug_file, text_var): class ImageGen:
class ImageGenAsync:
async def async_image_gen( def main():
if name == "main": Sir, @manatlan , could you please tell me how I can simplify it? For example, by inputting a prompt in the chatbot along with predefined token values and specifying the output path |
@UseLEss213 ... |
"Actually, sir @manatlan , I am running the BingImageGenerator in Python using Visual Studio Code, and it is working without any issues. However, the problem is that it requires running the module command like this: 'python -m BingImageCreator -U <YOUR_TOKEN> --prompt "YOUR_IMAGE_PROMPT" --output-dir "OUTPUT_IMAGE_PATH"'. Now, I want to define the output path and token directly in the code, so I don't have to write them in the terminal. Essentially, I would like to create a chatbot in the terminal where I can provide just the prompt, and it will generate and save the images accordingly." |
@UseLEss213 ... ok, you are not developper ! The best way, for you ... without python modifications.
(replace "U8HDHDS88782838972398" by your token .... and "YOUR_FOLDER" by your destination folder) thus, in console, you'll be be able to call it like that:
|
Sir @manatlan , I just want to retrieve the final image URL. I don't want to save the image itself, but instead, I need a simple code where I can input my token. Based on my prompt, it should generate images and provide me with the corresponding image URL. |
@UseLEss213 image_generator = ImageGen(...)
print(image_generator.get_images(prompt)) |
Actually, @acheong08 , sir, can you please provide me with the entire short code that retrieves the final image URL from the generated images on the Bing site? In the code, the "u" token is already included; we just need to provide the prompt in the terminal. sorry to bother, but i am unable to understand the code by myself. |
Just don't call |
In some cases, BIC doesn't work (http connexion errors). (in threads of gunicorn/h11 workers)
I dive into the code, and reach to fix it ... just by changing the global var "BING_URL" from "https://www.bing.com" to "https://cn.bing.com" ... (in that cases, when using the original one, the first POST couldn't return the id, and breaks the following .... with the 'cn' version : it worked ootb) ... perhaps It could help someone.
BTW, you should concentrate your effort on the async one .... and make the sync one based on using the async one. (currently you have the 2 methods to maintain, when api/http changes)
BTW2: relations between edgegpt & bingimagecreator are confusing. For my needs, I've made a class, which use both, and expose functions from both in one instance. It's the way to go. And since, "creative mode" can return images too, it has a lot of sense ! Because it could replace the BIC apis. Because in chat mode, it's a lot easier to make multiple requests to make images, with context keeped. (the are no context in BIC)
The text was updated successfully, but these errors were encountered: