0

I have come across a very strange issue, I am using miniconda to test out the GPT-NEO ai text generator, I would call it like this in the command prompt:

C:\tools\miniconda3\python C:\Users\Graham\Desktop\Files\programming\Languages\Python\gpt_neo_app\ai.py

Python Code:

from os import system
from transformers import pipeline
import json

try:

    # generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B', device=-1)
    generator = pipeline('text-generation', model='EleutherAI/gpt-neo-1.3B', device=-1)
    outjson = generator("unlock your hip flexors", do_sample=True, max_length=500, temperature=1.9)
    outtext = json.loads(json.dumps(outjson[0]))["generated_text"]
    # with open(r"C:\Users\Graham\Desktop\Files\programming\Languages\Python\gpttext.txt", "w") as f:
    with open("gpttext.txt", "w") as f:
        f.write(outtext)
    print(outtext)

except Exception:
    pass

The part that fails is writing to the .txt file, no matter what i do (even commenting out the text generation code and just putting in a random string to be written) the .txt file is never created or written to.

The text generator is working fine, i even tried the full path to the .txt file this still never worked, such a basic issue i cannot seem to see the problem, is there anything i have missed or have done wrong? it seems fairly straight forward enough but it just will not be created.

Any help would be appreciated.

graham23s
  • 385
  • 3
  • 14
  • 1
    This is a perfect example of why catching `Exception` is not a great idea :-) Take out the `try` and `except` and run it again and see if you can now see a problem. – JonSG May 25 '21 at 17:21
  • Hello Jon, there was an indent issue after taking out the try / catch, I fixed that, now there is no error shown just no .txt file being created. this is a strange one for me lol – graham23s May 25 '21 at 17:41
  • Are you allowed to write to that path? It shouldn't fail silently if you can't write. Can you read files? How about writing files to other locations? – Pranav Hosangadi May 25 '21 at 20:09
  • Believe it or not the issue was because i had to put the FULL path to the .txt file, other wise it was a silent error which is strange, the first time i have had to do the full path. If it had shown me that error i would have known the issue! thank you guys. – graham23s May 25 '21 at 20:25
  • 2
    @graham23s presumably it was writing to a different directory than you were expecting, see https://stackoverflow.com/q/5137497/1358308 for getting the "current working directory" in python – Sam Mason May 25 '21 at 20:30
  • 1
    your use of `json` seems less than useful, why not just use `f.write(outjson[0]["generated_text"])` – Sam Mason May 25 '21 at 20:33
  • Thank you guys, noted both suggestions! – graham23s May 25 '21 at 21:29

1 Answers1

0

Just posting incase anyone else has this issue, I needed to put the full path to the .txt file.

graham23s
  • 385
  • 3
  • 14