ihtesham0345 commited on
Commit
8220e69
Β·
1 Parent(s): 0bf7aa3

Update docs with A-Z guide

Browse files
Files changed (1) hide show
  1. PROJECT_DOCS.md +91 -62
PROJECT_DOCS.md CHANGED
@@ -1,87 +1,116 @@
1
- # πŸ“˜ SEO Keyword Analyzer API - Project Documentation
2
 
3
  ## 1. Project Overview
4
- This project is a high-performance **Microservice** built with **FastAPI**. Its purpose is to act as an intelligent SEO (Search Engine Optimization) consultant. It accepts raw text (like a video topic or blog idea) and uses Google's **Gemini Generative AI** to output a structured strategy containing keywords, viral hashtags, and target audience analysis.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  ---
7
 
8
- ## 2. Directory Structure & File Explanation
 
9
 
10
  ```
11
  SEO_Analyzer_FastAPI/
12
- β”œβ”€β”€ main.py # πŸš€ The Entry Point
13
- β”œβ”€β”€ requirements.txt # πŸ“¦ Dependencies
 
14
  β”œβ”€β”€ models/
15
- β”‚ └── schemas.py # πŸ—οΈ Data Structure (Validation)
16
  └── services/
17
- └── analyzer.py # 🧠 The Brain (AI Logic)
18
  ```
19
 
20
- ### A. `main.py` (The Traffic Controller)
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
- ### C. `services/analyzer.py` (The Brain)
32
- This contains the core business logic.
33
- - **Loading Environment**: Finds your `.env` file to get the Secret Key.
34
- - **Prompt Engineering**: Constructs a strict prompt telling the AI to act as an "SEO Strategist".
35
- - **AI Call**: Sends the prompt to Google Gemini.
36
- - **JSON Parsing**: Takes the AI's raw text response and converts it into a clean Python Dictionary.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  ---
39
 
40
- ## 3. How the Logic Works (A to Z)
41
-
42
- 1. **Request**: You send `POST /analyze-seo` with `{"content": "youtube automation"}`.
43
- 2. **Validation**: `main.py` uses `schemas.py` to confirm you sent text, not a number or empty file.
44
- 3. **Processing**:
45
- - The code calls `analyze_seo_content("youtube automation")`.
46
- - It builds a prompt: *"Analyze 'youtube automation' and give me high-volume keywords..."*
47
- 4. **AI Interaction**:
48
- - The system tries to connect to `gemini-2.0-flash`.
49
- - If that model is busy or broken (404), it automatically loops to the next backup model (`gemini-flash-latest`).
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
- ## 4. How to Use & Run (Step-by-Step)
60
 
61
- ### Step 1: Install Requirements
62
- Open your terminal in the project folder and run:
63
- ```bash
64
- pip install -r requirements.txt
65
- ```
66
 
67
- ### Step 2: Run the Server
68
- Start the API server (Uvicorn):
69
- ```bash
70
- python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000
71
- ```
72
- - `host 0.0.0.0`: Allows it to work on LAN (WiFi).
73
- - `reload`: Updates automatically when you save code.
74
 
75
- ### Step 3: Test with Swagger UI
76
- 1. Open your browser to: **[http://localhost:8000/docs](http://localhost:8000/docs)**
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
- ## 5. Troubleshooting
86
- - **Error 500 "GEMINI_API_KEY not found"**: correct: Ensure your `.env` file is in the parent directory or properly loaded in `analyzer.py`.
87
- - **404 Model Not Found**: The code handles this by trying multiple models, but ensure `MODELS_FALLBACK` contains valid model names correctly.
 
 
 
 
 
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**