Update app.py
Browse files
app.py
CHANGED
|
@@ -224,10 +224,92 @@ print("Loading model...")
|
|
| 224 |
pipe = pipeline(
|
| 225 |
"text-generation",
|
| 226 |
model="meta-llama/Llama-3.2-1B-Instruct",
|
| 227 |
-
device="cpu"
|
| 228 |
-
trust_remote_code=True
|
| 229 |
)
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
# =========================
|
| 232 |
# MAIN FUNCTION (LLM)
|
| 233 |
# =========================
|
|
@@ -280,4 +362,28 @@ def generate_response(user_prompt):
|
|
| 280 |
return generated_text.strip()
|
| 281 |
|
| 282 |
except Exception as e:
|
| 283 |
-
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
pipe = pipeline(
|
| 225 |
"text-generation",
|
| 226 |
model="meta-llama/Llama-3.2-1B-Instruct",
|
| 227 |
+
device="cpu"
|
|
|
|
| 228 |
)
|
| 229 |
|
| 230 |
+
|
| 231 |
+
# =========================
|
| 232 |
+
# TOKEN FUNCTION
|
| 233 |
+
# =========================
|
| 234 |
+
def generate_sap_xsuaa_token():
|
| 235 |
+
global access_token
|
| 236 |
+
|
| 237 |
+
print("Generating SAP token...")
|
| 238 |
+
|
| 239 |
+
auth_response = requests.post(
|
| 240 |
+
token_url,
|
| 241 |
+
data={"grant_type": "client_credentials"},
|
| 242 |
+
auth=(client_id, client_secret)
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
if auth_response.status_code != 200:
|
| 246 |
+
print("Token Error:", auth_response.text)
|
| 247 |
+
return None
|
| 248 |
+
|
| 249 |
+
access_token = auth_response.json().get("access_token")
|
| 250 |
+
print("Token generated!")
|
| 251 |
+
return access_token
|
| 252 |
+
|
| 253 |
+
# =========================
|
| 254 |
+
# FETCH SAP DATA
|
| 255 |
+
# =========================
|
| 256 |
+
def fetch_sap_data():
|
| 257 |
+
global access_token
|
| 258 |
+
|
| 259 |
+
if not access_token:
|
| 260 |
+
generate_sap_xsuaa_token()
|
| 261 |
+
|
| 262 |
+
headers = {
|
| 263 |
+
"Authorization": f"Bearer {access_token}",
|
| 264 |
+
"Accept": "application/json"
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
res1 = requests.get(cap_service_url_customers, headers=headers)
|
| 268 |
+
res2 = requests.get(cap_service_url_products, headers=headers)
|
| 269 |
+
res3 = requests.get(cap_service_url_saleorders, headers=headers)
|
| 270 |
+
res4 = requests.get(cap_service_url_saleorderitems, headers=headers)
|
| 271 |
+
|
| 272 |
+
# Retry if token expired
|
| 273 |
+
if res1.status_code in [400,401,403]:
|
| 274 |
+
print("Token expired. Regenerating...")
|
| 275 |
+
access_token = None
|
| 276 |
+
generate_sap_xsuaa_token()
|
| 277 |
+
|
| 278 |
+
headers["Authorization"] = f"Bearer {access_token}"
|
| 279 |
+
res1 = requests.get(cap_service_url_customers, headers=headers)
|
| 280 |
+
res2 = requests.get(cap_service_url_products, headers=headers)
|
| 281 |
+
res3 = requests.get(cap_service_url_saleorders, headers=headers)
|
| 282 |
+
res4 = requests.get(cap_service_url_saleorderitems, headers=headers)
|
| 283 |
+
|
| 284 |
+
df_customers = pd.DataFrame(res1.json()["value"])
|
| 285 |
+
df_products = pd.DataFrame(res2.json()["value"])
|
| 286 |
+
df_saleorders = pd.DataFrame(res3.json()["value"])
|
| 287 |
+
df_saleorderitems = pd.DataFrame(res4.json()["value"])
|
| 288 |
+
|
| 289 |
+
# Keep only important columns
|
| 290 |
+
df_customers = df_customers[["ID","name","country","industry"]]
|
| 291 |
+
df_products = df_products[["ID","name","category","price","currency"]]
|
| 292 |
+
df_saleorders = df_saleorders[["ID","customer_ID","orderDate","status"]]
|
| 293 |
+
df_saleorderitems = df_saleorderitems[["ID","parent_ID","product_ID","quantity","netAmount"]]
|
| 294 |
+
|
| 295 |
+
return df_customers, df_products, df_saleorders, df_saleorderitems
|
| 296 |
+
|
| 297 |
+
# =========================
|
| 298 |
+
# CACHE LOGIC
|
| 299 |
+
# =========================
|
| 300 |
+
|
| 301 |
+
def get_cached_data():
|
| 302 |
+
global cached_customers, cached_products,cached_salesorders,cached_salesorderitems, last_refresh
|
| 303 |
+
|
| 304 |
+
# Refresh every 5 minutes
|
| 305 |
+
if time.time() - last_refresh > 3000 or cached_customers is None:
|
| 306 |
+
print("Refreshing SAP data...")
|
| 307 |
+
cached_customers, cached_products,cached_salesorders,cached_salesorderitems = fetch_sap_data()
|
| 308 |
+
last_refresh = time.time()
|
| 309 |
+
|
| 310 |
+
return cached_customers, cached_products,cached_salesorders,cached_salesorderitems
|
| 311 |
+
|
| 312 |
+
|
| 313 |
# =========================
|
| 314 |
# MAIN FUNCTION (LLM)
|
| 315 |
# =========================
|
|
|
|
| 362 |
return generated_text.strip()
|
| 363 |
|
| 364 |
except Exception as e:
|
| 365 |
+
return f"Error: {str(e)}"
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
# =========================
|
| 369 |
+
# GRADIO UI + API
|
| 370 |
+
# =========================
|
| 371 |
+
with gr.Blocks() as demo:
|
| 372 |
+
|
| 373 |
+
user_input = gr.Textbox(label="User Question")
|
| 374 |
+
|
| 375 |
+
output = gr.Textbox(label="Response")
|
| 376 |
+
|
| 377 |
+
btn = gr.Button("Generate")
|
| 378 |
+
|
| 379 |
+
btn.click(
|
| 380 |
+
fn=generate_response,
|
| 381 |
+
inputs=[user_input],
|
| 382 |
+
outputs=output,
|
| 383 |
+
api_name="predict"
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
# REQUIRED for API exposure
|
| 387 |
+
demo.queue()
|
| 388 |
+
|
| 389 |
+
demo.launch()
|