Spaces:
Sleeping
Sleeping
Commit ·
33ab95a
1
Parent(s): c1f6c6c
Upgrade to Mistral-7B Serverless API for high intelligence
Browse files- PROJECT_DOCS.md +3 -4
- models/schemas.py +1 -0
- requirements.txt +1 -4
- services/analyzer.py +57 -76
PROJECT_DOCS.md
CHANGED
|
@@ -20,10 +20,9 @@ We used the following technologies to build this application from scratch:
|
|
| 20 |
| Component | Technology | Purpose |
|
| 21 |
| :--- | :--- | :--- |
|
| 22 |
| **Framework** | **FastAPI** | High-performance web framework for building the API endpoints. |
|
| 23 |
-
| **AI Model** | **
|
| 24 |
-
| **
|
| 25 |
-
| **
|
| 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 |
---
|
|
|
|
| 20 |
| Component | Technology | Purpose |
|
| 21 |
| :--- | :--- | :--- |
|
| 22 |
| **Framework** | **FastAPI** | High-performance web framework for building the API endpoints. |
|
| 23 |
+
| **AI Model** | **Mistral-7B-Instruct-v0.3** | A high-intelligence 7 Billion parameter model accessed via API. |
|
| 24 |
+
| **Connection** | **HF Inference Client** | Connects to Hugging Face's serverless GPU cluster. |
|
| 25 |
+
| **Container** | **Docker** | Lightweight container (no heavy ML libraries needed anymore). |
|
|
|
|
| 26 |
| **Deployment** | **Hugging Face Spaces** | The cloud platform hosting the Docker container. |
|
| 27 |
|
| 28 |
---
|
models/schemas.py
CHANGED
|
@@ -5,6 +5,7 @@ class KeywordData(BaseModel):
|
|
| 5 |
keyword: str
|
| 6 |
search_volume: str
|
| 7 |
competition: str
|
|
|
|
| 8 |
|
| 9 |
class HashtagData(BaseModel):
|
| 10 |
tag: str
|
|
|
|
| 5 |
keyword: str
|
| 6 |
search_volume: str
|
| 7 |
competition: str
|
| 8 |
+
relevance: int
|
| 9 |
|
| 10 |
class HashtagData(BaseModel):
|
| 11 |
tag: str
|
requirements.txt
CHANGED
|
@@ -1,8 +1,5 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
-
|
| 4 |
-
transformers
|
| 5 |
-
accelerate
|
| 6 |
python-dotenv
|
| 7 |
pydantic
|
| 8 |
-
sentencepiece
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
+
huggingface_hub
|
|
|
|
|
|
|
| 4 |
python-dotenv
|
| 5 |
pydantic
|
|
|
services/analyzer.py
CHANGED
|
@@ -1,36 +1,20 @@
|
|
|
|
|
| 1 |
import json
|
| 2 |
-
import
|
| 3 |
-
from
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
MODEL_ID,
|
| 16 |
-
torch_dtype=torch.float32,
|
| 17 |
-
low_cpu_mem_usage=True
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
pipe = pipeline(
|
| 21 |
-
"text-generation",
|
| 22 |
-
model=model,
|
| 23 |
-
tokenizer=tokenizer,
|
| 24 |
-
max_new_tokens=512, # Reduced from 1024 to prevent timeout on CPU
|
| 25 |
-
temperature=0.7,
|
| 26 |
-
top_p=0.9,
|
| 27 |
-
repetition_penalty=1.1,
|
| 28 |
-
do_sample=True,
|
| 29 |
-
)
|
| 30 |
-
print("✅ Model loaded successfully!")
|
| 31 |
-
except Exception as e:
|
| 32 |
-
print(f"❌ Failed to load model: {e}")
|
| 33 |
-
pipe = None
|
| 34 |
|
| 35 |
def clean_json_string(text: str) -> str:
|
| 36 |
"""Helper to extract JSON from markdown or raw text."""
|
|
@@ -42,58 +26,55 @@ def clean_json_string(text: str) -> str:
|
|
| 42 |
|
| 43 |
def analyze_seo_content(content: str) -> dict:
|
| 44 |
"""
|
| 45 |
-
Analyzes content using
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
"""
|
| 47 |
-
if pipe is None:
|
| 48 |
-
return {"error": "Model failed to load on server startup."}
|
| 49 |
-
|
| 50 |
-
# System prompt for TinyLlama
|
| 51 |
-
# We use the chat template structure for TinyLlama
|
| 52 |
-
messages = [
|
| 53 |
-
{
|
| 54 |
-
"role": "system",
|
| 55 |
-
"content": "You are a professional SEO Data Scientist. You ONLY speak in valid JSON. No generic text. Analyze the user's content and output SEO strategy."
|
| 56 |
-
},
|
| 57 |
-
{
|
| 58 |
-
"role": "user",
|
| 59 |
-
"content": f"""Analyze this topic: "{content[:500]}"
|
| 60 |
-
|
| 61 |
-
Return a JSON object with this exact structure:
|
| 62 |
-
{{
|
| 63 |
-
"core_keywords": [
|
| 64 |
-
{{"keyword": "str", "search_volume": "str", "competition": "str"}}
|
| 65 |
-
],
|
| 66 |
-
"related_phrases": ["str", "str"],
|
| 67 |
-
"viral_hashtags": [
|
| 68 |
-
{{"tag": "str", "post_count": "str"}}
|
| 69 |
-
],
|
| 70 |
-
"strategy_tips": ["str", "str"],
|
| 71 |
-
"target_audience": "str"
|
| 72 |
-
}}
|
| 73 |
-
"""
|
| 74 |
-
}
|
| 75 |
-
]
|
| 76 |
-
|
| 77 |
-
prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 78 |
|
| 79 |
try:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
cleaned_json = clean_json_string(response_part)
|
| 88 |
|
|
|
|
|
|
|
| 89 |
data = json.loads(cleaned_json)
|
| 90 |
return data
|
| 91 |
|
| 92 |
-
except
|
| 93 |
-
print(f"
|
| 94 |
return {
|
| 95 |
-
"error": "Failed
|
| 96 |
-
"raw_output": response_part[:200] + "..."
|
| 97 |
}
|
| 98 |
-
except Exception as e:
|
| 99 |
-
return {"error": f"Error during inference: {str(e)}"}
|
|
|
|
| 1 |
+
import os
|
| 2 |
import json
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from pathlib import Path
|
| 6 |
|
| 7 |
+
# Load environment variables
|
| 8 |
+
env_path = Path(__file__).resolve().parent.parent.parent / ".env"
|
| 9 |
+
load_dotenv(dotenv_path=env_path)
|
| 10 |
|
| 11 |
+
# Configuration: Use a powerful model available via free Serverless API
|
| 12 |
+
# Mistral-7B-Instruct-v0.3 is excellent for following JSON instructions.
|
| 13 |
+
MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 14 |
+
|
| 15 |
+
# Initialize Client
|
| 16 |
+
# It automatically picks up HF_TOKEN from environment if set, otherwise works anonymously (with lower limits)
|
| 17 |
+
client = InferenceClient(model=MODEL_ID)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def clean_json_string(text: str) -> str:
|
| 20 |
"""Helper to extract JSON from markdown or raw text."""
|
|
|
|
| 26 |
|
| 27 |
def analyze_seo_content(content: str) -> dict:
|
| 28 |
"""
|
| 29 |
+
Analyzes content using Mistral-7B via HF Inference API.
|
| 30 |
+
"""
|
| 31 |
+
print(f"🧠 Sending request to {MODEL_ID}...")
|
| 32 |
+
|
| 33 |
+
# Prompt Engineering for Advanced Results
|
| 34 |
+
prompt = f"""
|
| 35 |
+
<s>[INST] You are an expert SEO Strategist and Senior Content Marketer.
|
| 36 |
+
Analyze the following topic and provide a high-level, professional keyword strategy.
|
| 37 |
+
|
| 38 |
+
Topic: "{content[:2000]}"
|
| 39 |
+
|
| 40 |
+
Return ONLY a valid JSON object. Do not include any explanation.
|
| 41 |
+
JSON Format:
|
| 42 |
+
{{
|
| 43 |
+
"core_keywords": [
|
| 44 |
+
{{"keyword": "long-tail keyword 1", "search_volume": "High (10k+)", "competition": "Medium", "relevance": 95}},
|
| 45 |
+
{{"keyword": "keyword 2", "search_volume": "Medium (5k)", "competition": "Low", "relevance": 90}}
|
| 46 |
+
],
|
| 47 |
+
"related_phrases": ["phrase 1", "phrase 2", "phrase 3", "phrase 4", "phrase 5"],
|
| 48 |
+
"viral_hashtags": [
|
| 49 |
+
{{"tag": "#Hashtag1", "post_count": "1M+"}},
|
| 50 |
+
{{"tag": "#Hashtag2", "post_count": "500k+"}}
|
| 51 |
+
],
|
| 52 |
+
"strategy_tips": [
|
| 53 |
+
"Specific actionable tip 1 (e.g., content structure)",
|
| 54 |
+
"Specific actionable tip 2 (e.g., audience hook)",
|
| 55 |
+
"Specific actionable tip 3"
|
| 56 |
+
],
|
| 57 |
+
"target_audience": "Detailed persona description of who is searching for this."
|
| 58 |
+
}}
|
| 59 |
+
[/INST]
|
| 60 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
response = client.text_generation(
|
| 64 |
+
prompt,
|
| 65 |
+
max_new_tokens=1500,
|
| 66 |
+
temperature=0.7,
|
| 67 |
+
top_p=0.9,
|
| 68 |
+
return_full_text=False
|
| 69 |
+
)
|
|
|
|
| 70 |
|
| 71 |
+
# Clean and Parse
|
| 72 |
+
cleaned_json = clean_json_string(response)
|
| 73 |
data = json.loads(cleaned_json)
|
| 74 |
return data
|
| 75 |
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"❌ Inference Error: {e}")
|
| 78 |
return {
|
| 79 |
+
"error": f"AI Generation Failed: {str(e)}. (Hint: Check if HF_TOKEN is set in Space Settings for better stability)"
|
|
|
|
| 80 |
}
|
|
|
|
|
|