File size: 9,839 Bytes
74e6e67
 
 
9de3f69
74e6e67
 
816ea83
1a324e9
816ea83
 
 
d63378e
2200ab2
9de3f69
2200ab2
3ec2c5d
 
 
 
 
 
 
 
816ea83
2200ab2
9de3f69
2200ab2
 
9de3f69
 
 
 
2200ab2
 
d63378e
9de3f69
d63378e
 
9de3f69
d63378e
 
9de3f69
2a2334f
5a4adc9
 
d63378e
0a577a4
7426828
9de3f69
7426828
9de3f69
7426828
d7d2f96
9de3f69
 
 
7426828
 
 
 
d7d2f96
9de3f69
 
7426828
d7d2f96
9de3f69
 
7426828
 
 
9de3f69
7426828
9de3f69
7426828
d7d2f96
7426828
9de3f69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7d2f96
 
9de3f69
 
7426828
9de3f69
7426828
 
9de3f69
7426828
d7d2f96
9de3f69
 
 
 
 
 
 
7426828
d7d2f96
9de3f69
d7d2f96
 
9de3f69
d7d2f96
9de3f69
d7d2f96
9de3f69
 
 
7fc680b
9de3f69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fc680b
9de3f69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7d2f96
9de3f69
7426828
9de3f69
 
d7d2f96
9de3f69
d7d2f96
9de3f69
 
d7d2f96
 
 
9de3f69
 
 
 
 
 
 
 
 
 
 
 
 
05046a1
9de3f69
 
 
 
 
 
 
 
 
 
 
 
1a324e9
 
9de3f69
2a2334f
 
1a324e9
2a2334f
 
9de3f69
 
 
 
 
 
 
 
 
 
 
a4e3caf
3c164b2
 
 
1cbae7b
9de3f69
 
 
 
 
 
 
 
 
0a577a4
d63378e
9de3f69
6f2172d
9de3f69
7426828
9de3f69
7426828
 
d7d2f96
3ec2c5d
d7d2f96
3ec2c5d
d7d2f96
7426828
 
 
 
3ec2c5d
 
 
7426828
 
3ec2c5d
 
 
7fc680b
c402222
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
275
276
277
278
279
280
281
282
import gradio as gr
from transformers import pipeline
import requests
import os
import pandas as pd
import time
from dotenv import load_dotenv
from textblob import TextBlob

load_dotenv()
hf_token = os.getenv("HF_TOKEN")

# =========================
# ENV VARIABLES (use HF Secrets)
# =========================
client_id = "sb-cap1-3c4588e0trial-dev!t617058"
client_secret = "acbe78be-ead5-4b12-b3b4-32fdb27d0f5f$hFj-hDXxwHkNHC-CAvv-OKSr3KH96nLL4KqwIg7M8D8="
token_url = "https://3c4588e0trial.authentication.us10.hana.ondemand.com/oauth/token"
cap_service_url_customers = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/Customers?$top=2"
cap_service_url_products = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/Products?$top=2"
cap_service_url_saleorders = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/SalesOrders?$top=2"
cap_service_url_saleorderitems = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/SalesOrderItems?$top=2"


# =========================
# GLOBAL VARIABLES
# =========================
access_token = None
cached_customers = None
cached_products = None
cached_salesorders = None
cached_salesorderitems = None
last_refresh = 0

# =========================
# LOAD MODEL (once)
# =========================
print("Loading model...")

pipe = pipeline(
    "text-generation",
    # model="Qwen/Qwen2.5-0.5B-Instruct",
    model="Qwen/Qwen2.5-1.5B-Instruct",
    device="cpu",
    torch_dtype="auto"
)

# =========================
# TOKEN FUNCTION
# =========================
def generate_sap_xsuaa_token():
    global access_token

    print("Generating SAP token...")

    auth_response = requests.post(
        token_url,
        data={"grant_type": "client_credentials"},
        auth=(client_id, client_secret)
    )

    if auth_response.status_code != 200:
        print("Token Error:", auth_response.text)
        return None

    access_token = auth_response.json().get("access_token")
    print("Token generated!")
    return access_token

# =========================
# FETCH SAP DATA
# =========================
def fetch_sap_data():
    global access_token

    if not access_token:
        generate_sap_xsuaa_token()

    headers = {
        "Authorization": f"Bearer {access_token}",
        "Accept": "application/json"
    }

    res1 = requests.get(cap_service_url_customers, headers=headers)
    res2 = requests.get(cap_service_url_products, headers=headers)
    res3 = requests.get(cap_service_url_saleorders, headers=headers)
    res4 = requests.get(cap_service_url_saleorderitems, headers=headers)

    # Retry if token expired
    if res1.status_code in [400,401,403]:
        print("Token expired. Regenerating...")
        access_token = None
        generate_sap_xsuaa_token()

        headers["Authorization"] = f"Bearer {access_token}"
        res1 = requests.get(cap_service_url_customers, headers=headers)
        res2 = requests.get(cap_service_url_products, headers=headers)
        res3 = requests.get(cap_service_url_saleorders, headers=headers)
        res4 = requests.get(cap_service_url_saleorderitems, headers=headers)

    df_customers = pd.DataFrame(res1.json()["value"])
    df_products = pd.DataFrame(res2.json()["value"])
    df_saleorders = pd.DataFrame(res3.json()["value"])
    df_saleorderitems = pd.DataFrame(res4.json()["value"])

    # Keep only important columns
    df_customers = df_customers[["ID","name","country","industry"]]
    df_products = df_products[["ID","name","category","price","currency"]]
    df_saleorders = df_saleorders[["ID","customer_ID","orderDate","status"]]
    df_saleorderitems = df_saleorderitems[["ID","parent_ID","product_ID","quantity","netAmount"]]

    return df_customers, df_products, df_saleorders, df_saleorderitems

