Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
avb-is-me opened this issue Nov 2, 2022 · 5 comments
Open

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

avb-is-me opened this issue Nov 2, 2022 · 5 comments

Comments

@avb-is-me
Copy link
Contributor

avb-is-me commented Nov 2, 2022

  1. Person Detection - Beginner
@avb-is-me
Copy link
Contributor Author

avb-is-me commented Nov 5, 2022

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)

@Bobliuuu
Copy link
Contributor

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!

@avb-is-me
Copy link
Contributor Author

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

@Bobliuuu
Copy link
Contributor

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

@avb-is-me
Copy link
Contributor Author

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants