xtekky / gpt4free

The official gpt4free repository | various collection of powerful language models
https://g4f.ai
GNU General Public License v3.0
60.98k stars 13.29k forks source link

AttributeError: 'BingCreateImages' #2181

Closed Ggre55 closed 1 month ago

Ggre55 commented 2 months ago

Bug description

  1. installed the package with 'pip install -U g4f[all]'
  2. started the server to test the working models with 'from g4f.gui import run_gui run_gui()'
  3. wrote simple code for further testing
    
    from g4f.client import Client

img_models= ['stable-diffusion-3', 'sdxl-lightning', 'playground-v2.5']

Execute with a specific provider

for modl in img_models: client = Client()

print(f'Trying=========={modl}')

  # response = client.chat.completions.create(
  #    model=modl,
  #    messages=[{"role": "user", "content": "hi, make cat with glasses and smily face in space dancing with cute man"}],
  # print(response.choices[0].message.content)
  # )/////this above works///

  print(f'BY=========={modl}')
  response = client.images.generate(
  model='sdxl',
  prompt=[{"role": "user", "content": "hi, make cat with glasses and smily face"}],
  )
  image_url = response.data[0].url

4. i get the following error 

c:/Users/user/Desktop/new/gpt4freee/gptt.py BY==========stable-diffusion-3 Traceback (most recent call last): File "c:\Users\user\Desktop\new\gpt4freee\gptt.py", line 58, in response = client.images.generate( ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\client\client.py", line 157, in generate response = list(provider.create(prompt)) ^^^^^^^^^^^^^^^ AttributeError: 'BingCreateImages' object has no attribute 'create'



**Environment**
- python version '3.12.5'
- requests, bs4, nodriver, undetected_chromedriver, 

**Additional context**

i did some diging and found the cause of the issue in this file, 
"C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\client\client.py"

in line 157 'response = list(provider.create(prompt))', the create methode is not defined for the BingCreateImages that is returned from this file wich also i think is missing something 

'C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\g4f\client\image_models.py' 

Anyways love this project so much and i am working on pr that will add few models and fix the phind provider is that ok
noneherel commented 2 months ago

i think they don't want to respond to anyone (: .

github-actions[bot] commented 1 month ago

Bumping this issue because it has been open for 7 days with no activity. Closing automatically in 7 days unless it becomes active again.

kqlio67 commented 1 month ago

Thank you for reporting this issue, @Ggre55. I've submitted a pull request that addresses this problem and improves the functionality of the image generation feature. While the PR is still pending review, I wanted to share a preview of the proposed changes.

The upcoming update should resolve the error you've experienced. Here's an example of how the improved image generation code would work:

from g4f.client import Client

img_models = ['sdxl', 'sd-3', 'playground-v2.5', 'flux', 'flux-realism', 'dalle', 'dalle-mini']

client = Client()

for model in img_models:
    try:
        print(f'Generating image with {model}')
        response = client.images.generate(
            model=model,
            prompt="hi, make cat with glasses and smily face in space dancing with cute man",
        )
        image_url = response.data[0].url
        print(f"Generated image URL: {image_url}")
    except Exception as e:
        print(f"Error with {model}: {str(e)}")
    print("------------------------")

This implementation supports multiple image models and includes error handling. When run, it produces the following results:

python Image.py 
Generating image with sdxl
Generated image URL: https://replicate.delivery/yhqm/5Z6gykXZjGYAG9PipdP1ynsuOISI6P0K8vqQhqQd0UlgnfsJA/out-0.png
------------------------
Generating image with sd-3
Generated image URL: https://replicate.delivery/yhqm/eEffgkOaU8VHWofX08RxPg9NLtKyz7JG6En7KZKp38TU45nNB/R8_SD3_00001_.webp
------------------------
Generating image with playground-v2.5
Generated image URL: https://replicate.delivery/yhqm/faIdfbVAGBsOepkoEXdxvV1yAt7fC26f4LmqIyD8TEISxzPbC/out-0.png
------------------------
Generating image with flux
Generated image URL: https://api.airforce/v1/imagine2?prompt=hi,+make+cat+with+glasses+and+smily+face+in+space+dancing+with+cute+man&size=1:1&model=flux
------------------------
Generating image with flux-realism
Generated image URL: https://api.airforce/v1/imagine2?prompt=hi,+make+cat+with+glasses+and+smily+face+in+space+dancing+with+cute+man&size=1:1&model=flux-realism
------------------------
Generating image with dalle
Generated image URL: generated_images/image_1725576605.png
------------------------
Generating image with dalle-mini
Generated image URL: generated_images/image_1725576664.png
------------------------

As shown, the code successfully generates images using various models, demonstrating improved functionality and reliability.

These enhancements should be available in a future release, pending review and approval by the project maintainers.

@noneherel, thank you for your patience. The project team is likely reviewing contributions to enhance the project.

Let's look forward to the upcoming updates that will improve the functionality of this great project!

TheFirstNoob commented 1 month ago

@kqlio67 i copy you code after update and we have problem with base64 decode EMI have same problem

Generating image with sdxl
C:\Users\TheFi\AppData\Local\Programs\Python\Python311\Lib\site-packages\curl_cffi\aio.py:39: RuntimeWarning:
    Proactor event loop does not implement add_reader family of methods required.
    Registering an additional selector thread for add_reader support.
    To avoid this warning use:
        asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())

  warnings.warn(PROACTOR_WARNING, RuntimeWarning)
