Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,151 +1,151 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
from typing import Dict, List, Any, Optional
|
| 5 |
-
import os
|
| 6 |
-
from groq import Groq
|
| 7 |
-
|
| 8 |
-
app = FastAPI(title="Architecture Evaluator LLM")
|
| 9 |
-
|
| 10 |
-
# Enable CORS for all origins (adjust in production)
|
| 11 |
-
app.add_middleware(
|
| 12 |
-
CORSMiddleware,
|
| 13 |
-
allow_origins=["*"],
|
| 14 |
-
allow_credentials=True,
|
| 15 |
-
allow_methods=["*"],
|
| 16 |
-
allow_headers=["*"],
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
# Initialize Groq client - FREE and FAST!
|
| 20 |
-
client = Groq(
|
| 21 |
-
api_key=os.getenv("GROQ_API_KEY", "your-groq-api-key-here")
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
class EvaluationRequest(BaseModel):
|
| 25 |
-
question: str
|
| 26 |
-
architecture: Dict[str, Any]
|
| 27 |
-
|
| 28 |
-
class HeuristicScores(BaseModel):
|
| 29 |
-
DURABILITY: float
|
| 30 |
-
AVAILABILITY: float
|
| 31 |
-
ENERGY_EFFICIENCY: float
|
| 32 |
-
CONSISTENCY: float
|
| 33 |
-
MAINTAINABILITY: float
|
| 34 |
-
LATENCY: float
|
| 35 |
-
COST: float
|
| 36 |
-
SECURITY: float
|
| 37 |
-
THROUGHPUT: float
|
| 38 |
-
SCALABILITY: float
|
| 39 |
-
|
| 40 |
-
class EvaluationResponse(BaseModel):
|
| 41 |
-
heuristic_scores: HeuristicScores
|
| 42 |
-
suggestion: str
|
| 43 |
-
|
| 44 |
-
def create_evaluation_prompt(question: str, architecture: Dict[str, Any]) -> str:
|
| 45 |
-
"""Create a detailed prompt for the LLM to evaluate the architecture."""
|
| 46 |
-
|
| 47 |
-
prompt = f"""You are an expert system architect. Analyze the following system design architecture and provide an evaluation.
|
| 48 |
-
|
| 49 |
-
**Question/Context:**
|
| 50 |
-
{question}
|
| 51 |
-
|
| 52 |
-
**Architecture JSON:**
|
| 53 |
-
{architecture}
|
| 54 |
-
|
| 55 |
-
**Your Task:**
|
| 56 |
-
1. Analyze the overall architecture based on the components and their connections (links)
|
| 57 |
-
2. Calculate aggregate heuristic scores (0-10 scale) for the entire architecture considering:
|
| 58 |
-
- Individual component scores
|
| 59 |
-
- How components interact through links
|
| 60 |
-
- Overall system design patterns
|
| 61 |
-
- Redundancy and fault tolerance
|
| 62 |
-
- Data flow efficiency
|
| 63 |
-
|
| 64 |
-
3. Provide constructive suggestions on:
|
| 65 |
-
- What's good about this architecture
|
| 66 |
-
- What could be improved
|
| 67 |
-
- Specific recommendations
|
| 68 |
-
- Alternative approaches if applicable
|
| 69 |
-
|
| 70 |
-
**Important:** You must respond with ONLY a valid JSON object in this exact format:
|
| 71 |
-
{{
|
| 72 |
-
"heuristic_scores": {{
|
| 73 |
-
"DURABILITY": <float between 0-10>,
|
| 74 |
-
"AVAILABILITY": <float between 0-10>,
|
| 75 |
-
"ENERGY_EFFICIENCY": <float between 0-10>,
|
| 76 |
-
"CONSISTENCY": <float between 0-10>,
|
| 77 |
-
"MAINTAINABILITY": <float between 0-10>,
|
| 78 |
-
"LATENCY": <float between 0-10>,
|
| 79 |
-
"COST": <float between 0-10>,
|
| 80 |
-
"SECURITY": <float between 0-10>,
|
| 81 |
-
"THROUGHPUT": <float between 0-10>,
|
| 82 |
-
"SCALABILITY": <float between 0-10>
|
| 83 |
-
}},
|
| 84 |
-
"suggestion": "<your detailed analysis and suggestions as a string>"
|
| 85 |
-
}}
|
| 86 |
-
|
| 87 |
-
Do not include any text outside the JSON object. Ensure all scores are numeric values between 0 and 10."""
|
| 88 |
-
|
| 89 |
-
return prompt
|
| 90 |
-
|
| 91 |
-
@app.get("/")
|
| 92 |
-
async def root():
|
| 93 |
-
return {
|
| 94 |
-
"message": "Architecture Evaluator LLM API",
|
| 95 |
-
"status": "online",
|
| 96 |
-
"endpoints": {
|
| 97 |
-
"/evaluate": "POST - Evaluate a system architecture",
|
| 98 |
-
"/health": "GET - Health check"
|
| 99 |
-
}
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
@app.get("/health")
|
| 103 |
-
async def health():
|
| 104 |
-
return {"status": "healthy"}
|
| 105 |
-
|
| 106 |
-
@app.post("/evaluate", response_model=EvaluationResponse)
|
| 107 |
-
async def evaluate_architecture(request: EvaluationRequest):
|
| 108 |
-
"""
|
| 109 |
-
Evaluate a system design architecture using LLM.
|
| 110 |
-
|
| 111 |
-
Args:
|
| 112 |
-
request: Contains the question and architecture JSON
|
| 113 |
-
|
| 114 |
-
Returns:
|
| 115 |
-
EvaluationResponse with heuristic scores and suggestions
|
| 116 |
-
"""
|
| 117 |
-
try:
|
| 118 |
-
# Create the prompt
|
| 119 |
-
prompt = create_evaluation_prompt(request.question, request.architecture)
|
| 120 |
-
|
| 121 |
-
# Call Groq LLM - Using best reasoning model (deepseek-r1-distill-llama-70b)
|
| 122 |
-
# Alternative models: llama-3.3-70b-versatile, llama-3.1-70b-versatile
|
| 123 |
-
response = client.chat.completions.create(
|
| 124 |
-
model=os.getenv("MODEL_NAME", "llama-3.3-70b-versatile"),
|
| 125 |
-
messages=[
|
| 126 |
-
{
|
| 127 |
-
"role": "system",
|
| 128 |
-
"content": "You are an expert system architect who provides detailed analysis and returns responses in strict JSON format."
|
| 129 |
-
},
|
| 130 |
-
{
|
| 131 |
-
"role": "user",
|
| 132 |
-
"content": prompt
|
| 133 |
-
}
|
| 134 |
-
],
|
| 135 |
-
temperature=0.7,
|
| 136 |
-
response_format={"type": "json_object"} # Enforce JSON response
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
# Parse the response
|
| 140 |
-
import json
|
| 141 |
-
result = json.loads(response.choices[0].message.content)
|
| 142 |
-
|
| 143 |
-
# Validate and return
|
| 144 |
-
return EvaluationResponse(**result)
|
| 145 |
-
|
| 146 |
-
except Exception as e:
|
| 147 |
-
raise HTTPException(status_code=500, detail=f"Error evaluating architecture: {str(e)}")
|
| 148 |
-
|
| 149 |
-
if __name__ == "__main__":
|
| 150 |
-
import uvicorn
|
| 151 |
-
uvicorn.run(app, host="0.0.0.0", port=7860) # Port 7860 is standard for HuggingFace Spaces
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import Dict, List, Any, Optional
|
| 5 |
+
import os
|
| 6 |
+
from groq import Groq
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="Architecture Evaluator LLM")
|
| 9 |
+
|
| 10 |
+
# Enable CORS for all origins (adjust in production)
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
| 14 |
+
allow_credentials=True,
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Initialize Groq client - FREE and FAST!
|
| 20 |
+
client = Groq(
|
| 21 |
+
api_key=os.getenv("GROQ_API_KEY", "your-groq-api-key-here")
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
class EvaluationRequest(BaseModel):
|
| 25 |
+
question: str
|
| 26 |
+
architecture: Dict[str, Any]
|
| 27 |
+
|
| 28 |
+
class HeuristicScores(BaseModel):
|
| 29 |
+
DURABILITY: float
|
| 30 |
+
AVAILABILITY: float
|
| 31 |
+
ENERGY_EFFICIENCY: float
|
| 32 |
+
CONSISTENCY: float
|
| 33 |
+
MAINTAINABILITY: float
|
| 34 |
+
LATENCY: float
|
| 35 |
+
COST: float
|
| 36 |
+
SECURITY: float
|
| 37 |
+
THROUGHPUT: float
|
| 38 |
+
SCALABILITY: float
|
| 39 |
+
|
| 40 |
+
class EvaluationResponse(BaseModel):
|
| 41 |
+
heuristic_scores: HeuristicScores
|
| 42 |
+
suggestion: str
|
| 43 |
+
|
| 44 |
+
def create_evaluation_prompt(question: str, architecture: Dict[str, Any]) -> str:
|
| 45 |
+
"""Create a detailed prompt for the LLM to evaluate the architecture."""
|
| 46 |
+
|
| 47 |
+
prompt = f"""You are an expert system architect. Analyze the following system design architecture and provide an evaluation.
|
| 48 |
+
|
| 49 |
+
**Question/Context:**
|
| 50 |
+
{question}
|
| 51 |
+
|
| 52 |
+
**Architecture JSON:**
|
| 53 |
+
{architecture}
|
| 54 |
+
|
| 55 |
+
**Your Task:**
|
| 56 |
+
1. Analyze the overall architecture based on the components and their connections (links)
|
| 57 |
+
2. Calculate aggregate heuristic scores (0-10 scale) for the entire architecture considering:
|
| 58 |
+
- Individual component scores
|
| 59 |
+
- How components interact through links
|
| 60 |
+
- Overall system design patterns
|
| 61 |
+
- Redundancy and fault tolerance
|
| 62 |
+
- Data flow efficiency
|
| 63 |
+
|
| 64 |
+
3. Provide constructive suggestions on:
|
| 65 |
+
- What's good about this architecture
|
| 66 |
+
- What could be improved
|
| 67 |
+
- Specific recommendations
|
| 68 |
+
- Alternative approaches if applicable
|
| 69 |
+
|
| 70 |
+
**Important:** You must respond with ONLY a valid JSON object in this exact format:
|
| 71 |
+
{{
|
| 72 |
+
"heuristic_scores": {{
|
| 73 |
+
"DURABILITY": <float between 0-10>,
|
| 74 |
+
"AVAILABILITY": <float between 0-10>,
|
| 75 |
+
"ENERGY_EFFICIENCY": <float between 0-10>,
|
| 76 |
+
"CONSISTENCY": <float between 0-10>,
|
| 77 |
+
"MAINTAINABILITY": <float between 0-10>,
|
| 78 |
+
"LATENCY": <float between 0-10>,
|
| 79 |
+
"COST": <float between 0-10>,
|
| 80 |
+
"SECURITY": <float between 0-10>,
|
| 81 |
+
"THROUGHPUT": <float between 0-10>,
|
| 82 |
+
"SCALABILITY": <float between 0-10>
|
| 83 |
+
}},
|
| 84 |
+
"suggestion": "<your detailed analysis and suggestions as a string which should be in bulleted point>"
|
| 85 |
+
}}
|
| 86 |
+
|
| 87 |
+
Do not include any text outside the JSON object. Ensure all scores are numeric values between 0 and 10."""
|
| 88 |
+
|
| 89 |
+
return prompt
|
| 90 |
+
|
| 91 |
+
@app.get("/")
|
| 92 |
+
async def root():
|
| 93 |
+
return {
|
| 94 |
+
"message": "Architecture Evaluator LLM API",
|
| 95 |
+
"status": "online",
|
| 96 |
+
"endpoints": {
|
| 97 |
+
"/evaluate": "POST - Evaluate a system architecture",
|
| 98 |
+
"/health": "GET - Health check"
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
@app.get("/health")
|
| 103 |
+
async def health():
|
| 104 |
+
return {"status": "healthy"}
|
| 105 |
+
|
| 106 |
+
@app.post("/evaluate", response_model=EvaluationResponse)
|
| 107 |
+
async def evaluate_architecture(request: EvaluationRequest):
|
| 108 |
+
"""
|
| 109 |
+
Evaluate a system design architecture using LLM.
|
| 110 |
+
|
| 111 |
+
Args:
|
| 112 |
+
request: Contains the question and architecture JSON
|
| 113 |
+
|
| 114 |
+
Returns:
|
| 115 |
+
EvaluationResponse with heuristic scores and suggestions
|
| 116 |
+
"""
|
| 117 |
+
try:
|
| 118 |
+
# Create the prompt
|
| 119 |
+
prompt = create_evaluation_prompt(request.question, request.architecture)
|
| 120 |
+
|
| 121 |
+
# Call Groq LLM - Using best reasoning model (deepseek-r1-distill-llama-70b)
|
| 122 |
+
# Alternative models: llama-3.3-70b-versatile, llama-3.1-70b-versatile
|
| 123 |
+
response = client.chat.completions.create(
|
| 124 |
+
model=os.getenv("MODEL_NAME", "llama-3.3-70b-versatile"),
|
| 125 |
+
messages=[
|
| 126 |
+
{
|
| 127 |
+
"role": "system",
|
| 128 |
+
"content": "You are an expert system architect who provides detailed analysis and returns responses in strict JSON format."
|
| 129 |
+
},
|
| 130 |
+
{
|
| 131 |
+
"role": "user",
|
| 132 |
+
"content": prompt
|
| 133 |
+
}
|
| 134 |
+
],
|
| 135 |
+
temperature=0.7,
|
| 136 |
+
response_format={"type": "json_object"} # Enforce JSON response
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Parse the response
|
| 140 |
+
import json
|
| 141 |
+
result = json.loads(response.choices[0].message.content)
|
| 142 |
+
|
| 143 |
+
# Validate and return
|
| 144 |
+
return EvaluationResponse(**result)
|
| 145 |
+
|
| 146 |
+
except Exception as e:
|
| 147 |
+
raise HTTPException(status_code=500, detail=f"Error evaluating architecture: {str(e)}")
|
| 148 |
+
|
| 149 |
+
if __name__ == "__main__":
|
| 150 |
+
import uvicorn
|
| 151 |
+
uvicorn.run(app, host="0.0.0.0", port=7860) # Port 7860 is standard for HuggingFace Spaces
|