File size: 2,673 Bytes
9999cbe
 
 
7a4de2d
 
cfc25cc
8816efc
 
 
 
 
9999cbe
7a4de2d
9999cbe
 
 
 
 
 
 
 
 
efac331
 
9999cbe
7a4de2d
9999cbe
7a4de2d
 
 
 
 
 
 
 
 
 
 
7dc8aa2
de68b77
e7d131e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9999cbe
7a4de2d
9999cbe
7dc8aa2
9999cbe
 
 
 
 
7a4de2d
7dc8aa2
 
 
0b0cb23
 
 
 
 
 
 
7dc8aa2
9999cbe
7dc8aa2
 
 
 
 
 
9999cbe
7dc8aa2
 
 
 
 
 
 
 
7a4de2d
9999cbe
7a4de2d
9999cbe
 
 
7dc8aa2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# main.py
from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware
import requests
import os

from vector_index import VectorDB

vector_db = VectorDB.load("vector_db.pkl")


# ---------------------------
# FastAPI app
# ---------------------------
app = FastAPI(title="Smart AI Backend")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ---------------------------
# OpenRouter settings
# ---------------------------
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")

API_URL = "https://openrouter.ai/api/v1/chat/completions"

HEADERS = {
    "Authorization": f"Bearer {OPENROUTER_API_KEY}",
    "Content-Type": "application/json",
    "HTTP-Referer": "https://huggingface.co",
    "X-Title": "My Free AI App"
}

MODEL = "meta-llama/llama-3.3-70b-instruct:free"  # FREE & fast





# ---------------------------
# AI Personality (System Prompt)
# ---------------------------

SYSTEM_PROMPT = """
You are a Men's Fashion Counsellor and Style Consultant.

Your job is to help men dress better while respecting:
- their personality
- comfort
- body type
- budget
- confidence level
- Indian climate and lifestyle

Tone:
- Warm
- Friendly
- Practical
- Confidence-boosting
- Non-judgmental

Style principles:
- Prefer timeless, clean fashion over short-lived trends
- Explain WHY an outfit works
- Suggest realistic, wearable combinations
- Avoid over-complication
- Help the user feel confident, not pressured

When needed:
- Ask simple clarifying questions
- Give step-by-step guidance
"""






# ---------------------------
# Routes
# ---------------------------

@app.get("/")
def home():
    return {"message": "Backend is working"}

@app.get("/chat")
def chat(prompt: str = Query(...)):
    if not OPENROUTER_API_KEY:
        return {"error": "OPENROUTER_API_KEY not set in Hugging Face Secrets"}

    payload = {
        "model": MODEL,
        "messages": [
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": prompt}
        ]
    }

    try:
        response = requests.post(
            API_URL,
            headers=HEADERS,
            json=payload,
            timeout=30
        )

        if response.status_code != 200:
            return {
                "error": "OpenRouter API error",
                "status_code": response.status_code,
                "details": response.text
            }

        data = response.json()
        reply = data["choices"][0]["message"]["content"]

        return {"reply": reply}

    except Exception as e:
        return {"error": str(e)}