ihtesham0345 commited on
Commit
33ab95a
·
1 Parent(s): c1f6c6c

Upgrade to Mistral-7B Serverless API for high intelligence

Browse files
Files changed (4) hide show
  1. PROJECT_DOCS.md +3 -4
  2. models/schemas.py +1 -0
  3. requirements.txt +1 -4
  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** | **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
  ---
 
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
- torch
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 torch
3
- from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
 
4
 
5
- # --- Configuration ---
6
- MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
 
7
 
8
- # Initialize model globally to load once at startup
9
- print(f"🔄 Loading Local Model: {MODEL_ID}...")
10
- try:
11
- # Use float32 for CPU compatibility (most Spaces default to CPU)
12
- # If a GPU is available, you could use torch.float16 and devise_map="auto"
13
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
14
- model = AutoModelForCausalLM.from_pretrained(
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 the local LLM.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- outputs = pipe(prompt)
81
- generated_text = outputs[0]["generated_text"]
82
-
83
- # Extract the assistant's response part
84
- # TinyLlama format: <|system|>...<|user|>...<|assistant|>
85
- response_part = generated_text.split("<|assistant|>")[-1]
86
-
87
- cleaned_json = clean_json_string(response_part)
88
 
 
 
89
  data = json.loads(cleaned_json)
90
  return data
91
 
92
- except json.JSONDecodeError:
93
- print(f"DEBUG - Raw Text: {response_part}")
94
  return {
95
- "error": "Failed to generate valid JSON. The model might be too small for complex formatting.",
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
  }