projects icon indicating copy to clipboard operation
projects copied to clipboard

Project Tutorial Proposal - Dev-Docs(Andrew Van Beek)

Open avb-is-me opened this issue 2 years ago • 5 comments

  1. Person Detection - Beginner

avb-is-me avatar Nov 02 '22 17:11 avb-is-me

Will be short tutorial on how to use hugging face and its API to train a very simple classifier. In my example I just detect whether it is the creator of Super Mario vs Minecraft.

You need to create a .env file or replace API-KEY value or replace it in the python code manually.

You can find your API Token here: https://huggingface.co/settings/tokens.

Then just google image search Miyamoto or Perrson and have fun!

index.html

<html>
    <head>
    </head>
    <body>
        <h1>Is it Miyamoto or Perrson?!?</h1>
        <form method="post" action="{{ url_for('upload') }}" enctype="multipart/form-data">
            <input type="file" name="file1">
            <input type="submit" value="Submit">
        </form>
    </body>
</html>

web.py

from flask import Flask, render_template, request, jsonify
import requests
import json
import os
from dotenv import load_dotenv
load_dotenv()

API_URL = "https://api-inference.huggingface.co/models/andrewvanbeek/autotrain-persson-versus-miyamoto-1990766287"
headers = {"Authorization": f'Bearer {os.getenv("HUGGING_FACE_API_KEY")}'}

app = Flask(__name__)

def query(data):
    response = requests.request("POST", API_URL, headers=headers, data=data)
    return json.loads(response.content.decode("utf-8"))

@app.route('/')
def index():
    return render_template('./index.html')

@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file1']
    modeldata = query(file)
    print(modeldata)
    if modeldata[0]:
        for person in modeldata:
            if person['score'] > 0.60:
                return f'It is probbaly {person["label"]}! The model result was {person["score"]}'
            else: 
                return 'Likely it is neither of those folks'
    else:
        return jsonify(modeldata)


app.run(host='0.0.0.0', port=81)

avb-is-me avatar Nov 05 '22 04:11 avb-is-me

Hey @avb-is-me. Amazing code!

Just a few changes:

  • Please follow our formatting guidelines
  • Python indentations are two spaces, add a new line between import dotenv and load_dotenv()
  • Everything should be single quotes (except the "HUGGING_FACE_API_KEY)
  • Let's call the main file main.py instead of web.py
  • "probably" not "probbaly"
  • No need to print in the python code (since the result is on HTML)
  • Would you mind doing a Silicon Valley joke: “Hotdog vs Not Hogdog” (instead of Miyamoto or Person)

Please let me know once you've made the changes!

Bobliuuu avatar Nov 14 '22 06:11 Bobliuuu

Sounds good @Bobliuuu. After I make those changes should I just start writing or do you want to review again?

avb-is-me avatar Nov 14 '22 20:11 avb-is-me

You can start writing once you make the changes! Please use the project template to write your project tutorial!

Bobliuuu avatar Nov 14 '22 20:11 Bobliuuu

👍

avb-is-me avatar Nov 14 '22 20:11 avb-is-me