Update app.py
Browse files
app.py
CHANGED
|
@@ -209,7 +209,6 @@
|
|
| 209 |
|
| 210 |
# ====================================================================================================================
|
| 211 |
|
| 212 |
-
import torch
|
| 213 |
import gradio as gr
|
| 214 |
from transformers import pipeline
|
| 215 |
import requests
|
|
@@ -217,17 +216,38 @@ import os
|
|
| 217 |
import pandas as pd
|
| 218 |
import time
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
# =========================
|
| 221 |
# LOAD MODEL (once)
|
| 222 |
# =========================
|
| 223 |
print("Loading model...")
|
|
|
|
| 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 |
# =========================
|
|
@@ -309,57 +329,66 @@ def get_cached_data():
|
|
| 309 |
|
| 310 |
return cached_customers, cached_products,cached_salesorders,cached_salesorderitems
|
| 311 |
|
| 312 |
-
|
| 313 |
# =========================
|
| 314 |
# MAIN FUNCTION (LLM)
|
| 315 |
# =========================
|
| 316 |
def generate_response(user_prompt):
|
|
|
|
| 317 |
try:
|
|
|
|
| 318 |
df_customers, df_products, df_saleorders, df_saleorderitems = get_cached_data()
|
| 319 |
|
| 320 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
customers_text = df_customers.to_string(index=False)
|
| 322 |
products_text = df_products.to_string(index=False)
|
| 323 |
saleorders_text = df_saleorders.to_string(index=False)
|
| 324 |
saleorderitems_text = df_saleorderitems.to_string(index=False)
|
| 325 |
|
| 326 |
-
|
| 327 |
-
Your sole purpose is to answer the user's questions based strictly on the database records provided below.
|
| 328 |
-
Never hallucinate or add information not present in the data.
|
| 329 |
-
|
| 330 |
-
Customers Data:
|
| 331 |
-
{customers_text}
|
| 332 |
-
|
| 333 |
-
Products Data:
|
| 334 |
-
{products_text}
|
| 335 |
-
|
| 336 |
-
Sales Orders Data:
|
| 337 |
-
{saleorders_text}
|
| 338 |
-
|
| 339 |
-
Sales Order Items Data:
|
| 340 |
-
{saleorderitems_text}
|
| 341 |
-
|
| 342 |
-
CRITICAL RULES:
|
| 343 |
-
1. Answer ONLY using the data above.
|
| 344 |
-
2. If the data does not contain the answer, reply: "I could not find that information in the current SAP database."
|
| 345 |
-
3. Always respond in clean Markdown. Use **bold** for key values and bullet points for lists.
|
| 346 |
-
4. Be concise, professional, and accurate."""
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
messages = [
|
| 350 |
-
{"role": "system", "content": system_prompt},
|
| 351 |
-
{"role": "user", "content": user_prompt}
|
| 352 |
-
]
|
| 353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
result = pipe(
|
| 355 |
-
|
| 356 |
-
max_new_tokens=
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
| 359 |
)
|
| 360 |
|
| 361 |
generated_text = result[0]["generated_text"]
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
except Exception as e:
|
| 365 |
return f"Error: {str(e)}"
|
|
@@ -386,4 +415,4 @@ with gr.Blocks() as demo:
|
|
| 386 |
# REQUIRED for API exposure
|
| 387 |
demo.queue()
|
| 388 |
|
| 389 |
-
demo.launch()
|
|
|
|
| 209 |
|
| 210 |
# ====================================================================================================================
|
| 211 |
|
|
|
|
| 212 |
import gradio as gr
|
| 213 |
from transformers import pipeline
|
| 214 |
import requests
|
|
|
|
| 216 |
import pandas as pd
|
| 217 |
import time
|
| 218 |
|
| 219 |
+
# =========================
|
| 220 |
+
# ENV VARIABLES (use HF Secrets)
|
| 221 |
+
# =========================
|
| 222 |
+
client_id = "sb-cap1-3c4588e0trial-dev!t617058"
|
| 223 |
+
client_secret = "acbe78be-ead5-4b12-b3b4-32fdb27d0f5f$hFj-hDXxwHkNHC-CAvv-OKSr3KH96nLL4KqwIg7M8D8="
|
| 224 |
+
token_url = "https://3c4588e0trial.authentication.us10.hana.ondemand.com/oauth/token"
|
| 225 |
+
cap_service_url_customers = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/Customers"
|
| 226 |
+
cap_service_url_products = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/Products"
|
| 227 |
+
cap_service_url_saleorders = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/SalesOrders"
|
| 228 |
+
cap_service_url_saleorderitems = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/SalesOrderItems"
|
| 229 |
+
|
| 230 |
+
# =========================
|
| 231 |
+
# GLOBAL VARIABLES
|
| 232 |
+
# =========================
|
| 233 |
+
access_token = None
|
| 234 |
+
cached_customers = None
|
| 235 |
+
cached_products = None
|
| 236 |
+
cached_salesorders = None
|
| 237 |
+
cached_salesorderitems = None
|
| 238 |
+
last_refresh = 0
|
| 239 |
+
|
| 240 |
# =========================
|
| 241 |
# LOAD MODEL (once)
|
| 242 |
# =========================
|
| 243 |
print("Loading model...")
|
| 244 |
+
|
| 245 |
pipe = pipeline(
|
| 246 |
"text-generation",
|
| 247 |
+
model="meta-llama/Llama-3.2-1B-Instruct",
|
| 248 |
device="cpu"
|
| 249 |
)
|
| 250 |
|
|
|
|
| 251 |
# =========================
|
| 252 |
# TOKEN FUNCTION
|
| 253 |
# =========================
|
|
|
|
| 329 |
|
| 330 |
return cached_customers, cached_products,cached_salesorders,cached_salesorderitems
|
| 331 |
|
|
|
|
| 332 |
# =========================
|
| 333 |
# MAIN FUNCTION (LLM)
|
| 334 |
# =========================
|
| 335 |
def generate_response(user_prompt):
|
| 336 |
+
|
| 337 |
try:
|
| 338 |
+
# Get cached SAP data
|
| 339 |
df_customers, df_products, df_saleorders, df_saleorderitems = get_cached_data()
|
| 340 |
|
| 341 |
+
# Reduce size (IMPORTANT)
|
| 342 |
+
# customers_text = str(df_customers)[:500]
|
| 343 |
+
# products_text = str(df_products)[:500]
|
| 344 |
+
# saleorders_text = str(df_saleorders)[:500]
|
| 345 |
+
# saleorderitems_text = str(df_saleorderitems)[:500]
|
| 346 |
+
|
| 347 |
customers_text = df_customers.to_string(index=False)
|
| 348 |
products_text = df_products.to_string(index=False)
|
| 349 |
saleorders_text = df_saleorders.to_string(index=False)
|
| 350 |
saleorderitems_text = df_saleorderitems.to_string(index=False)
|
| 351 |
|
| 352 |
+
# Build prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
+
system_prompt = f"""
|
| 355 |
+
You are an intelligent Corporate SAP Assistant bot.
|
| 356 |
+
Your sole purpose is to answer the user's questions based strictly on the database records provided to you.
|
| 357 |
+
Customers Data: {customers_text}
|
| 358 |
+
Products Data: {products_text}
|
| 359 |
+
Sale orders Data: {saleorders_text}
|
| 360 |
+
Sale order items Data: {saleorderitems_text}
|
| 361 |
+
|
| 362 |
+
CRITICAL RULES:
|
| 363 |
+
1. NO HALLUCINATIONS: You must base your answer ONLY on the data provided.
|
| 364 |
+
2. MISSING DATA: If the provided data does not contain the answer, do not guess. Say: "I could not find that information in the current SAP database."
|
| 365 |
+
3. FORMATTING: You must output your response in Markdown. Use bold text for important nouns and bullet points for lists to make it easy to read.
|
| 366 |
+
4. TONE: Be concise, highly professional, and helpful.
|
| 367 |
+
"""
|
| 368 |
+
|
| 369 |
+
prompt = f"""
|
| 370 |
+
{system_prompt}
|
| 371 |
+
User: {user_prompt}
|
| 372 |
+
Assistant:
|
| 373 |
+
"""
|
| 374 |
+
|
| 375 |
+
# Generate response
|
| 376 |
result = pipe(
|
| 377 |
+
prompt,
|
| 378 |
+
max_new_tokens=100,
|
| 379 |
+
# temperature=0.3, # Temperature controls randomness
|
| 380 |
+
do_sample=False, # controls HOW the next word is selected. If False then Always picks most probable next word and No randomness in answer. And if you use True then Picks from multiple possible words
|
| 381 |
+
# repetition_penalty=1.1,
|
| 382 |
+
return_full_text=False # If return_full_text=False ensure that the model output contains only the newly generated text.
|
| 383 |
+
|
| 384 |
)
|
| 385 |
|
| 386 |
generated_text = result[0]["generated_text"]
|
| 387 |
+
|
| 388 |
+
# Clean output
|
| 389 |
+
response = generated_text.replace(prompt, "").strip()
|
| 390 |
+
|
| 391 |
+
return response
|
| 392 |
|
| 393 |
except Exception as e:
|
| 394 |
return f"Error: {str(e)}"
|
|
|
|
| 415 |
# REQUIRED for API exposure
|
| 416 |
demo.queue()
|
| 417 |
|
| 418 |
+
demo.launch()
|