PD03 commited on
Commit
0670950
·
verified ·
1 Parent(s): 108139b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -20
app.py CHANGED
@@ -3,15 +3,14 @@ import requests
3
  import os
4
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
 
6
- # ---- Hugging Face model: use a public chat/instruct model ----
7
- model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
  tokenizer = AutoTokenizer.from_pretrained(model_id)
9
  model = AutoModelForCausalLM.from_pretrained(model_id)
10
  llm = pipeline("text-generation", model=model, tokenizer=tokenizer)
11
 
12
  # --- SAP Sales Order Header tool ---
13
  def fetch_sales_order_headers():
14
- """Fetches the top 5 SAP sales orders from the sandbox API and returns as a list of dicts."""
15
  api_url = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SALES_ORDER_SRV/A_SalesOrder?$top=5&$inlinecount=allpages"
16
  api_key = os.getenv("SAP_SANDBOX_API_KEY", "YOUR_API_KEY") # Set in Space secrets!
17
  headers = {
@@ -30,9 +29,8 @@ def fetch_sales_order_headers():
30
  return f"Error fetching Sales Orders: {e}"
31
 
32
  def format_sales_orders_for_llm(orders):
33
- """Converts the SAP sales order list to readable context for the LLM."""
34
  if isinstance(orders, str):
35
- return orders # Pass through error message
36
  context = "Here are the latest SAP sales orders:\n"
37
  for i, order in enumerate(orders, 1):
38
  context += (
@@ -47,37 +45,27 @@ def format_sales_orders_for_llm(orders):
47
  return context
48
 
49
  def chat_agent(message, history):
50
- # Step 1: Fetch SAP data every time
51
  sales_orders = fetch_sales_order_headers()
52
  context = format_sales_orders_for_llm(sales_orders)
53
- # Step 2: Prompt LLM with both the question and the context
54
  prompt = (
55
  f"{context}\n"
56
  f"User asked: {message}\n"
57
  "Based on the above SAP sales orders, answer the user's question as accurately as possible. "
58
- "If the question asks for sorting, filtering, maximum values, customer-specific info, or other details, do it based on the orders above. "
59
- "If the question is unclear, summarize the available sales orders."
60
  )
61
- # Step 3: Generate LLM answer
62
  llm_output = llm(prompt, max_new_tokens=256)[0]["generated_text"]
63
- # Remove the prompt from the response if present
64
  response = llm_output.replace(prompt, "").strip()
65
  history = history or []
66
  history.append((message, response))
67
  return history, history
68
 
69
- # ---- Gradio UI ----
70
  with gr.Blocks() as demo:
71
  gr.Markdown(
72
  """
73
- # SAP Sales Order Chat Agent (Smart Reasoning)
74
- Ask about SAP sales orders, values, filtering, sorting, etc.
75
- Example questions:
76
- - `Show me the latest SAP sales orders.`
77
- - `Tell me about SAP order values.`
78
- - `Show me top 2 sales orders with maximum value.`
79
- - `Which order has the highest net value?`
80
- - `List orders by customer.`
81
  """
82
  )
83
  chatbot = gr.Chatbot()
 
3
  import os
4
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
 
6
+ # -- Use a tiny, chat-tuned model --
7
+ model_id = "Qwen/Qwen1.5-0.5B-Chat"
8
  tokenizer = AutoTokenizer.from_pretrained(model_id)
9
  model = AutoModelForCausalLM.from_pretrained(model_id)
10
  llm = pipeline("text-generation", model=model, tokenizer=tokenizer)
11
 
12
  # --- SAP Sales Order Header tool ---
13
  def fetch_sales_order_headers():
 
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") # Set in Space secrets!
16
  headers = {
 
29
  return f"Error fetching Sales Orders: {e}"
30
 
31
  def format_sales_orders_for_llm(orders):
 
32
  if isinstance(orders, str):
33
+ return orders # Error
34
  context = "Here are the latest SAP sales orders:\n"
35
  for i, order in enumerate(orders, 1):
36
  context += (
 
45
  return context
46
 
47
  def chat_agent(message, history):
 
48
  sales_orders = fetch_sales_order_headers()
49
  context = format_sales_orders_for_llm(sales_orders)
 
50
  prompt = (
51
  f"{context}\n"
52
  f"User asked: {message}\n"
53
  "Based on the above SAP sales orders, answer the user's question as accurately as possible. "
54
+ "If the question asks for sorting, filtering, or summarizing, do it based on the data above."
 
55
  )
 
56
  llm_output = llm(prompt, max_new_tokens=256)[0]["generated_text"]
57
+ # Remove the prompt if echoed
58
  response = llm_output.replace(prompt, "").strip()
59
  history = history or []
60
  history.append((message, response))
61
  return history, history
62
 
 
63
  with gr.Blocks() as demo:
64
  gr.Markdown(
65
  """
66
+ # SAP Sales Order Chat Agent (Small Chat Model)
67
+ - Asks about SAP sales orders, values, filtering, sorting, etc.
68
+ - Example: `Show me top 2 sales orders by value.`
 
 
 
 
 
69
  """
70
  )
71
  chatbot = gr.Chatbot()