Generated image URL: https://replicate.delivery/yhqm/EB4euKPImYzjFyiHzrcuApWg0NTeeD16M165xUFbeA31JIrNB/out-0.png
------------------------
Generating image with sd-3
C:\Users\TheFi\AppData\Local\Programs\Python\Python311\Lib\site-packages\curl_cffi\aio.py:205: UserWarning: Curlm alread closed! quitting from process_data
  warnings.warn("Curlm alread closed! quitting from process_data")
Generated image URL: https://replicate.delivery/yhqm/NHf3Sudt1a1VVSJ4ISeDu4riYw9g7C0jkQN5d8psexeEKIrNB/R8_SD3_00001_.webp
------------------------
Generating image with playground-v2.5
Generated image URL: https://replicate.delivery/yhqm/qz02OWmR8ma5CJ5cmSco0MKrhgMJBswgNHTeVwwH4OMTBZtJA/out-0.png
------------------------
Generating image with flux
Generated image URL: https://api.airforce/v1/imagine2?prompt=hi,+make+cat+with+glasses+and+smily+face+in+space+dancing+with+cute+man&size=1:1&model=flux
------------------------
Generating image with flux-realism
Generated image URL: https://api.airforce/v1/imagine2?prompt=hi,+make+cat+with+glasses+and+smily+face+in+space+dancing+with+cute+man&size=1:1&model=flux-realism
------------------------
Generating image with dalle
ERROR:root:Error in synchronous image generation: Invalid image format (from magic code).
Error with dalle: Invalid image format (from magic code).
------------------------
Generating image with dalle-mini
Invalid base64 data
ERROR:root:Error in synchronous image generation: Invalid data URI image.
Error with dalle-mini: Invalid data URI image.
------------------------
kqlio67 commented 1 month ago

Thank you for reporting this issue, @TheFirstNoob. I've checked and fixed the provider, and now the error is gone. Here's an example of the execution and the result:

from g4f.client import Client

img_models = ['sdxl', 'sd-3', 'playground-v2.5', 'flux', 'flux-realism', 'dalle', 'dalle-2', 'dalle-mini', 'emi']

client = Client()

for model in img_models:
    try:
        print(f'Generating image with {model}')
        response = client.images.generate(
            model=model,
            prompt="hi, make cat with glasses and smily face in space dancing with cute man",
        )
        image_url = response.data[0].url
        print(f"Generated image URL: {image_url}")
    except Exception as e:
        print(f"Error with {model}: {str(e)}")
    print("------------------------")

Execution result:

python test-issues-2181.py 
Generating image with sdxl
Generated image URL: https://replicate.delivery/yhqm/yc57p97xRn6WOp24xA3GfRfPYSQiOU76XvhUq77Kp8zD40bTA/out-0.png
------------------------
Generating image with sd-3
Generated image URL: https://replicate.delivery/yhqm/yibhbjpQzrL2FdyMfoonSFv62k5ePJfm6UFaCRzb1AeYgTvNB/R8_SD3_00001_.webp
------------------------
Generating image with playground-v2.5
Generated image URL: https://replicate.delivery/yhqm/S8rPefCdJXu0y0gxdjgNMDJQgTBfSKeqiEohwbt0e8XdCne2E/out-0.png
------------------------
Generating image with flux
Generated image URL: https://api.airforce/v1/imagine2?prompt=hi,+make+cat+with+glasses+and+smily+face+in+space+dancing+with+cute+man&size=1:1&model=flux
------------------------
Generating image with flux-realism
Generated image URL: https://api.airforce/v1/imagine2?prompt=hi,+make+cat+with+glasses+and+smily+face+in+space+dancing+with+cute+man&size=1:1&model=flux-realism
------------------------
Generating image with dalle
Generated image URL: https://iili.io/dUvt1Cx.png
------------------------
Generating image with dalle-2
Generated image URL: https://iili.io/dUvDeR9.webp
------------------------
Generating image with dalle-mini
Generated image URL: https://iili.io/dUvmt6v.webp
------------------------
Generating image with emi
Generated image URL: https://iili.io/dUvyueI.png
------------------------

As you can see, all models are now working without errors. I've submitted a pull request with these fixes: https://github.com/xtekky/gpt4free/pull/2220

This issue should be resolved in the next update once the pull request is merged. Thank you for your patience and for reporting this problem!

github-actions[bot] commented 1 month ago

Bumping this issue because it has been open for 7 days with no activity. Closing automatically in 7 days unless it becomes active again.

github-actions[bot] commented 1 month ago

Closing due to inactivity.