gpt4free icon indicating copy to clipboard operation
gpt4free copied to clipboard

ImportError: cannot import name 'forefront' from partially initialized module 'gpt4free'

Open davidhwilliams opened this issue 1 year ago • 13 comments

After moving streamlit_app.py to the root directory and running streamlit run streamlit_app.py, I was hit with the issue:

ImportError: cannot import name 'forefront' from partially initialized module 'gpt4free' (most likely due to a circular import) (C:\Documents\GitHub\gpt4free\gpt4free\__init__.py)

I resolved this by changing the 6 from imports on gpt4free/__init__.py to the singular import gpt4free and qualifying each imported class.

For example usesless.Completion.create() -> gpt4free.usesless.Completion.create()

davidhwilliams avatar Apr 30 '23 19:04 davidhwilliams

I have the same problem.... Thanks for sharing it. I think however that we should find a better solution

zioalex avatar Apr 30 '23 20:04 zioalex

After moving streamlit_app.py to the root directory and running streamlit run streamlit_app.py, I was hit with the issue:

ImportError: cannot import name 'forefront' from partially initialized module 'gpt4free' (most likely due to a circular import) (C:\Documents\GitHub\gpt4free\gpt4free\__init__.py)

I resolved this by changing the 6 from imports on gpt4free/__init__.py to the singular import gpt4free and qualifying each imported class.

For example usesless.Completion.create() -> gpt4free.usesless.Completion.create()

It works, but in result after asking question - "Unable to fetch the response, Please try again." Perhaps this is an unrelated issue

mdlufy avatar Apr 30 '23 21:04 mdlufy

After moving streamlit_app.py to the root directory and running streamlit run streamlit_app.py, I was hit with the issue:

I face this issue event when having streamlit_app.py file in gui directory. Also seems every such import across the project breaks it

davidarny avatar Apr 30 '23 21:04 davidarny

I solved it creating a venv outside from the gpt4free repo and reinstalling all the dependencies. You need to copy the streamlit_app.py to the root repo folder. Then it works.

zioalex avatar Apr 30 '23 21:04 zioalex

Use a docker container when you are attempting to try something new you don't understand completely. Install docker and docker-compose if they are not installed, and run docker-compose up --build -d

valerii-chirkov avatar May 01 '23 00:05 valerii-chirkov

When I try to run it on a cloud instance, it gives me "Unable to fetch the response, Please try again."

How do you solve this?

Ace167 avatar May 01 '23 09:05 Ace167

@davidhwilliams Is the issue still happening? I'm unable to reproduce it. Could you please verify if it's still occurring and provide the precise instructions for reproducing it if it is?

sudouser777 avatar May 01 '23 11:05 sudouser777

When I try to run it on a cloud instance, it gives me "Unable to fetch the response, Please try again."

How do you solve this?

@Ace167 This error happens when it's not able to get the response, we can't do anything about it

sudouser777 avatar May 01 '23 11:05 sudouser777

import argparse
import os
import pickle
from gpt4free import forefront

