HF Space Basic Guide
Making a Huggingface space using just your Android mobile¶
First we will see how to create and run a HuggingFace Space then we will proceed for mobile steps!¶
- The title might be a little lucrative because it is, I am one of those billions of zombies on this planet who have their palm-held device, LOL, mobile in their hand, every awake second. I am also going through the fast dot ai Practical Deep Learning Course by Jeremy Howard, which by the way is pretty cool, and dates only 2022, well the latest version does, so you should give it a try too- and Jeremy encourages students to do, not just learn, so I decided to give it a shot, like why not. Problem is, I dont sit with my laptop all the time, but I do have my mobile at hand all the time, and I had just learned how to build a working HuggingFace public space to greet someone when they entered their name, I am going to reproduce the steps below for your benefit.¶
- Sign up for HuggingFace, a website for the community of machine learners(its way beyond that now, obviously!)
In [2]:
#step 2- install joblib and run a simple code to greet someone with name inserted
pip install joblib
Collecting joblib Downloading joblib-1.4.2-py3-none-any.whl.metadata (5.4 kB) Downloading joblib-1.4.2-py3-none-any.whl (301 kB) Installing collected packages: joblib Successfully installed joblib-1.4.2 Note: you may need to restart the kernel to use updated packages.
In [4]:
import joblib
# Simple function to greet by name
def greet(name):
return f"Hello, {name}!"
# Save the function as a model
joblib.dump(greet, 'greet_model.pkl')
Out[4]:
['greet_model.pkl']
In [5]:
# Load the saved model
greet_model = joblib.load('greet_model.pkl')
# Test it
print(greet_model('Avatansh'))
Hello, Avatansh!
Next, since we will be using HugginggFace spaces, to basically host our model, we are going to have to use a Space SDK and we will be using Gradio as did Jeremy, like so -
¶
In [ ]:
# so again we install gradio and test the interface locally first
!pip install gradio
In [8]:
import gradio as gr
import joblib
# Load the greet model
greet_model = joblib.load('greet_model.pkl')
# Gradio interface for greeting
def greet_user(name):
return greet_model(name)
# Launch Gradio app
demo = gr.Interface(fn=greet_user, inputs="text", outputs="text")
demo.launch()
* Running on local URL: http://127.0.0.1:7860 To create a public link, set `share=True` in `launch()`.
Out[8]:
Success, we see that the gradio app is running locally. Now all that remains is to upload the model file and code file to files tab in HuggingFace space. Take special care that the code above is in 1 file, that is named app.py and also upload a requirement.txt file, which will let HuggingFace know which libraries are you using! so all in all, three files, app.py, joblib and requirements.txt¶
- and the requirements.txt file should just have the words of the required libraries, screenshot attached
Now once you have uploaded the files, you just need to come back to app tab on your space, where it will be running the Logs and a building sign will be displayed in background, once you close the logs, it might take some time, but the app will run and result will look something like this -¶
Now for the mobile part-¶
- All you need are the ChatGPT app and Pydroid and mobile browser, in my case Google Chrome-
- First I generated a code for running a basic salary prediction model using mock data in chatgpt, then ran it on colab, here is the notebook link - https://colab.research.google.com/drive/12a8ccDSdeLktI9rjy-XAnho3xORB3Dtq?usp=sharing
- As you can see the model has run and been saved in colab directory also, the gradio interface is up and running-
,
- Now all you need to do is combine all the code in one single file name app.py, simply copy paste from colab into Pydroid app, but dont run, just save it.
- Since Pydroid app allows us to modify the extensions of the files, we can also write our "Requirements.txt" file in it only, in this case we will need gradio, scikit-learn, joblib
- The link to up and running HuggingFace Space described in this cell - https://huggingface.co/spaces/AvtnshM/Salary_Pred_Basic
- All this, I did on my Android Mobile and also built a third app today, dated 30th October 2024, for Career Counselling Indian students after standard 12t, that is, after 12 years of schooling- be sure to check it out too on my profile - https://huggingface.co/AvtnshM
Disclaimer- All the code is generated by CGPT, I am merely the human supervisor, acting on Jeremy's advice to just do it myself, I can understand most part of the code, having done 3 courses till now, so Id recommend to do it first, get CGPT to breakdown the code and understand it for yourself! Its not that hard, Actually not at all hard(maybe talking to myself!)¶
See in the next post¶
In [ ]: