File size: 10,534 Bytes
c64a3e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import os
import pandas as pd
import json
from datetime import datetime
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
import requests

app = FastAPI(title="HTS to HSN Classifier", version="1.0.0")

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

class ClassificationRequest(BaseModel):
    hts_code_or_desc: str

class ClassificationResponse(BaseModel):
    HSN_Code: str | None
    HSN_Description: str | None
    Confidence: str
    Reasoning: str

class HuggingFaceInferenceClient:
    def __init__(self, model_name: str = "meta-llama/Meta-Llama-3-8B-Instruct", api_token: str = None):
        self.model_name = model_name
        self.api_token = api_token or ""
        if not self.api_token:
            raise ValueError("Hugging Face API token not provided")
        self.headers = {
            "Authorization": f"Bearer {self.api_token}",
            "Content-Type": "application/json"
        }
        # Fixed API URL - use the correct inference endpoint
        self.api_url = f"https://api-inference.huggingface.co/models/{self.model_name}"

    def invoke(self, prompt: str) -> str:
        # Fixed payload structure for Hugging Face Inference API
        payload = {
            "inputs": prompt,
            "parameters": {
                "max_new_tokens": 500,
                "temperature": 0.6,
                "top_p": 0.95,
                "return_full_text": False
            }
        }
        
        try:
            response = requests.post(self.api_url, json=payload, headers=self.headers, timeout=60)
            response.raise_for_status()
            data = response.json()
            
            # Handle different response formats
            if isinstance(data, list) and len(data) > 0:
                if "generated_text" in data[0]:
                    return data[0]["generated_text"]
                elif "text" in data[0]:
                    return data[0]["text"]
            elif isinstance(data, dict):
                if "generated_text" in data:
                    return data["generated_text"]
                elif "text" in data:
                    return data["text"]
            
            return str(data)
            
        except requests.exceptions.RequestException as e:
            print(f"API request failed: {e}")
            raise Exception(f"Hugging Face API error: {e}")
        except json.JSONDecodeError as e:
            print(f"JSON decode error: {e}")
            raise Exception(f"Invalid JSON response from API: {e}")


@app.on_event("startup")
async def startup_event():
    try:
        global vs_hts, vs_hsn, df_hts, df_hsn, llm_client, hts_code_col, hts_desc_col, hsn_code_col, hsn_desc_col
        
        hts_path = "data/Htsdata.xlsx"
        hsn_path = "data/HSN_SAC.xlsx"
        cached_hts_vector_path = "data/faiss_hts_store"
        cached_hsn_vector_path = "data/faiss_hsn_store"

        print("Loading HSN data from:", hsn_path)
        
        hts_code_col = "HTS Number"
        hts_desc_col = "Description"
        hsn_code_col = "HSN_CD"
        hsn_desc_col = "HSN_Description"

        df_hts = pd.read_excel(hts_path)
        df_hts.columns = df_hts.columns.str.strip()
        df_hsn = pd.read_excel(hsn_path)
        df_hsn.columns = df_hsn.columns.str.strip()
        df_hsn[hsn_code_col] = df_hsn[hsn_code_col].astype(str)

        # Initialize with correct model name
        llm_client = HuggingFaceInferenceClient(model_name="meta-llama/Meta-Llama-3-8B-Instruct")
        
        print("βœ… Application started successfully!")
        
    except Exception as e:
        print(f"❌ Startup error: {e}")
        raise e

@app.get("/health")
async def health_check():
    return {"status": "healthy", "timestamp": datetime.now().isoformat()}

def extract_structure(code: str):
    code = "".join(filter(str.isdigit, str(code)))
    return {
        "chapter": code[:4] if len(code) >= 4 else None,
        "heading": code[:6] if len(code) >= 6 else None,
        "hsn8": code[:8] if len(code) >= 8 else code,
        "full": code
    }

def extract_json_from_text(text: str) -> dict:
    """Extract JSON from text response, handling various formats."""
    text = text.strip()
    
    # Find JSON content between braces
    start_idx = text.find('{')
    end_idx = text.rfind('}')
    
    if start_idx != -1 and end_idx != -1 and start_idx < end_idx:
        json_str = text[start_idx:end_idx + 1]
        try:
            return json.loads(json_str)
        except json.JSONDecodeError:
            pass
    
    # If JSON extraction fails, try to parse key-value pairs
    try:
        lines = text.split('\n')
        result = {}
        for line in lines:
            if ':' in line:
                key, value = line.split(':', 1)
                key = key.strip().strip('"\'')
                value = value.strip().strip('",\'')
                if key in ['HSN_Code', 'HSN_Description', 'Confidence', 'Reasoning']:
                    result[key] = value
        
        if len(result) >= 2:  # At least some keys found
            return result
    except:
        pass
    
    return None

def map_hts_to_hsn(hts_code_or_desc: str):
    reasoning_parts = []

    if hts_code_or_desc.isdigit():
        struct = extract_structure(hts_code_or_desc)
        reasoning_parts.append(f"Input HTS code: {struct['full']}")

        hts_match = df_hts[df_hts[hts_code_col].astype(str).str.startswith(struct["chapter"])]
        hts_desc_list = hts_match[hts_desc_col].head(3).tolist() if not hts_match.empty else []
        hts_desc_text = "; ".join(hts_desc_list) if hts_desc_list else "No HTS description found."
        reasoning_parts.append(f"HTS Chapter {struct['chapter']}: {hts_desc_text}")

        hsn_match = df_hsn[df_hsn[hsn_code_col] == struct["hsn8"]]
        if not hsn_match.empty:
            best_match = hsn_match.iloc[0]
            reasoning_parts.append(f"Exact 8-digit HSN {struct['hsn8']} found.")
            return {
                "HSN_Code": best_match[hsn_code_col],
                "HSN_Description": best_match[hsn_desc_col],
                "Confidence": "High",
                "Reasoning": " ".join(reasoning_parts)
            }

        fallback_heading_match = df_hsn[df_hsn[hsn_code_col].str.startswith(struct["heading"])]
        if not fallback_heading_match.empty:
            fallback_heading = fallback_heading_match.iloc[0]
            reasoning_parts.append(f"No exact 8-digit HSN. Fallback heading {struct['heading']} found.")

            # Improved prompt with better formatting
            system_prompt = "You are an expert in Indian HSN classification. Respond only with valid JSON containing the keys: HSN_Code, HSN_Description, Confidence, Reasoning."
            
            user_prompt = f"""
Input HTS code: {struct['full']}
HTS Description: {hts_desc_text}
Fallback HSN heading: {fallback_heading[hsn_code_col]} - {fallback_heading[hsn_desc_col]}

Based on this information, provide the most appropriate 8-digit HSN code and description.

Required JSON format:
{{
    "HSN_Code": "XXXXXXXX",
    "HSN_Description": "description here",
    "Confidence": "High/Medium/Low",
    "Reasoning": "explanation here"
}}
"""

            full_prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n{system_prompt}<|eot_id|><|start_header_id|>user<|end_header_id|>\n{user_prompt}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n"

            try:
                llm_response = llm_client.invoke(full_prompt).strip()
                print(f"LLM Response: {llm_response}")  # Debug logging
                
                parsed_response = extract_json_from_text(llm_response)
                
                if parsed_response and all(key in parsed_response for key in ["HSN_Code", "HSN_Description"]):
                    # Ensure all required keys are present with defaults
                    return {
                        "HSN_Code": parsed_response.get("HSN_Code"),
                        "HSN_Description": parsed_response.get("HSN_Description"),
                        "Confidence": parsed_response.get("Confidence", "Medium"),
                        "Reasoning": parsed_response.get("Reasoning", "LLM classification")
                    }
                else:
                    print(f"Invalid LLM response format: {parsed_response}")
                    
            except Exception as e:
                print(f"LLM failed: {e}")
                
            # Fallback if LLM fails
            return {
                "HSN_Code": fallback_heading[hsn_code_col],
                "HSN_Description": fallback_heading[hsn_desc_col],
                "Confidence": "Medium",
                "Reasoning": " ".join(reasoning_parts) + " LLM failed, using fallback 6-digit heading."
            }

        chapter_match = df_hsn[df_hsn[hsn_code_col].str.startswith(struct["chapter"][:4])]
        if not chapter_match.empty:
            best_match = chapter_match.iloc[0]
            reasoning_parts.append(f"No heading match. Fallback to chapter {struct['chapter'][:4]}.")
            return {
                "HSN_Code": best_match[hsn_code_col],
                "HSN_Description": best_match[hsn_desc_col],
                "Confidence": "Low",
                "Reasoning": " ".join(reasoning_parts)
            }

        return {
            "HSN_Code": None,
            "HSN_Description": None,
            "Confidence": "Low",
            "Reasoning": " ".join(reasoning_parts) + " No HSN match found."
        }

    else:
        reasoning_parts.append("Input is description. Semantic search not implemented for Hugging Face deployment.")
        return {"HSN_Code": None, "HSN_Description": None, "Confidence": "Low",
                "Reasoning": "Description search not available in this deployment."}

@app.post("/classify", response_model=ClassificationResponse)
async def classify_hts(request: ClassificationRequest):
    try:
        result = map_hts_to_hsn(request.hts_code_or_desc)
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Classification error: {str(e)}")

@app.get("/")
async def root():
    return {"message": "HTS to HSN Classification API", "status": "running"}

if __name__ == "__main__":
    uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)