# =========================
# CACHE LOGIC
# =========================

def get_cached_data():
    global cached_customers, cached_products,cached_salesorders,cached_salesorderitems, last_refresh

    # Refresh every 5 minutes
    if time.time() - last_refresh > 3000 or cached_customers is None:
        print("Refreshing SAP data...")
        cached_customers, cached_products,cached_salesorders,cached_salesorderitems = fetch_sap_data()
        last_refresh = time.time()

    return cached_customers, cached_products,cached_salesorders,cached_salesorderitems

# =========================
# MAIN FUNCTION (LLM)
# =========================
# def generate_response(user_prompt):

#     try:
#         # Get cached SAP data
#         df_customers, df_products, df_saleorders, df_saleorderitems = get_cached_data()

#         # Reduce size (IMPORTANT)
#         # customers_text = str(df_customers)[:500]
#         # products_text = str(df_products)[:500]
#         # saleorders_text = str(df_saleorders)[:500]
#         # saleorderitems_text = str(df_saleorderitems)[:500]

#         customers_text = df_customers.to_string(index=False)
#         products_text = df_products.to_string(index=False)
#         saleorders_text = df_saleorders.to_string(index=False)
#         saleorderitems_text = df_saleorderitems.to_string(index=False)

#         # Build prompt

#         system_prompt = f"""
#         You are an intelligent Corporate SAP Assistant bot.
#         Your sole purpose is to answer the user's questions based strictly on the database records provided to you.
#         Customers Data: {customers_text}
#         Products Data: {products_text}
#         Sale orders Data: {saleorders_text}
#         Sale order items Data: {saleorderitems_text}
    
#         CRITICAL RULES:
#         1. NO HALLUCINATIONS: You must base your answer ONLY on the data provided. 
#         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."
#         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.
#         4. TONE: Be concise, highly professional, and helpful.
#         """
        
#         prompt = f"""
#         {system_prompt}        
#         User: {user_prompt}
#         Assistant:
#         """
       
#         # Generate response
#         result = pipe(
#             prompt,
#             max_new_tokens=100,
#             # temperature=0.3, # Temperature controls randomness
#             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
#             # repetition_penalty=1.1,
#             return_full_text=False # If return_full_text=False ensure that the model output contains only the newly generated text.
        
#         )

#         generated_text = result[0]["generated_text"]

#         # Clean output
#         response = generated_text.replace(prompt, "").strip()

#         return response

#     except Exception as e:
#         return f"Error: {str(e)}"



def generate_response(user_prompt):
    try:
        # Get cached SAP data
        df_customers, df_products, df_saleorders, df_saleorderitems = get_cached_data()

        # Reduce size
        customers_text = df_customers.to_string(index=False)
        products_text = df_products.to_string(index=False)
        saleorders_text = df_saleorders.to_string(index=False)
        saleorderitems_text = df_saleorderitems.to_string(index=False)

        # Build system prompt
        system_prompt = f"""
        Your purpose is to answer the user's questions based strictly on the database records provided to you.
        
        Customers Data: {customers_text}
        Products Data: {products_text}
        Sale orders Data: {saleorders_text}
        Sale order items Data: {saleorderitems_text}
        
        CRITICAL RULES:
        1. NO HALLUCINATIONS: You must base your answer ONLY on the data provided. 
        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."
        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.
        4. TONE: Be concise, highly professional, and helpful.
        """

        user_corrected_prompt = str(TextBlob(user_prompt).correct())
        
        messages = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_corrected_prompt}
        ]

        
        prompt = pipe.tokenizer.apply_chat_template(
            messages, 
            tokenize=False, 
            add_generation_prompt=True
        )

        # Generate response
        result = pipe(
            prompt,
            max_new_tokens=100,
            do_sample=True,
            temperature = 0.3,
            top_k = 3,
            top_p = 0.7,
            # use_cache = True,
            return_full_text=False 
        )
        
        generated_text = result[0]["generated_text"]
        
        # Clean output and strip any leftover end-of-turn tokens
        response = generated_text.replace(prompt, "").replace("<|im_end|>", "").strip()
        
        return response

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

        
# =========================
# GRADIO UI + API
# =========================
with gr.Blocks() as demo:

    user_input = gr.Textbox(label="User Question")

    output = gr.Textbox(label="Response")

    btn = gr.Button("Generate")

    btn.click(
        fn=generate_response,
        inputs=[user_input],
        outputs=output,
        api_name="predict"
    )

# REQUIRED for API exposure
demo.queue()

demo.launch()