Help me with my first code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help me with my first code (/thread-32252.html) |
Help me with my first code - ivo_georgiev - Jan-30-2021 Hello everybody, please do not laugh at me, because I have absolutely 0 knowledge of programming. I did this project, where you train a machine learning to responds to what you say to it. Here is the python code I got : from PIL import Image import requests def classify(text): key = "#my credits activation key" url = "https://machinelearningforkids.co.uk/api/scratch/"+ key + "/classify" response = requests.get(url, params={ "data" : text }) if response.ok: responseData = response.json() topMatch = responseData[0] return topMatch else: response.raise_for_status() input = input("What do you want to tell me? > ") recognized = classify(input) label = recognized["class_name"] if label == "kind_things": print ("Mmm, thanks") img = Image.open("happy.png") img.show() else: print ("Well fuck you too!") img = Image.open("sad.png") img.show()This way it works, so I assume I have everything properly installed (pip, pillow etc) Now I wanted to take this one step further and make an executable file out of it (with pyinstaller, which I did) and send it to my friends as a joke. The idea is that they write something nice, they get the nice response and a picture of me smiling is opened from an url. If they say mean things, then they get the mean response and an angry picture of me opens. This is what I have tried to far: if label == "kind_things": print ("Mmm, thanks") url = "#url of the picture" response = requests.get(url) img = Image.open(response.raw) img.show() else: print ("Well fuck you too!") url = "#url of the other picture" response = requests.get(url) img = Image.open(response.raw) img.show()It doesn't work. I read about stuff like "urllib" or "bytesIO" or "StringIO" but it only made a huge chaos in my head and I have been trying to get this to work for the last 5 hours I assume this is a very basic thing to code, but please help me do it RE: Help me with my first code - buran - Jan-30-2021 Not an answer to your question, but a note about input = input("What do you want to tell me? > ")Don't use input as name (i.e. on the left-hand side) - you overwrite the input() function>>> input = input('enter something:') enter something:spam >>> eggs = input('now something else:') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object is not callable >>> |