dsdanielpark / Bard-API

The unofficial python package that returns response of Google Bard through cookie value.

Home Page:https://pypi.org/project/bardapi/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Image generation

raymerjacque opened this issue · comments

I want to ask if image generation is now supported in bardapi ?

Google bard is now capable of generating images, has this been added to Bardapi and can you point me to the code that handles images so i can adjust my script please ?

commented

I'm currently working through it but it seems like the images are returned normally, and they start with
https://lh3.googleusercontent.com/bard/
To access the content however you need to pass your account.google.com cookies, or you get 403 forbidden errors.

A working prompt to trigger them is generate an image of a cat etc.

Thank you, however i am a little lost here. can you point me to the code i need to add or edit in my fastapi script ?

for example here is my fastapi script :


@app.post("/api/ask", response_model=ResponseModel)
def get_response(user_input: UserInput):
    try:
    
        # Load .env file for each request and overwrite existing variables
        load_dotenv(override=True)

        # Get BARD_API_KEY from the .env file
        BARD_API_KEY = os.environ.get('BARD_API_KEY')

        # Set up the session for each request
        session = requests.Session()
        session.headers = {
            "Host": "gemini.google.com",
            "X-Same-Domain": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            "Origin": "https://gemini.google.com",
            "Referer": "https://gemini.google.com/",
        }
        session.cookies.set("__Secure-1PSID", BARD_API_KEY)

        bard = Bard(token=BARD_API_KEY, session=session, timeout=30)
            
        # Print the user input to the console
        print(f"Received user input: {user_input.user_input}")    
        response = process_input(user_input.user_input, bard)
        # Save response to a temporary file
        temp_file_path = save_response_to_file(response)
        # Return the path of the temporary file instead of the response content
        return {"content": temp_file_path}        
        
        print(f"Response saved to: {temp_file_path}")  # For debugging
        return response
    except Exception as e:
        # Log the error details for debugging
        print(f"Error: {e}")
        # You might want to return a more generic error message in a production environment
        raise HTTPException(status_code=400, detail=f"Error processing request: {e}")
        
def save_response_to_file(response):
    # Define the desired directory path
    directory_path = '/www/html/temp/'

    # Ensure the directory exists, if not, create it
    os.makedirs(directory_path, exist_ok=True)

    # Create a unique file name. You can also use other methods to generate unique names
    file_name = f"response_{int(time.time())}.json"
    file_path = os.path.join(directory_path, file_name)

    # Write the response to the file
    with open(file_path, 'w') as file:
        json.dump(response, file)

    return file_path      

def process_input(user_input, bard):
    response = bard.get_answer(user_input)
    result = {
        'content': response['content'],
        'images': list(response['images']) if 'images' in response and isinstance(response['images'], set) else response.get('images', []),
        'links': list(response['links']) if 'links' in response and isinstance(response['links'], set) else response.get('links', [])
    }

    return result

I am unsure what code to change or edit, it does not generate images, if i ask it to generate an image it simply tells me it cant. normal text generations works fine, urls work fine as well. if i ask it to show me images it does, it sends links. but it doesnt generate images.

any assistance is welcomed, thank you.

Any progress on image generation ?

[NOTICE] Please, go to Gemini-API https://github.com/dsdanielpark/Gemini-API

Gemini Icon Google - Gemini API

A unofficial Python wrapper, python-gemini-api, operates through reverse-engineering, utilizing cookie values to interact with Google Gemini for users struggling with frequent authentication problems or unable to authenticate via Google Authentication.

Collaborated competently with Antonio Cheong.

What is Gemini?

[Paper] [Official Website] [Official API] [API Documents]

Gemini is a family of generative AI models developed by Google DeepMind that is designed for multimodal use cases. The Gemini API gives you access to the Gemini Pro and Gemini Pro Vision models. In February 2024, Google's Bard service was changed to Gemini.


Installation

pip install python-gemini-api
pip install git+https://github.com/dsdanielpark/Gemini-API.git

For the updated version, use as follows:

pip install -q -U python-gemini-api