# Define the command line arguments
parser = argparse.ArgumentParser(description='Run GPT-4 model')
parser.add_argument('--prompt', type=str, help='The prompt to generate text from', default='hello world, tell us s
parser.add_argument('--model', type=str, help='The model to use', default='gpt-4')
parser.add_argument('--logging', type=bool, help='Whether to log the request', default=False)
parser.add_argument('--token-file', type=str, help='The file to store the token in', default='gpt4free_token.pkl')
parser.add_argument('--token', type=str, help='The API token to use (overrides --token-file)', default=None)
parser.add_argument('--v', type=str, help='Verbose', default=False)

# Parse the command line arguments
args = parser.parse_args()

v = args.v
if v:
    print("Running with args:")
    print(args)
# Check if the token file exists
if os.path.exists(args.token_file):
    # Load the token from the file
    if v:
        print(f'Token_file load:')
    with open(args.token_file, 'rb') as f:
        token = pickle.load(f)
        if v:
            print(f'Token at {args.token_file} loaded')
elif not args.token:
    if v:
        print(f'Create account:')
    # Create a new account and save the token to the file
    token = forefront.Account.create(logging=args.logging)
    with open(args.token_file, 'wb') as f:
        pickle.dump(token, f)
        if v:
            print(f'Token saved to: {args.token_file}')
    if v:
        print(f'Token:')

# If a token was provided on the command line, use it instead of the one from the file
if args.token:
    if v:
        print(f'Token from cli:')
    token = args.token

if v:
    print(token)

# Get a response
for response in forefront.StreamingCompletion.create(token=token, prompt=args.prompt, model=args.model):
    print(response.completion.choices[0].text, end='')
print("")

Currently output is EMPTY *here is with --v=1

Running with args: Namespace(logging=False, model='gpt-4', prompt='hello world, tell us something about fish', token=None, token_file='gpt4free_token.pkl', v='1') Token_file load: Token at gpt4free_token.pkl loaded ey_token_gibberish_Bg

But 5 minutes ago it was

Traceback (most recent call last): File "main.py", line 53, in print(response.completion.choices[0].text, end='') AttributeError: 'ForeFrontResponse' object has no attribute 'completion'

spacewalkingninja avatar May 01 '23 11:05 spacewalkingninja

@spacewalkingninja response object updated, use this print(response.text, end='')

sudouser777 avatar May 01 '23 11:05 sudouser777

@spacewalkingninja response object updated, use this print(response.text, end='')

It worked once while I was testing 5 mins ago, without your addition:

if v:
    print(token)

# Get a response
for response in forefront.StreamingCompletion.create(token=token, prompt=args.prompt, model=args.model):
    print(response.completion.choices[0].text, end='')
    print("")

for response in forefront.StreamingCompletion.create(
        token=token,
        prompt=args.prompt,
        model=args.model
):

for response in forefront.StreamingCompletion.create(
	token=token,
	prompt='hello world',
	model='gpt-4'
):
    print(response.choices[0].text, end='')
File main.py saved

Had this Out:

Running with args: Namespace(logging=False, model='gpt-4', prompt='hello world, tell us something about fish', token=None, token_file='gpt4free_token.pkl', v='1') Create account: Token saved to: gpt4free_token.pkl Token: N0pbePE4SQGeRA Fish are a diverse group of aquatic animals that belong to the phylum Chordata and the superclass Pisces. They can be found in various habitats, including oceans, rivers, lakes, and ponds. There are over 33,000 known species of fish, which can be broadly classified into three main groups: jawless fish (Agnatha), cartilaginous fish (Chondrichthyes), and bony fish (Osteichthyes).

  1. Jawless fish (Agnatha): This group includes lampreys and hagfish. They lack jaws and paired fins, and have a cartilaginous skeleton.

Then it just randomly stopped working, I don't get why

spacewalkingninja avatar May 01 '23 11:05 spacewalkingninja

Good news: For anybody who wants this, I am getting output with it:


import argparse
import os
import pickle
from gpt4free import forefront

# Define the command line arguments
parser = argparse.ArgumentParser(description='Run GPT-4 model')
parser.add_argument('--prompt', type=str, help='The prompt to generate text from', default='hello world, tell us something about fish')
parser.add_argument('--model', type=str, help='The model to use', default='gpt-4')
parser.add_argument('--logging', type=bool, help='Whether to log the request', default=False)
parser.add_argument('--token-file', type=str, help='The file to store the token in', default='gpt4free_token.pkl')
parser.add_argument('--token', type=str, help='The API token to use (overrides --token-file)', default=None)
parser.add_argument('--v', type=str, help='Verbose', default=False)
parser.add_argument('--force_new_user', type=bool, help='Force new user token creation', default=False)
# Parse the command line arguments
args = parser.parse_args()

v = args.v
the_force = args.force_new_user
if v:
    print("Running with args:")
    print(args)

if the_force:
    if v:
        print(f'Create account:')
    # Create a new account and save the token to the file
    token = forefront.Account.create(logging=args.logging)
    with open(args.token_file, 'wb') as f:
        pickle.dump(token, f)
        if v:
            print(f'Token saved to: {args.token_file}')
    if v:
        print(f'Token:')
# Check if the token file exists
if os.path.exists(args.token_file) and not the_force:
    # Load the token from the file
    if v:
        print(f'Token_file load:')
    with open(args.token_file, 'rb') as f:
        token = pickle.load(f)
        if v:
            print(f'Token at {args.token_file} loaded')
elif not args.token and not the_force:
    if v:
        print(f'Create account:')
    # Create a new account and save the token to the file
    token = forefront.Account.create(logging=args.logging)
    with open(args.token_file, 'wb') as f:
        pickle.dump(token, f)
        if v:
            print(f'Token saved to: {args.token_file}')
    if v:
        print(f'Token:')

# If a token was provided on the command line, use it instead of the one from the file
if args.token:
    if v:
        print(f'Token from cli:')
    token = args.token

if v:
    print(token)

# Get a response
for response in forefront.StreamingCompletion.create(
        token=token,
        prompt=args.prompt,
        model=args.model
):
    print(response.text, end='')
)

spacewalkingninja avatar May 01 '23 11:05 spacewalkingninja

I am getting consistent outputs with the force of --force=1 creating new accounts, if i store the token, it dies after first output, conisstently

spacewalkingninja avatar May 01 '23 11:05 spacewalkingninja