Bard-API icon indicating copy to clipboard operation
Bard-API copied to clipboard

Provide Links For Images Generated by Google's Bard AI

Open GordonLevy opened this issue 1 year ago • 11 comments

Hello,

It seems BardAI now supports image generation as per below snapshot. However the Bard-API only returns the alt-image-text for the generated image. Is there a way to include the links of the generated images?

GordonLevy avatar May 23 '23 22:05 GordonLevy

Thank you for bringing up this interesting issue. While there is a high possibility that it may not work, I will investigate it further.

dsdanielpark avatar May 24 '23 04:05 dsdanielpark

Would really like it to have the links of generated images, maybe put a network interceptor to catch hold of these links - based on domain/sub-domain name maybe?

Edit : Found some libs like - mitmproxy (Man-in-the-Middle Proxy) that helps to intercept additional requests that the leading API req makes So prob catch hold of t0.gstatic.com/images URI and acc append the response to the BARD API

GordonLevy avatar May 24 '23 05:05 GordonLevy

bard_links

Bard is a language model, not an image generation AI model. However, I have recently confirmed the addition of the image web search feature to Google Bard. As a temporary solution, it returns the links included in the response. Please note that this feature is temporary and is only deployed in the developer version. Please use it as follows.

pip install git+https://github.com/dsdanielpark/Bard-API.git
from bardapi import Bard
bard = Bard(token='xxxxxxxxxxx')
res = bard.get_answer("Find me an image of the main entrance of Stanford University.")
res['links']

dsdanielpark avatar May 24 '23 06:05 dsdanielpark

Yes, from the images response list - it looks the the uri for generated image is : t0.gstatic.com/images which could be appended in the response string.

Any timelines around which this feature is deployed to prod?

GordonLevy avatar May 24 '23 10:05 GordonLevy

It is not desirable to continuously add specific functionalities to the developer deployment version. Therefore, we recommend post-processing and using the following approach instead.

token='xxxxxxxx'
bard = Bard(token=token)
link_list = bard.get_answer("Find me an image of the main entrance of Stanford University.")['links']
filtered_links = [link for link in link_list if "https://lh3.googleusercontent.com/" in link or "http://t0.gstatic.com/" in link]

print(filtered_links)

dsdanielpark avatar May 24 '23 14:05 dsdanielpark

Thanks for your detailed response, just have one final query : Any timelines around which this feature is deployed to prod?

GordonLevy avatar May 24 '23 20:05 GordonLevy

Please check this commit hash acbf3fba8453ea354a0b53e59230175a44500171

dsdanielpark avatar May 25 '23 05:05 dsdanielpark

I just added support in my repo https://github.com/acheong08/Bard/compare/1.0.3...1.1.0

You can parse for images

if len(json_chat_data) >= 3:
            for img in json_chat_data[4][0][4]:
                images.add(img[0][0][0])

acheong08 avatar May 25 '23 06:05 acheong08

acheong08

images = set()
        if len(json_chat_data) >= 3:
            if len(json_chat_data[4][0]) >= 3:
                for img in json_chat_data[4][0][4]:
                    images.add(img[0][0][0])

Could you kindly contribute by including this part and the part where "images" is the key?

dsdanielpark avatar May 25 '23 06:05 dsdanielpark

chat_data = json.loads(resp.content.splitlines()[3])[0][2]
if not chat_data:
    return {"content": f"Google Bard encountered an error: {resp.content}."}
json_chat_data = json.loads(chat_data)
images = set()
if len(json_chat_data) >= 3:
    if len(json_chat_data[4][0]) >= 4:
        for img in json_chat_data[4][0][4]:
            images.add(img[0][0][0])
results = {
    "content": json_chat_data[0][0],
    "conversation_id": json_chat_data[1][0],
    "response_id": json_chat_data[1][1],
    "factualityQueries": json_chat_data[3],
    "textQuery": json_chat_data[2][0] if json_chat_data[2] is not None else "",
    "choices": [{"id": i[0], "content": i[1]} for i in json_chat_data[4]],
    "images": images,
}

acheong08 avatar May 25 '23 07:05 acheong08

acheong08

Thank you, then I will modify and commit.

dsdanielpark avatar May 25 '23 07:05 dsdanielpark

Please check this commit hash acbf3fba8453ea354a0b53e59230175a44500171

Was looking to have this feature in the official pip package - 'bardapi'

GordonLevy avatar May 25 '23 20:05 GordonLevy

GordonLevy

Use developer version or you can install via github tag. Refere this CoCoder package. https://github.com/dsdanielpark/Co-Coder/blob/main/requirements.txt

dsdanielpark avatar May 25 '23 21:05 dsdanielpark

@dsdanielpark based on your code, I have converted it to PHP and support getting images Bard AI PHP version

CatAnonymous avatar Jun 04 '23 18:06 CatAnonymous

bard_links

Bard is a language model, not an image generation AI model. However, I have recently confirmed the addition of the image web search feature to Google Bard. As a temporary solution, it returns the links included in the response. Please note that this feature is temporary and is only deployed in the developer version. Please use it as follows.

pip install git+https://github.com/dsdanielpark/Bard-API.git
from bardapi import Bard
bard = Bard(token='xxxxxxxxxxx')
res = bard.get_answer("Find me an image of the main entrance of Stanford University.")
res['links']

Hi Daniel, this works in Google colab and gives the links, but doesn't work on Jupyter notebook; why so?

MohitSM18 avatar Jun 15 '23 08:06 MohitSM18

MohitSM18

There could be various reasons for the issue. If it continues to not work on your local Jupyter Notebook, please feel free to create a new issue. Thank you.

dsdanielpark avatar Jan 18 '24 15:01 dsdanielpark