PD03 commited on
Commit
faa2d89
·
verified ·
1 Parent(s): 9232b2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -25
app.py CHANGED
@@ -1,41 +1,63 @@
1
  import gradio as gr
2
  import requests
3
 
4
- # Updated MCP middleware URL
5
- MCP_URL = "https://pretty-areas-make.loca.lt/mcp"
6
-
7
- def get_sap_orders(top=5):
8
- payload = {
9
- "jsonrpc": "2.0",
10
- "method": "SAP.getProcOrderConfirmation",
11
- "params": {"top": top},
12
- "id": "req-002"
13
- }
 
 
 
 
 
 
 
 
14
  try:
15
- response = requests.post(MCP_URL, json=payload).json()
16
- results = response.get("result", {}).get("d", {}).get("results", [])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  if not results:
19
- return "No process order confirmations found."
20
 
21
- formatted_results = "\n\n".join([
22
- f"Order ID: {r.get('ProcOrdConf', 'N/A')}\n"
23
- f"Material: {r.get('Material', 'N/A')}\n"
24
- f"Quantity: {r.get('YieldQty', 'N/A')} {r.get('YieldQtyUnit', '')}\n"
25
- f"Posting Date: {r.get('PostingDate', 'N/A')}"
26
  for r in results
27
- ])
 
 
28
 
29
- return formatted_results
30
  except Exception as e:
31
- return f"Error fetching data: {str(e)}"
32
 
33
  iface = gr.Interface(
34
- fn=get_sap_orders,
35
- inputs=gr.Slider(minimum=1, maximum=10, step=1, value=3, label="Number of Orders"),
36
  outputs="text",
37
- title="Agentic AI MCP Client for SAP",
38
- description="Fetch SAP Process Order Confirmations via MCP middleware."
39
  )
40
 
41
  iface.launch()
 
1
  import gradio as gr
2
  import requests
3
 
4
+ MCP_URL = "https://free-wombats-make.loca.lt"
5
+
6
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
7
+
8
+ def query_llm(prompt):
9
+ response = requests.post(API_URL, json={"inputs": prompt})
10
+ return response.json()[0]['generated_text']
11
+
12
+ def extract_query_params(query):
13
+ prompt = f"""
14
+ Extract date and quantity from: '{query}'.
15
+ Respond with JSON format only:
16
+ {{"date_from":"YYYY-MM-DD","min_quantity":number}}
17
+ """
18
+ llm_response = query_llm(prompt)
19
+
20
+ # Clean up and safely parse response
21
+ import re, json
22
  try:
23
+ json_text = re.search(r"\{.*\}", llm_response, re.DOTALL).group()
24
+ return json.loads(json_text)
25
+ except:
26
+ return {"date_from":"2023-01-01","min_quantity":0}
27
+
28
+ def agent_query(user_query):
29
+ try:
30
+ params = extract_query_params(user_query)
31
+ payload = {
32
+ "jsonrpc":"2.0",
33
+ "method":"SAP.getFilteredProcOrder",
34
+ "params":params,
35
+ "id":"dynamic-001"
36
+ }
37
+ response = requests.post(MCP_URL,json=payload).json()
38
+ results = response.get("result", [])
39
 
40
  if not results:
41
+ return "No matching orders found."
42
 
43
+ formatted = "\n\n".join(
44
+ f"Order ID: {r.get('ProcessOrderConfirmation','N/A')}, Material: {r.get('Material','N/A')}, "
45
+ f"Qty: {r.get('ConfirmedYieldQuantity','N/A')} {r.get('ConfirmedYieldQuantityUnit','')}, "
46
+ f"Posting: {r.get('PostingDate','N/A')}"
 
47
  for r in results
48
+ )
49
+
50
+ return formatted
51
 
 
52
  except Exception as e:
53
+ return f"Error: {str(e)}"
54
 
55
  iface = gr.Interface(
56
+ fn=agent_query,
57
+ inputs="text",
58
  outputs="text",
59
+ title="Dynamic MCP Agent for SAP (Free LLM)",
60
+ description="Ask SAP queries naturally, e.g., 'Orders after 2023-04-01 with at least 10 units.'"
61
  )
62
 
63
  iface.launch()