Developing a Smart Health Assistant with Hugging Face and React Native

Lakshitha Vimuth
4 min readMay 15, 2024

--

In today’s fast-paced world, health and wellness are more important than ever. Imagine having a personal health assistant that not only tracks your daily health metrics but also provides personalized recommendations to improve your overall well-being. This is the concept behind our Smart Health Assistant, a project that leverages AI and Natural Language Processing (NLP) models from Hugging Face, combined with the power of React Native and Django, to create a comprehensive health monitoring and recommendation system.

In this article, I will walk you through the development process of the Smart Health Assistant, detailing the technologies used, the key features of the application, and how you can set up and run this project yourself. You can also find the complete code and detailed instructions on my GitHub repository.

Project Overview

The Smart Health Assistant is designed to help users track their health metrics such as steps taken, sleep hours, and heart rate. It uses this data to provide personalized fitness and nutrition advice, mental health support, and real-time health monitoring. Here are some of the key features:

  • Personalized Health Recommendations: Get customized diet and exercise plans based on your health data.
  • Health Monitoring and Alerts: Track vital signs and receive alerts for irregular health metrics.
  • Virtual Health Coach: An AI chatbot that offers health tips, answers questions, and provides motivation.
  • Mental Health Support: Stress management techniques, mindfulness exercises, and access to mental health resources.
  • Community and Social Features: Share your health journey, participate in challenges, and support each other.

Tech Stack

To bring this project to life, we used a combination of modern technologies:

  • Backend: Django and Django REST framework for managing user data and health metrics.
  • Frontend: React Native and Expo for building a cross-platform mobile application.
  • AI/NLP: Hugging Face Transformers for generating personalized health recommendations.

Development Process

Step 1: Setting Up the Environment

First, we need to install the necessary tools and libraries. Ensure you have Python (3.7+) and Node.js (14+) installed on your machine. Then, set up a virtual environment and install Django and Django REST framework:

pip install django djangorestframework

Next, install React Native CLI and Expo CLI:

npm install -g expo-cli

Step 2: Django Backend Setup

Create a new Django project and app:

django-admin startproject health_assistant
cd health_assistant
python manage.py startapp api

Configure the settings in health_assistant/settings.py by adding 'rest_framework' and 'api' to INSTALLED_APPS. Set up the database (default is SQLite) and define models for user health data.

Models:

from django.db import models
from django.contrib.auth.models import User

class HealthData(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(auto_now_add=True)
steps = models.IntegerField()
sleep_hours = models.FloatField()
heart_rate = models.IntegerField()

Create serializers and set up views and URLs to handle API requests.

Views and URLs:

from rest_framework import viewsets
from .models import HealthData
from .serializers import HealthDataSerializer
class HealthDataViewSet(viewsets.ModelViewSet):
queryset = HealthData.objects.all()
serializer_class = HealthDataSerializer
from django.urls import path, include
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'healthdata', HealthDataViewSet)
urlpatterns = [
path('', include(router.urls)),
]

Apply migrations and create a superuser:

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

Run the server:

python manage.py runserver

Step 3: Integrate Hugging Face Models

Install Hugging Face Transformers:

pip install transformers

Create a service to interact with Hugging Face models for generating health recommendations.

Service:

from transformers import pipeline
recommendation_model = pipeline('text-generation', model='gpt-3')
def get_health_recommendation(user_data):
prompt = f"User data: {user_data}. Provide a health recommendation."
result = recommendation_model(prompt, max_length=50)
return result[0]['generated_text']

Add an endpoint for health recommendations:

Views and URLs:

from rest_framework.decorators import api_view
from rest_framework.response import Response
from .services import get_health_recommendation
@api_view(['POST'])
def health_recommendation(request):
user_data = request.data
recommendation = get_health_recommendation(user_data)
return Response({'recommendation': recommendation})
urlpatterns += [
path('recommendation/', health_recommendation),
]

Step 4: React Native Frontend Setup

Initialize a new React Native project with Expo:

expo init HealthAssistantApp
cd HealthAssistantApp

Install necessary dependencies:

npm install axios react-navigation react-navigation-stack

Create screens and navigation for the mobile app.

App.js:

import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import HomeScreen from './screens/HomeScreen';
import RecommendationScreen from './screens/RecommendationScreen';

const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Recommendations" component={RecommendationScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}

Create the HomeScreen and RecommendationScreen components.

HomeScreen.js:

import React, { useState } from 'react';
import { View, Text, Button, TextInput } from 'react-native';
import axios from 'axios';
export default function HomeScreen({ navigation }) {
const [steps, setSteps] = useState('');
const [sleep, setSleep] = useState('');
const [heartRate, setHeartRate] = useState('');
const submitData = () => {
axios.post('http://your-server-url/api/recommendation/', {
steps: steps,
sleep_hours: sleep,
heart_rate: heartRate
}).then(response => {
navigation.navigate('Recommendations', { recommendation: response.data.recommendation });
});
};
return (
<View>
<Text>Steps:</Text>
<TextInput value={steps} onChangeText={setSteps} keyboardType="numeric" />
<Text>Sleep Hours:</Text>
<TextInput value={sleep} onChangeText={setSleep} keyboardType="numeric" />
<Text>Heart Rate:</Text>
<TextInput value={heartRate} onChangeText={setHeartRate} keyboardType="numeric" />
<Button title="Get Recommendation" onPress={submitData} />
</View>
);
}

RecommendationScreen.js:

import React from 'react';
import { View, Text } from 'react-native';
export default function RecommendationScreen({ route }) {
const { recommendation } = route.params;
return (
<View>
<Text>Health Recommendation:</Text>
<Text>{recommendation}</Text>
</View>
);
}

Step 5: Finalize and Deploy

Test the application locally to ensure all features work correctly. Deploy the Django backend on AWS using Elastic Beanstalk or EC2. Build and publish the React Native app to app stores using Expo.

Finally, document your project and create demo videos for LinkedIn to showcase your work.

Conclusion

The Smart Health Assistant is a powerful tool that combines the latest in AI and mobile development to provide personalized health and wellness recommendations. By following this guide, you can create a similar application and customize it to fit your needs. For the complete code and detailed setup instructions, check out the GitHub repository.

Feel free to contribute to this project and make it even better! Happy coding! 😊

Thank you for reading! If you have any questions or suggestions, please leave a comment below or open an issue on GitHub. Let’s build a healthier future together! 🚀

--

--

Lakshitha Vimuth
Lakshitha Vimuth

Written by Lakshitha Vimuth

Bio-Medical Research Engineer | Emerging AI and ML Specialist | Passionate in Python & Image Processing | Aspiring Data Scientist

Responses (1)