PD03 commited on
Commit
1a169fb
·
verified ·
1 Parent(s): 00835a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -34
app.py CHANGED
@@ -1,15 +1,18 @@
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"
@@ -36,48 +39,36 @@ def query_sales_order_header(query: str) -> str:
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()
 
1
  import gradio as gr
 
2
  import requests
3
  import os
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
+
6
+ # --- Load an open LLM (here Mistral 7B) ---
7
+ model_id = "mistralai/Mistral-7B-Instruct-v0.1"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
10
+ llm = pipeline("text-generation", model=model, tokenizer=tokenizer)
11
 
12
  # --- SAP Sales Order Header tool ---
13
+ def query_sales_order_header():
 
 
 
14
  api_url = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SALES_ORDER_SRV/A_SalesOrder?$top=5&$inlinecount=allpages"
15
+ api_key = os.getenv("SAP_SANDBOX_API_KEY", "YOUR_API_KEY")
16
  headers = {
17
  "APIKey": api_key,
18
  "Accept": "application/json"
 
39
  except Exception as e:
40
  return f"Error fetching Sales Orders: {e}"
41
 
42
+ # --- Main chat function ---
43
+ def chat_agent(message, history):
44
+ # Rudimentary tool use: check for sales order question
45
+ tool_trigger = ["sales order", "sap order", "sap sales"]
46
+ if any(kw in message.lower() for kw in tool_trigger):
47
+ tool_response = query_sales_order_header()
48
+ response = f"(SAP Sales Orders)\n{tool_response}"
49
+ else:
50
+ # Pure LLM response
51
+ response = llm(message, max_new_tokens=256)[0]["generated_text"]
52
+ history = history or []
53
+ history.append((message, response))
54
+ return history, history
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  # --- Gradio UI ---
57
  with gr.Blocks() as demo:
58
  gr.Markdown(
59
  """
60
  # SAP Sales Order Chat Agent
61
+ Ask about SAP sales orders or general questions.
62
  Example:
63
  - `Show me the latest SAP sales orders.`
64
+ - `Tell me about SAP order values.`
65
  """
66
  )
67
  chatbot = gr.Chatbot()
68
  txt = gr.Textbox(label="Your question")
69
  clear = gr.Button("Clear chat")
70
 
71
+ txt.submit(chat_agent, [txt, chatbot], [chatbot, chatbot])
72
  clear.click(lambda: ([], []), None, [chatbot, chatbot])
73
 
74
  demo.launch()