Spaces:
Sleeping
Sleeping
Commit Β·
8220e69
1
Parent(s): 0bf7aa3
Update docs with A-Z guide
Browse files- PROJECT_DOCS.md +91 -62
PROJECT_DOCS.md
CHANGED
|
@@ -1,87 +1,116 @@
|
|
| 1 |
-
# π SEO Keyword Analyzer API -
|
| 2 |
|
| 3 |
## 1. Project Overview
|
| 4 |
-
This project is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
---
|
| 7 |
|
| 8 |
-
##
|
|
|
|
| 9 |
|
| 10 |
```
|
| 11 |
SEO_Analyzer_FastAPI/
|
| 12 |
-
βββ main.py #
|
| 13 |
-
βββ requirements.txt # π¦ Dependencies
|
|
|
|
| 14 |
βββ models/
|
| 15 |
-
β βββ schemas.py #
|
| 16 |
βββ services/
|
| 17 |
-
βββ analyzer.py # π§ The Brain
|
| 18 |
```
|
| 19 |
|
| 20 |
-
|
| 21 |
-
This is where the application starts.
|
| 22 |
-
- **FastAPI App**: Initializes the web server.
|
| 23 |
-
- **Endpoint `/analyze-seo`**: A specific "door" where users send data.
|
| 24 |
-
- **Logic**: When a request comes in, it checks the data format and passes it to the *Brain* (`analyzer.py`).
|
| 25 |
-
|
| 26 |
-
### B. `models/schemas.py` (The Blueprint)
|
| 27 |
-
This file defines exactly what data looks like using **Pydantic**.
|
| 28 |
-
- `SEORequest`: Ensures the user sends a JSON with a `"content"` field (String).
|
| 29 |
-
- `SEOResponse`: Ensures the API returns a standard format (Keywords, Hashtags, Tips) so the frontend never crashes.
|
| 30 |
|
| 31 |
-
##
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
- **
|
| 36 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
---
|
| 39 |
|
| 40 |
-
##
|
| 41 |
-
|
| 42 |
-
1. **
|
| 43 |
-
2. **
|
| 44 |
-
3. **
|
| 45 |
-
- The
|
| 46 |
-
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
- This "Fallback Loop" ensures high reliability.
|
| 51 |
-
5. **Response Handling**:
|
| 52 |
-
- The AI returns a JSON-formatted string.
|
| 53 |
-
- Python parses this string.
|
| 54 |
-
- The function returns the data.
|
| 55 |
-
6. **Response**: You receive the JSON data with code `200 OK`.
|
| 56 |
|
| 57 |
---
|
| 58 |
|
| 59 |
-
##
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
```
|
| 72 |
-
- `host 0.0.0.0`: Allows it to work on LAN (WiFi).
|
| 73 |
-
- `reload`: Updates automatically when you save code.
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
2. Click **POST /analyze-seo**.
|
| 78 |
-
3. Click **Try it out**.
|
| 79 |
-
4. Enter your topic in the "content" field.
|
| 80 |
-
5. Click **Execute**.
|
| 81 |
-
6. See the results below!
|
| 82 |
|
| 83 |
---
|
| 84 |
|
| 85 |
-
##
|
| 86 |
-
- **
|
| 87 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π SEO Keyword Analyzer API - Complete Development Guide
|
| 2 |
|
| 3 |
## 1. Project Overview
|
| 4 |
+
This project is an **AI-Powered Microservice** built with **FastAPI**. It serves as an intelligent SEO consultant that accepts a topic (e.g., "Digital Marketing") and generates a comprehensive strategy including:
|
| 5 |
+
- High-volume Keywords
|
| 6 |
+
- Viral Hashtags
|
| 7 |
+
- Competition Analysis
|
| 8 |
+
- Strategic Tips
|
| 9 |
+
|
| 10 |
+
**Key Feature:** Unlike traditional wrapper APIs, this project uses a **Local LLM (TinyLlama-1.1B)** embedded directly within the application. It does **not** rely on external paid APIs (like OpenAI or Gemini), ensuring it is:
|
| 11 |
+
- **Free to run** (no per-token cost).
|
| 12 |
+
- **Private** (data doesn't leave the container).
|
| 13 |
+
- **Self-contained**.
|
| 14 |
+
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
## 2. Technology Stack used
|
| 18 |
+
We used the following technologies to build this application from scratch:
|
| 19 |
+
|
| 20 |
+
| Component | Technology | Purpose |
|
| 21 |
+
| :--- | :--- | :--- |
|
| 22 |
+
| **Framework** | **FastAPI** | High-performance web framework for building the API endpoints. |
|
| 23 |
+
| **AI Model** | **TinyLlama-1.1B-Chat** | A compact, powerful open-source LLM optimized for chat and instruction following. |
|
| 24 |
+
| **ML Library** | **Hugging Face Transformers** | To load and run the model locally. |
|
| 25 |
+
| **Compute** | **PyTorch (CPU/GPU)** | The underlying tensor computation engine. |
|
| 26 |
+
| **Container** | **Docker** | To package the application with all dependencies for cloud deployment. |
|
| 27 |
+
| **Deployment** | **Hugging Face Spaces** | The cloud platform hosting the Docker container. |
|
| 28 |
|
| 29 |
---
|
| 30 |
|
| 31 |
+
## 3. Directory Structure Explaination
|
| 32 |
+
Here is how the project files are organized:
|
| 33 |
|
| 34 |
```
|
| 35 |
SEO_Analyzer_FastAPI/
|
| 36 |
+
βββ main.py # οΏ½ Entry Point: Defines the API routes & server.
|
| 37 |
+
βββ requirements.txt # π¦ Dependencies: Lists libraries (torch, fastapi, etc.).
|
| 38 |
+
βββ Dockerfile # π³ Deployment: Instructions to build the Linux container.
|
| 39 |
βββ models/
|
| 40 |
+
β βββ schemas.py # π Data Models: Pydantic classes to validate input/output.
|
| 41 |
βββ services/
|
| 42 |
+
βββ analyzer.py # π§ The Brain: Loads the AI model and handles inference.
|
| 43 |
```
|
| 44 |
|
| 45 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
## 4. How It Was Built (A to Z)
|
| 48 |
+
|
| 49 |
+
### Step 1: Defining the Data Structure (`models/schemas.py`)
|
| 50 |
+
Before writing code, we defined what the "Input" and "Output" should look like using **Pydantic**.
|
| 51 |
+
- **Input**: A simple JSON object `{"content": "..."}`.
|
| 52 |
+
- **Output**: A strict JSON schema ensuring the UI always receives `core_keywords`, `hashtags`, etc.
|
| 53 |
+
|
| 54 |
+
### Step 2: Building the Logic Core (`services/analyzer.py`)
|
| 55 |
+
This is the most complex part. Instead of calling an external API, we:
|
| 56 |
+
1. **Initialize the Model**: On startup, we download `TinyLlama/TinyLlama-1.1B-Chat-v1.0` using `AutoModelForCausalLM`.
|
| 57 |
+
2. **Pipeline Creation**: We set up a Hugging Face `pipeline("text-generation")` to handle the complexity of tokenization and prediction.
|
| 58 |
+
3. **Prompt Engineering**: We crafted a specific "System Prompt" that forces the AI to act as an SEO expert and output **strict JSON**.
|
| 59 |
+
> *"You are a professional SEO Data Scientist. You ONLY speak in valid JSON..."*
|
| 60 |
+
4. **Parsing**: The model outputs raw text. We wrote a helper function `clean_json_string` to strip away markdown code blocks and extract the pure JSON data for the API response.
|
| 61 |
+
|
| 62 |
+
### Step 3: Creating the API Endpoints (`main.py`)
|
| 63 |
+
We created a FastAPI app with two routes:
|
| 64 |
+
- `GET /`: A health check to confirm the server is running.
|
| 65 |
+
- `POST /analyze-seo`: The main worker. It:
|
| 66 |
+
1. Receives the user's text.
|
| 67 |
+
2. Validates it against our Schema.
|
| 68 |
+
3. Passes it to the `analyzer.py` brain.
|
| 69 |
+
4. Returns the structured results.
|
| 70 |
+
|
| 71 |
+
### Step 4: Dockerization (`Dockerfile`)
|
| 72 |
+
To make this run anywhere (local PC, Cloud, Hugging Face), we packaged it in a Docker container.
|
| 73 |
+
- **Base Image**: `python:3.9`
|
| 74 |
+
- **User Permissions**: Sets up a non-root user (id 1000) for security (required by HF Spaces).
|
| 75 |
+
- **Commands**: Installs `requirements.txt` and starts the `uvicorn` server on port **7860**.
|
| 76 |
|
| 77 |
---
|
| 78 |
|
| 79 |
+
## 5. How It Works (The Flow)
|
| 80 |
+
|
| 81 |
+
1. **User Action**: Sends a request: `POST {"content": "dropshipping"}`.
|
| 82 |
+
2. **API Layer**: FastAPI validates the request.
|
| 83 |
+
3. **Buisness Logic**:
|
| 84 |
+
- The `analyzer` constructs a prompt.
|
| 85 |
+
- The **TinyLlama** model runs the prompt through its neural network (locally on CPU).
|
| 86 |
+
- The model generates a comprehensive analysis in text format.
|
| 87 |
+
4. **Processing**: Python extracts the JSON from the text.
|
| 88 |
+
5. **Response**: The user receives a clean JSON response with keywords, hashtags, and tips.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
---
|
| 91 |
|
| 92 |
+
## 6. How to Run Locally
|
| 93 |
|
| 94 |
+
1. **Install Requirements**:
|
| 95 |
+
```bash
|
| 96 |
+
pip install -r requirements.txt
|
| 97 |
+
```
|
| 98 |
+
*(Note: This installs PyTorch, which is large ~2GB)*
|
| 99 |
|
| 100 |
+
2. **Run the Server**:
|
| 101 |
+
```bash
|
| 102 |
+
python -m uvicorn main:app --reload
|
| 103 |
+
```
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
3. **Access Documentation**:
|
| 106 |
+
Open your browser to `http://localhost:8000/docs` to see the interactive Swagger UI.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
---
|
| 109 |
|
| 110 |
+
## 7. Configuration Limitations
|
| 111 |
+
- **Hardware**: Running a 1.1 Billion parameter model requires about **3GB of RAM**.
|
| 112 |
+
- **Speed**: On a standard CPU, generation might take 10-30 seconds. On a GPU, it takes <2 seconds.
|
| 113 |
+
- **Max Length**: The model is configured to generate up to 1024 new tokens.
|
| 114 |
+
|
| 115 |
+
---
|
| 116 |
+
**Developed by Ihtesham | Powered by Open Source AI**
|