Prathamesh Sable commited on
Commit
af48aca
·
1 Parent(s): 38c555b

Add Hugging Face Transformers integration and update documentation

Browse files

* **services/ai_agent.py**
- Add `integrate_hugging_face_transformers` function to integrate Hugging Face Transformers for NLP tasks.

* **tests/test_ai_agent_service.py**
- Add unit test for `integrate_hugging_face_transformers` function.

* **requirements.txt**
- Add `transformers` library.

* **.env.example**
- Add example environment variables including Hugging Face Transformers API key.

* **README.md**
- Add installation and setup instructions.
- Add API endpoints documentation.
- Add environment variables documentation.

.env.example ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Environment variables for FoodAnalyzer-API
2
+
3
+ # Database configuration
4
+ DATABASE_URL=sqlite:///./test.db
5
+
6
+ # JWT Secret Key
7
+ SECRET_KEY=09d8f7a6b5c4e3d2f1a0b9c8d7e6f5a4
8
+ ALGORITHM=HS256
9
+ ACCESS_TOKEN_EXPIRE_MINUTES=30
10
+
11
+ # Redis configuration
12
+ REDIS_HOST=localhost
13
+ REDIS_PORT=6379
14
+ REDIS_DB=0
15
+
16
+ # OCR configuration
17
+ TESSERACT_CMD=/usr/bin/tesseract
18
+
19
+ # API configuration
20
+ API_BASE_URL=https://api.example.com
21
+
22
+ # Hugging Face Transformers API key
23
+ HUGGING_FACE_API_KEY=your_api_key_here # Get your API key from https://huggingface.co
README.md CHANGED
@@ -1,3 +1,70 @@
1
- # Module 2
2
 
3
- ## API END POINT TO PROCESS PRODUCT DATA AND RETURN RESPONSE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FoodAnalyzer-API
2
 
3
+ ## Installation and Setup
4
+
5
+ 1. **Clone the repository**:
6
+ ```bash
7
+ git clone https://github.com/prathameshks/FoodAnalyzer-API.git
8
+ ```
9
+
10
+ 2. **Navigate to the project directory**:
11
+ ```bash
12
+ cd FoodAnalyzer-API
13
+ ```
14
+
15
+ 3. **Create a virtual environment**:
16
+ ```bash
17
+ python -m venv venv
18
+ ```
19
+
20
+ 4. **Activate the virtual environment**:
21
+ - On Windows:
22
+ ```bash
23
+ venv\Scripts\activate
24
+ ```
25
+ - On macOS/Linux:
26
+ ```bash
27
+ source venv/bin/activate
28
+ ```
29
+
30
+ 5. **Install dependencies**:
31
+ ```bash
32
+ pip install -r requirements.txt
33
+ ```
34
+
35
+ 6. **Set up environment variables**:
36
+ Copy the `.env.example` file to `.env` and fill in the required values, including API keys for Hugging Face Transformers.
37
+
38
+ 7. **Run the application**:
39
+ ```bash
40
+ uvicorn main:app --reload
41
+ ```
42
+
43
+ ## API Endpoints
44
+
45
+ ### Authentication
46
+ - `POST /api/register`: Register a new user.
47
+ - `POST /api/login`: Login and obtain an access token.
48
+ - `GET /api/users/me`: Get the current user's information.
49
+
50
+ ### Ingredient Analysis
51
+ - `POST /api/analyze_ingredients`: Analyze a list of ingredients.
52
+ - `GET /api/personalized_recommendations`: Get personalized ingredient recommendations.
53
+
54
+ ### Scan History
55
+ - `POST /api/scan`: Record a new scan.
56
+ - `GET /api/history/{user_id}`: Retrieve the scan history for a user.
57
+
58
+ ### Product Data
59
+ - `GET /api/extract_product_info`: Extract product information from a barcode.
60
+ - `POST /api/fetch_product_data`: Fetch product data for a list of barcodes.
61
+
62
+ ## Environment Variables
63
+
64
+ ### Hugging Face Transformers API key
65
+ Obtain a free API key from Hugging Face by signing up on their website. Add the API key to the `.env` file with the variable name `HUGGING_FACE_API_KEY`.
66
+
67
+ Example:
68
+ ```env
69
+ HUGGING_FACE_API_KEY=your_api_key_here # Get your API key from https://huggingface.co
70
+ ```
requirements.txt CHANGED
@@ -7,3 +7,4 @@ opencv-python
7
  sqlalchemy
8
  alembic
9
  redis
 
 
7
  sqlalchemy
8
  alembic
9
  redis
10
+ transformers
services/ai_agent.py CHANGED
@@ -5,6 +5,7 @@ from models.ingredient import Ingredient
5
  from services.ingredients import get_ingredient_by_name, save_ingredient_data
6
  from typing import Dict, Any
7
  import json
 
8
 
9
  def preprocess_data(barcode: str) -> Dict[str, Any]:
10
  data = fetch_product_data_from_api(barcode)
@@ -86,3 +87,8 @@ def process_data(db: Session, barcode: str) -> Dict[str, Any]:
86
  data = enrich_data(db, data)
87
  save_json_file(barcode, data)
88
  return data
 
 
 
 
 
 
5
  from services.ingredients import get_ingredient_by_name, save_ingredient_data
6
  from typing import Dict, Any
7
  import json
8
+ from transformers import pipeline
9
 
10
  def preprocess_data(barcode: str) -> Dict[str, Any]:
11
  data = fetch_product_data_from_api(barcode)
 
87
  data = enrich_data(db, data)
88
  save_json_file(barcode, data)
89
  return data
90
+
91
+ def integrate_hugging_face_transformers(model_name: str, text: str) -> str:
92
+ nlp = pipeline("fill-mask", model=model_name)
93
+ result = nlp(text)
94
+ return result[0]['sequence']
tests/test_ai_agent_service.py CHANGED
@@ -1,7 +1,7 @@
1
  import unittest
2
  from unittest.mock import patch, MagicMock
3
  from sqlalchemy.orm import Session
4
- from services.ai_agent import preprocess_data, validate_data, clean_data, standardize_data, enrich_data, process_data
5
 
6
  class TestAIAgentService(unittest.TestCase):
7
 
@@ -137,5 +137,11 @@ class TestAIAgentService(unittest.TestCase):
137
  self.assertEqual(result['product_name'], 'Test Product')
138
  mock_save_json_file.assert_called_once_with('test_barcode', {'product_name': 'Test Product'})
139
 
 
 
 
 
 
 
140
  if __name__ == '__main__':
141
  unittest.main()
 
1
  import unittest
2
  from unittest.mock import patch, MagicMock
3
  from sqlalchemy.orm import Session
4
+ from services.ai_agent import preprocess_data, validate_data, clean_data, standardize_data, enrich_data, process_data, integrate_hugging_face_transformers
5
 
6
  class TestAIAgentService(unittest.TestCase):
7
 
 
137
  self.assertEqual(result['product_name'], 'Test Product')
138
  mock_save_json_file.assert_called_once_with('test_barcode', {'product_name': 'Test Product'})
139
 
140
+ @patch('services.ai_agent.pipeline')
141
+ def test_integrate_hugging_face_transformers(self, mock_pipeline):
142
+ mock_pipeline.return_value = lambda text: [{'sequence': 'Test sequence'}]
143
+ result = integrate_hugging_face_transformers('test_model', 'Test text')
144
+ self.assertEqual(result, 'Test sequence')
145
+
146
  if __name__ == '__main__':
147
  unittest.main()