Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me with my first code
#1
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 Big Grin Big Grin Wall


I assume this is a very basic thing to code, but please help me do it Heart
Reply
#2
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
>>> 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020