PD03 commited on
Commit
c5c381b
·
verified ·
1 Parent(s): ef36ca7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -407
app.py CHANGED
@@ -1,411 +1,83 @@
1
  import gradio as gr
2
-
3
- def submit_action(*inputs):
4
- return (
5
- f"<div style='padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 16px; margin-top: 20px;'>"
6
- f"<div style='color: white; font-size: 1.3em; font-weight: 600; margin-bottom: 8px;'>🎉 Registration Submitted Successfully!</div>"
7
- f"<div style='color: rgba(255,255,255,0.9); font-size: 1.1em; margin-bottom: 16px;'>Welcome aboard, <span style='font-weight: 600;'>{inputs[0]}</span>!</div>"
8
- f"<div style='display: flex; gap: 12px; flex-wrap: wrap;'>"
9
- f"<span style='background: rgba(34, 197, 94, 0.2); color: #22c55e; padding: 8px 16px; border-radius: 24px; font-size: 0.9em; font-weight: 500; border: 1px solid rgba(34, 197, 94, 0.3);'>✓ Verified</span>"
10
- f"<span style='background: rgba(245, 158, 11, 0.2); color: #f59e0b; padding: 8px 16px; border-radius: 24px; font-size: 0.9em; font-weight: 500; border: 1px solid rgba(245, 158, 11, 0.3);'>⏳ Approval Pending</span>"
11
- f"</div>"
12
- f"</div>"
13
- )
14
-
15
- with gr.Blocks(css="""
16
- @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');
17
-
18
- * {
19
- font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif !important;
20
- }
21
-
22
- .gradio-container {
23
- background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
24
- min-height: 100vh;
25
- padding: 2rem 1rem;
26
- }
27
-
28
- .main-card {
29
- max-width: 1100px;
30
- margin: 0 auto;
31
- background: rgba(255, 255, 255, 0.95);
32
- backdrop-filter: blur(20px);
33
- border-radius: 24px;
34
- box-shadow: 0 32px 64px rgba(0, 0, 0, 0.1);
35
- padding: 0;
36
- border: 1px solid rgba(255, 255, 255, 0.2);
37
- overflow: hidden;
38
- }
39
-
40
- .header-section {
41
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
42
- padding: 40px 48px;
43
- color: white;
44
- position: relative;
45
- overflow: hidden;
46
- }
47
-
48
- .header-section::before {
49
- content: '';
50
- position: absolute;
51
- top: -50%;
52
- right: -50%;
53
- width: 200%;
54
- height: 200%;
55
- background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
56
- animation: float 6s ease-in-out infinite;
57
- }
58
-
59
- @keyframes float {
60
- 0%, 100% { transform: translateY(0px) rotate(0deg); }
61
- 50% { transform: translateY(-20px) rotate(180deg); }
62
- }
63
-
64
- .step-indicator {
65
- display: flex;
66
- align-items: center;
67
- gap: 16px;
68
- margin-bottom: 32px;
69
- position: relative;
70
- z-index: 1;
71
- }
72
-
73
- .step {
74
- display: flex;
75
- align-items: center;
76
- gap: 8px;
77
- padding: 8px 16px;
78
- border-radius: 20px;
79
- font-size: 0.9em;
80
- font-weight: 500;
81
- transition: all 0.3s ease;
82
- }
83
-
84
- .step.active {
85
- background: rgba(255, 255, 255, 0.2);
86
- color: white;
87
- font-weight: 600;
88
- }
89
-
90
- .step.inactive {
91
- color: rgba(255, 255, 255, 0.6);
92
- }
93
-
94
- .step-arrow {
95
- color: rgba(255, 255, 255, 0.4);
96
- font-size: 1.2em;
97
- }
98
-
99
- .main-title {
100
- font-size: 2.5em;
101
- font-weight: 800;
102
- margin-bottom: 8px;
103
- position: relative;
104
- z-index: 1;
105
- text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
106
- }
107
-
108
- .subtitle {
109
- font-size: 1.2em;
110
- color: rgba(255, 255, 255, 0.9);
111
- font-weight: 400;
112
- position: relative;
113
- z-index: 1;
114
- }
115
-
116
- .form-content {
117
- padding: 48px;
118
- }
119
-
120
- .section-title {
121
- font-size: 1.3em;
122
- font-weight: 700;
123
- color: #1f2937;
124
- margin-bottom: 24px;
125
- margin-top: 40px;
126
- position: relative;
127
- padding-left: 16px;
128
- }
129
-
130
- .section-title::before {
131
- content: '';
132
- position: absolute;
133
- left: 0;
134
- top: 50%;
135
- transform: translateY(-50%);
136
- width: 4px;
137
- height: 24px;
138
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
139
- border-radius: 2px;
140
- }
141
-
142
- .section-title:first-of-type {
143
- margin-top: 0;
144
- }
145
-
146
- .input-group {
147
- display: grid;
148
- grid-template-columns: 1fr 1fr;
149
- gap: 32px;
150
- margin-bottom: 32px;
151
- }
152
-
153
- .input-wrapper {
154
- position: relative;
155
- }
156
-
157
- .verification-badge {
158
- display: inline-flex;
159
- align-items: center;
160
- gap: 6px;
161
- padding: 6px 12px;
162
- border-radius: 20px;
163
- font-size: 0.85em;
164
- font-weight: 500;
165
- margin-left: 8px;
166
- transition: all 0.3s ease;
167
- }
168
-
169
- .badge-verified {
170
- background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
171
- color: #065f46;
172
- border: 1px solid #a7f3d0;
173
- }
174
-
175
- .badge-pending {
176
- background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
177
- color: #92400e;
178
- border: 1px solid #fde68a;
179
- }
180
-
181
- .status-section {
182
- background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
183
- padding: 24px;
184
- border-radius: 16px;
185
- margin: 24px 0;
186
- border: 1px solid #e2e8f0;
187
- }
188
-
189
- .status-badges {
190
- display: flex;
191
- gap: 12px;
192
- flex-wrap: wrap;
193
- margin-bottom: 16px;
194
- }
195
-
196
- .access-level {
197
- color: #64748b;
198
- font-size: 0.95em;
199
- font-weight: 500;
200
- }
201
-
202
- .summary-table {
203
- width: 100%;
204
- border-collapse: collapse;
205
- margin-top: 16px;
206
- }
207
-
208
- .summary-table td {
209
- padding: 12px 0;
210
- border-bottom: 1px solid #e2e8f0;
211
- font-size: 0.95em;
212
- }
213
-
214
- .summary-table td:first-child {
215
- color: #64748b;
216
- font-weight: 500;
217
- width: 180px;
218
- }
219
-
220
- .summary-table td:last-child {
221
- color: #1f2937;
222
- font-weight: 600;
223
- }
224
-
225
- .submit-btn {
226
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
227
- color: white;
228
- border: none;
229
- padding: 16px 32px;
230
- border-radius: 12px;
231
- font-size: 1.1em;
232
- font-weight: 600;
233
- cursor: pointer;
234
- transition: all 0.3s ease;
235
- box-shadow: 0 8px 24px rgba(102, 126, 234, 0.3);
236
- margin-top: 32px;
237
- width: 100%;
238
- }
239
-
240
- .submit-btn:hover {
241
- transform: translateY(-2px);
242
- box-shadow: 0 12px 32px rgba(102, 126, 234, 0.4);
243
- }
244
-
245
- /* Gradio component styling */
246
- .gradio-textbox, .gradio-dropdown {
247
- border-radius: 12px !important;
248
- border: 2px solid #e2e8f0 !important;
249
- transition: all 0.3s ease !important;
250
- }
251
-
252
- .gradio-textbox:focus, .gradio-dropdown:focus {
253
- border-color: #667eea !important;
254
- box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
255
- }
256
-
257
- .gradio-textbox input, .gradio-dropdown select {
258
- font-size: 0.95em !important;
259
- padding: 12px 16px !important;
260
- }
261
-
262
- label {
263
- font-weight: 600 !important;
264
- color: #374151 !important;
265
- margin-bottom: 8px !important;
266
- font-size: 0.9em !important;
267
- }
268
-
269
- @media (max-width: 768px) {
270
- .main-card {
271
- margin: 0 16px;
272
- border-radius: 16px;
273
- }
274
-
275
- .header-section {
276
- padding: 32px 24px;
277
- }
278
-
279
- .form-content {
280
- padding: 32px 24px;
281
  }
282
-
283
- .input-group {
284
- grid-template-columns: 1fr;
285
- gap: 24px;
286
- }
287
-
288
- .main-title {
289
- font-size: 2em;
290
- }
291
-
292
- .step-indicator {
293
- flex-wrap: wrap;
294
- gap: 8px;
295
- }
296
- }
297
- """) as demo:
298
- with gr.Group(elem_classes="main-card"):
299
- # Header Section
300
- gr.HTML("""
301
- <div class='header-section'>
302
- <div class='step-indicator'>
303
- <div class='step active'>
304
- <span>1</span>
305
- <span>Onboarding</span>
306
- </div>
307
- <div class='step-arrow'>→</div>
308
- <div class='step inactive'>
309
- <span>2</span>
310
- <span>Documents</span>
311
- </div>
312
- <div class='step-arrow'>→</div>
313
- <div class='step inactive'>
314
- <span>3</span>
315
- <span>Complete</span>
316
- </div>
317
- </div>
318
- <div class='main-title'>Business Partner Registration</div>
319
- <div class='subtitle'>Auto Digital Public Infrastructure Platform</div>
320
- </div>
321
- """)
322
-
323
- # Form Content
324
- with gr.Group(elem_classes="form-content"):
325
- # Identification Section
326
- gr.HTML("<div class='section-title'>🏢 Company Identification</div>")
327
- with gr.Row(elem_classes="input-group"):
328
- legal_name = gr.Textbox(label="Legal Entity Name", value="ABC Enterprises")
329
- legal_type = gr.Dropdown(choices=["OEM", "Supplier", "Distributor"], label="Legal Entity Type", value="OEM")
330
-
331
- # Location Section
332
- gr.HTML("<div class='section-title'>📍 Business Location</div>")
333
- with gr.Row(elem_classes="input-group"):
334
- with gr.Column():
335
- country = gr.Dropdown(choices=["India"], value="India", label="Country")
336
- state = gr.Dropdown(choices=["Haryana", "Karnataka"], value="Haryana", label="State")
337
- city = gr.Dropdown(choices=["Faridabad", "Bangalore"], value="Faridabad", label="City")
338
- with gr.Column():
339
- plant_address = gr.Textbox(label="Plant Address", placeholder="Enter your complete plant address...", lines=2)
340
- postal_code = gr.Textbox(label="Postal Code", value="121002")
341
-
342
- # Legal Identifiers Section
343
- gr.HTML("<div class='section-title'>📋 Legal Identifiers</div>")
344
- with gr.Row(elem_classes="input-group"):
345
- with gr.Column():
346
- with gr.Row():
347
- gstin = gr.Textbox(label="GSTIN", value="22AAAAA0000A1Z5")
348
- gr.HTML("<span class='verification-badge badge-verified'>✓ Verified</span>")
349
- with gr.Row():
350
- pan = gr.Textbox(label="PAN", value="AAAAA0000A")
351
- gr.HTML("<span class='verification-badge badge-verified'>✓ Verified</span>")
352
- with gr.Row():
353
- cin = gr.Textbox(label="CIN", value="L01631KA2010PTC096843")
354
- gr.HTML("<span class='verification-badge badge-verified'>✓ Verified</span>")
355
- with gr.Column():
356
- global_id_type = gr.Dropdown(choices=["DUNS", "LEI"], value="DUNS", label="Global ID Type")
357
- global_id = gr.Textbox(label="Global ID Number", value="XXYYZZ1234567890")
358
-
359
- # Status Section
360
- gr.HTML("""
361
- <div class='status-section'>
362
- <div class='status-badges'>
363
- <span class='verification-badge badge-verified'>✅ Auto-Verified (Government Database)</span>
364
- <span class='verification-badge badge-pending'>⏳ Approval Pending</span>
365
- </div>
366
- <div class='access-level'>
367
- <strong>Access Level:</strong> Public & Private Infrastructure
368
- </div>
369
- </div>
370
- """)
371
-
372
- # Registration Summary
373
- gr.HTML("<div class='section-title'>📊 Registration Summary</div>")
374
- gr.HTML("""
375
- <table class='summary-table'>
376
- <tr>
377
- <td>Registration Date</td>
378
- <td>29/05/2025 11:02:02 AM</td>
379
- </tr>
380
- <tr>
381
- <td>Last Updated</td>
382
- <td>29/05/2025 12:07:23 PM</td>
383
- </tr>
384
- <tr>
385
- <td>Updated By</td>
386
- <td>SYSTEM-ADMIN</td>
387
- </tr>
388
- <tr>
389
- <td>Approval Date</td>
390
- <td>29/05/2025 12:07:23 PM</td>
391
- </tr>
392
- <tr>
393
- <td>Approved By</td>
394
- <td>ADMIN-APPROVER</td>
395
- </tr>
396
- </table>
397
- """)
398
-
399
- # Output for success message
400
- output = gr.HTML()
401
-
402
- # Submit Button
403
- submit_btn = gr.Button("🚀 Submit Registration", elem_classes="submit-btn")
404
- submit_btn.click(
405
- submit_action,
406
- [legal_name, legal_type, country, state, city, plant_address, postal_code,
407
- gstin, pan, cin, global_id_type, global_id],
408
- output
409
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
410
 
411
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers.agents import Tool, HfAgent
3
+ import requests
4
+ import os
5
+
6
+ # --- SAP Sales Order Header tool ---
7
+ def query_sales_order_header(query: str) -> str:
8
+ """
9
+ Query SAP API for Sales Order Header information (top 5 records).
10
+ """
11
+ api_url = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SALES_ORDER_SRV/A_SalesOrder?$top=5&$inlinecount=allpages"
12
+ api_key = os.getenv("SAP_SANDBOX_API_KEY", "YOUR_API_KEY") # Set in Space secrets!
13
+ headers = {
14
+ "APIKey": api_key,
15
+ "Accept": "application/json"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  }
17
+ try:
18
+ r = requests.get(api_url, headers=headers, timeout=10)
19
+ r.raise_for_status()
20
+ data = r.json()
21
+ results = data.get('d', {}).get('results', [])
22
+ if not results:
23
+ return "No sales orders found."
24
+ summaries = []
25
+ for order in results:
26
+ summaries.append(
27
+ f"Order: {order['SalesOrder']} | "
28
+ f"Type: {order['SalesOrderType']} | "
29
+ f"Org: {order['SalesOrganization']} | "
30
+ f"Date: {order['SalesOrderDate']} | "
31
+ f"SoldTo: {order['SoldToParty']} | "
32
+ f"Net: {order['TotalNetAmount']} {order['TransactionCurrency']} | "
33
+ f"Status: {order['OverallSDProcessStatus']}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  )
35
+ return "\n".join(summaries)
36
+ except Exception as e:
37
+ return f"Error fetching Sales Orders: {e}"
38
+
39
+ # --- Register the tool for the agent ---
40
+ sap_so_tool = Tool(
41
+ name="SalesOrderHeader",
42
+ description="Fetch SAP Sales Order Header data.",
43
+ function=query_sales_order_header
44
+ )
45
+
46
+ # --- Agent setup (add more tools if needed) ---
47
+ agent = HfAgent(
48
+ repo_id="mistralai/Mistral-7B-Instruct-v0.1",
49
+ tools=[sap_so_tool]
50
+ )
51
+
52
+ # --- Chat with agent & simple memory ---
53
+ def run_agent(input_text, chat_history):
54
+ # Prepend chat memory for better context
55
+ full_input = ""
56
+ if chat_history:
57
+ for turn in chat_history:
58
+ full_input += f"User: {turn[0]}\nAgent: {turn[1]}\n"
59
+ full_input += f"User: {input_text}\nAgent:"
60
+ output = agent.run(full_input)
61
+ chat_history = chat_history or []
62
+ chat_history.append((input_text, output))
63
+ return chat_history, chat_history
64
+
65
+ # --- Gradio UI ---
66
+ with gr.Blocks() as demo:
67
+ gr.Markdown(
68
+ """
69
+ # SAP Sales Order Chat Agent
70
+ Ask about sales orders!
71
+ Example:
72
+ - `Show me the latest SAP sales orders.`
73
+ - `Give me the net value of top sales orders.`
74
+ """
75
+ )
76
+ chatbot = gr.Chatbot()
77
+ txt = gr.Textbox(label="Your question")
78
+ clear = gr.Button("Clear chat")
79
+
80
+ txt.submit(run_agent, [txt, chatbot], [chatbot, chatbot])
81
+ clear.click(lambda: ([], []), None, [chatbot, chatbot])
82
 
83
+ demo.launch()