Files changed (1) hide show
  1. app.py +281 -0
app.py ADDED
@@ -0,0 +1,281 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import requests
4
+ import os
5
+ import pandas as pd
6
+ import time
7
+ from dotenv import load_dotenv
8
+
9
+ load_dotenv()
10
+ hf_token = os.getenv("HF_TOKEN")
11
+
12
+ # =========================
13
+ # ENV VARIABLES (use HF Secrets)
14
+ # =========================
15
+ client_id = "sb-cap1-3c4588e0trial-dev!t617058"
16
+ client_secret = "acbe78be-ead5-4b12-b3b4-32fdb27d0f5f$hFj-hDXxwHkNHC-CAvv-OKSr3KH96nLL4KqwIg7M8D8="
17
+ token_url = "https://3c4588e0trial.authentication.us10.hana.ondemand.com/oauth/token"
18
+ cap_service_url_customers = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/Customers?$top=2"
19
+ cap_service_url_products = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/Products?$top=2"
20
+ cap_service_url_saleorders = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/SalesOrders?$top=2"
21
+ cap_service_url_saleorderitems = "https://3c4588e0trial-dev-cap1-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/sales/SalesOrderItems?$top=2"
22
+
23
+
24
+ # =========================
25
+ # GLOBAL VARIABLES
26
+ # =========================
27
+ access_token = None
28
+ cached_customers = None
29
+ cached_products = None
30
+ cached_salesorders = None
31
+ cached_salesorderitems = None
32
+ last_refresh = 0
33
+
34
+ # =========================
35
+ # LOAD MODEL (once)
36
+ # =========================
37
+ print("Loading model...")
38
+
39
+ pipe = pipeline(
40
+ "text-generation",
41
+ # model="Qwen/Qwen2.5-0.5B-Instruct",
42
+ # model="Qwen/Qwen2.5-1.5B-Instruct",
43
+ model = "google/gemma-2-2b-it",
44
+ device="cpu",
45
+ torch_dtype="auto"
46
+ )
47
+
48
+ # =========================
49
+ # TOKEN FUNCTION
50
+ # =========================
51
+ def generate_sap_xsuaa_token():
52
+ global access_token
53
+
54
+ print("Generating SAP token...")
55
+
56
+ auth_response = requests.post(
57
+ token_url,
58
+ data={"grant_type": "client_credentials"},
59
+ auth=(client_id, client_secret)
60
+ )
61
+
62
+ if auth_response.status_code != 200:
63
+ print("Token Error:", auth_response.text)
64
+ return None
65
+
66
+ access_token = auth_response.json().get("access_token")
67
+ print("Token generated!")
68
+ return access_token
69
+
70
+ # =========================
71
+ # FETCH SAP DATA
72
+ # =========================
73
+ def fetch_sap_data():
74
+ global access_token
75
+
76
+ if not access_token:
77
+ generate_sap_xsuaa_token()
78
+
79
+ headers = {
80
+ "Authorization": f"Bearer {access_token}",
81
+ "Accept": "application/json"
82
+ }
83
+
84
+ res1 = requests.get(cap_service_url_customers, headers=headers)
85
+ res2 = requests.get(cap_service_url_products, headers=headers)
86
+ res3 = requests.get(cap_service_url_saleorders, headers=headers)
87
+ res4 = requests.get(cap_service_url_saleorderitems, headers=headers)
88
+
89
+ # Retry if token expired
90
+ if res1.status_code in [400,401,403]:
91
+ print("Token expired. Regenerating...")
92
+ access_token = None
93
+ generate_sap_xsuaa_token()
94
+
95
+ headers["Authorization"] = f"Bearer {access_token}"
96
+ res1 = requests.get(cap_service_url_customers, headers=headers)
97
+ res2 = requests.get(cap_service_url_products, headers=headers)
98
+ res3 = requests.get(cap_service_url_saleorders, headers=headers)
99
+ res4 = requests.get(cap_service_url_saleorderitems, headers=headers)
100
+
101
+ df_customers = pd.DataFrame(res1.json()["value"])
102
+ df_products = pd.DataFrame(res2.json()["value"])
103
+ df_saleorders = pd.DataFrame(res3.json()["value"])
104
+ df_saleorderitems = pd.DataFrame(res4.json()["value"])
105
+
106
+ # Keep only important columns
107
+ df_customers = df_customers[["ID","name","country","industry"]]
108
+ df_products = df_products[["ID","name","category","price","currency"]]
109
+ df_saleorders = df_saleorders[["ID","customer_ID","orderDate","status"]]
110
+ df_saleorderitems = df_saleorderitems[["ID","parent_ID","product_ID","quantity","netAmount"]]
111
+
112
+ return df_customers, df_products, df_saleorders, df_saleorderitems
113
+
114
+ # =========================
115
+ # CACHE LOGIC
116
+ # =========================
117
+
118
+ def get_cached_data():
119
+ global cached_customers, cached_products,cached_salesorders,cached_salesorderitems, last_refresh
120
+
121
+ # Refresh every 5 minutes
122
+ if time.time() - last_refresh > 3000 or cached_customers is None:
123
+ print("Refreshing SAP data...")
124
+ cached_customers, cached_products,cached_salesorders,cached_salesorderitems = fetch_sap_data()
125
+ last_refresh = time.time()
126
+
127
+ return cached_customers, cached_products,cached_salesorders,cached_salesorderitems
128
+
129
+ # =========================
130
+ # MAIN FUNCTION (LLM)
131
+ # =========================
132
+ # def generate_response(user_prompt):
133
+
134
+ # try:
135
+ # # Get cached SAP data
136
+ # df_customers, df_products, df_saleorders, df_saleorderitems = get_cached_data()
137
+
138
+ # # Reduce size (IMPORTANT)
139
+ # # customers_text = str(df_customers)[:500]
140
+ # # products_text = str(df_products)[:500]
141
+ # # saleorders_text = str(df_saleorders)[:500]
142
+ # # saleorderitems_text = str(df_saleorderitems)[:500]
143
+
144
+ # customers_text = df_customers.to_string(index=False)
145
+ # products_text = df_products.to_string(index=False)
146
+ # saleorders_text = df_saleorders.to_string(index=False)
147
+ # saleorderitems_text = df_saleorderitems.to_string(index=False)
148
+
149
+ # # Build prompt
150
+
151
+ # system_prompt = f"""
152
+ # You are an intelligent Corporate SAP Assistant bot.
153
+ # Your sole purpose is to answer the user's questions based strictly on the database records provided to you.
154
+ # Customers Data: {customers_text}
155
+ # Products Data: {products_text}
156
+ # Sale orders Data: {saleorders_text}
157
+ # Sale order items Data: {saleorderitems_text}
158
+
159
+ # CRITICAL RULES:
160
+ # 1. NO HALLUCINATIONS: You must base your answer ONLY on the data provided.
161
+ # 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."
162
+ # 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.
163
+ # 4. TONE: Be concise, highly professional, and helpful.
164
+ # """
165
+
166
+ # prompt = f"""
167
+ # {system_prompt}
168
+ # User: {user_prompt}
169
+ # Assistant:
170
+ # """
171
+
172
+ # # Generate response
173
+ # result = pipe(
174
+ # prompt,
175
+ # max_new_tokens=100,
176
+ # # temperature=0.3, # Temperature controls randomness
177
+ # 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
178
+ # # repetition_penalty=1.1,
179
+ # return_full_text=False # If return_full_text=False ensure that the model output contains only the newly generated text.
180
+
181
+ # )
182
+
183
+ # generated_text = result[0]["generated_text"]
184
+
185
+ # # Clean output
186
+ # response = generated_text.replace(prompt, "").strip()
187
+
188
+ # return response
189
+
190
+ # except Exception as e:
191
+ # return f"Error: {str(e)}"
192
+
193
+
194
+
195
+ def generate_response(user_prompt):
196
+ try:
197
+ # Get cached SAP data
198
+ df_customers, df_products, df_saleorders, df_saleorderitems = get_cached_data()
199
+
200
+ # Reduce size
201
+ customers_text = df_customers.to_string(index=False)
202
+ products_text = df_products.to_string(index=False)
203
+ saleorders_text = df_saleorders.to_string(index=False)
204
+ saleorderitems_text = df_saleorderitems.to_string(index=False)
205
+
206
+ # Build system prompt
207
+ system_prompt = f"""
208
+ Your purpose is to answer the user's questions based strictly on the database records provided to you.
209
+
210
+ Customers Data: {customers_text}
211
+ Products Data: {products_text}
212
+ Sale orders Data: {saleorders_text}
213
+ Sale order items Data: {saleorderitems_text}
214
+
215
+ CRITICAL RULES:
216
+ 1. NO HALLUCINATIONS: You must base your answer ONLY on the data provided.
217
+ 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."
218
+ 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.
219
+ 4. TONE: Be concise, highly professional, and helpful.
220
+ """
221
+
222
+ # Format for Qwen/Qwen2.5-1.5B-Instruct
223
+ # messages = [
224
+ # {"role": "system", "content": system_prompt},
225
+ # {"role": "user", "content": user_prompt}
226
+ # ]
227
+
228
+ # Format for google/gemma-2-2b-it
229
+ combined_prompt = f"{system_prompt}\n\nUser Question:\n{user_prompt}"
230
+ messages = [{"role": "user", "content": combined_prompt}]
231
+
232
+ prompt = pipe.tokenizer.apply_chat_template(
233
+ messages,
234
+ tokenize=False,
235
+ add_generation_prompt=True
236
+ )
237
+
238
+ # Generate response
239
+ result = pipe(
240
+ prompt,
241
+ max_new_tokens=100,
242
+ do_sample=True,
243
+ temperature = 0.3,
244
+ top_k = 3,
245
+ top_p = 0.7,
246
+ return_full_text=False
247
+ )
248
+
249
+ generated_text = result[0]["generated_text"]
250
+
251
+ # Clean output and strip any leftover end-of-turn tokens
252
+ response = generated_text.replace(prompt, "").replace("<|im_end|>", "").strip()
253
+
254
+ return response
255
+
256
+ except Exception as e:
257
+ return f"Error: {str(e)}"
258
+
259
+
260
+ # =========================
261
+ # GRADIO UI + API
262
+ # =========================
263
+ with gr.Blocks() as demo:
264
+
265
+ user_input = gr.Textbox(label="User Question")
266
+
267
+ output = gr.Textbox(label="Response")
268
+
269
+ btn = gr.Button("Generate")
270
+
271
+ btn.click(
272
+ fn=generate_response,
273
+ inputs=[user_input],
274
+ outputs=output,
275
+ api_name="predict"
276
+ )
277
+
278
+ # REQUIRED for API exposure
279
+ demo.queue()
280
+
281
+ demo.launch()