Spaces:
Sleeping
Sleeping
File size: 17,785 Bytes
393aa68 b9fdcec a21f844 4d067a4 e866dc6 4d067a4 393aa68 f2283a2 4d067a4 f2283a2 4d067a4 393aa68 4d067a4 a21f844 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 4d067a4 393aa68 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# Setup Hugging Face Inference API for LLAMA3
import os
import requests
import json
import gradio as gr
from typing import List, Dict, Any, Optional
import logging
import time
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Configuration - Set these as environment variables in Hugging Face Spaces
SAP_API_KEY = os.getenv('SAP_API_KEY') # Set in Space secrets
HF_TOKEN = os.getenv('HF_TOKEN') # Set in Space secrets
SAP_BASE_URL = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap"
# Hugging Face Inference API endpoints
HF_API_BASE = "https://api-inference.huggingface.co/models"
LLAMA3_MODEL = "meta-llama/Meta-Llama-3-8B-Instruct" # Using inference API
class LLAMA3Client:
def __init__(self, hf_token: str):
self.hf_token = hf_token
self.api_url = f"{HF_API_BASE}/{LLAMA3_MODEL}"
self.headers = {
"Authorization": f"Bearer {hf_token}",
"Content-Type": "application/json"
}
# Warm up the model
self._warm_up_model()
def _warm_up_model(self):
"""Warm up the model to avoid cold start delays"""
try:
logger.info("Warming up LLAMA3 model...")
self._make_inference_request("Hello", max_new_tokens=10)
logger.info("Model warmed up successfully")
except Exception as e:
logger.warning(f"Model warm-up failed: {e}")
def _make_inference_request(self, prompt: str, max_new_tokens: int = 500, temperature: float = 0.1, max_retries: int = 3) -> str:
"""Make inference request to Hugging Face API with retry logic"""
payload = {
"inputs": prompt,
"parameters": {
"max_new_tokens": max_new_tokens,
"temperature": temperature,
"do_sample": True,
"top_p": 0.9,
"return_full_text": False
}
}
for attempt in range(max_retries):
try:
response = requests.post(
self.api_url,
headers=self.headers,
json=payload,
timeout=60
)
if response.status_code == 503:
# Model is loading, wait and retry
wait_time = min(20 * (attempt + 1), 60)
logger.info(f"Model loading, waiting {wait_time}s...")
time.sleep(wait_time)
continue
response.raise_for_status()
result = response.json()
if isinstance(result, list) and len(result) > 0:
return result[0].get('generated_text', '').strip()
elif isinstance(result, dict) and 'generated_text' in result:
return result['generated_text'].strip()
else:
logger.error(f"Unexpected response format: {result}")
return "I received an unexpected response format."
except requests.exceptions.RequestException as e:
logger.error(f"Request failed (attempt {attempt + 1}): {e}")
if attempt == max_retries - 1:
return f"Failed to get response after {max_retries} attempts: {str(e)}"
time.sleep(2 ** attempt) # Exponential backoff
except Exception as e:
logger.error(f"Unexpected error: {e}")
return f"An unexpected error occurred: {str(e)}"
return "Failed to generate response"
def generate_response(self, prompt: str, max_length: int = 500, temperature: float = 0.1) -> str:
"""Generate response using LLAMA3 via Inference API"""
# Format prompt for LLAMA3 instruction format
formatted_prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are a helpful SAP data analyst. Provide clear, concise answers based on the provided data. Keep responses under 300 words.<|eot_id|><|start_header_id|>user<|end_header_id|>
{prompt}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
"""
try:
response = self._make_inference_request(
formatted_prompt,
max_new_tokens=min(max_length, 400), # Limit tokens to avoid timeouts
temperature=temperature
)
# Clean up the response
if response and len(response.strip()) > 0:
return response
else:
return "I couldn't generate a proper response. Please try rephrasing your question."
except Exception as e:
logger.error(f"Error generating response: {e}")
return f"I encountered an error while processing your question: {str(e)}"
class SAPDataFetcher:
def __init__(self, api_key: str):
self.api_key = api_key
self.headers = {
"APIKey": api_key,
"Accept": "application/json",
"Content-Type": "application/json"
}
def _make_request(self, url: str, timeout: int = 30) -> Optional[Dict]:
"""Make HTTP request with proper error handling"""
try:
logger.info(f"Making request to: {url}")
response = requests.get(url, headers=self.headers, timeout=timeout)
response.raise_for_status()
data = response.json()
logger.info(f"Request successful. Response size: {len(str(data))}")
return data
except requests.exceptions.RequestException as e:
logger.error(f"Request failed: {e}")
return None
except json.JSONDecodeError as e:
logger.error(f"JSON decode error: {e}")
return None
def fetch_sales_orders(self, top: int = 30) -> List[Dict]:
"""Fetch sales orders with error handling"""
url = f"{SAP_BASE_URL}/API_SALES_ORDER_SRV/A_SalesOrder?$top={top}&$inlinecount=allpages"
data = self._make_request(url)
if data and 'd' in data and 'results' in data['d']:
orders = data['d']['results']
# Simplify the data structure
simplified_orders = []
for order in orders:
simplified_order = {
"SalesOrder": order.get("SalesOrder", ""),
"SalesOrderType": order.get("SalesOrderType", ""),
"SalesOrganization": order.get("SalesOrganization", ""),
"SoldToParty": order.get("SoldToParty", ""),
"CreationDate": order.get("CreationDate", ""),
"CreatedByUser": order.get("CreatedByUser", ""),
"TransactionCurrency": order.get("TransactionCurrency", ""),
"TotalNetAmount": order.get("TotalNetAmount", "0")
}
simplified_orders.append(simplified_order)
return simplified_orders
else:
logger.error("Failed to fetch sales orders or invalid response format")
return []
def fetch_purchase_orders(self, top: int = 30) -> List[Dict]:
"""Fetch purchase order headers"""
url = f"{SAP_BASE_URL}/API_PURCHASEORDER_PROCESS_SRV/A_PurchaseOrder?$top={top}&$inlinecount=allpages"
data = self._make_request(url)
if data and 'd' in data and 'results' in data['d']:
orders = data['d']['results']
simplified_orders = []
for order in orders:
simplified_order = {
"PurchaseOrder": order.get("PurchaseOrder", ""),
"CompanyCode": order.get("CompanyCode", ""),
"PurchaseOrderType": order.get("PurchaseOrderType", ""),
"CreatedByUser": order.get("CreatedByUser", ""),
"CreationDate": order.get("CreationDate", ""),
"Supplier": order.get("Supplier", ""),
"PurchasingOrganization": order.get("PurchasingOrganization", ""),
"PurchasingGroup": order.get("PurchasingGroup", ""),
"PurchaseOrderDate": order.get("PurchaseOrderDate", ""),
"DocumentCurrency": order.get("DocumentCurrency", ""),
"ExchangeRate": order.get("ExchangeRate", "1.0")
}
simplified_orders.append(simplified_order)
return simplified_orders
else:
logger.error("Failed to fetch purchase orders or invalid response format")
return []
def fetch_purchase_order_items(self, purchase_orders: List[str]) -> List[Dict]:
"""Fetch purchase order items for given order numbers"""
all_items = []
for po_number in purchase_orders[:5]: # Reduced limit for faster processing
url = f"{SAP_BASE_URL}/API_PURCHASEORDER_PROCESS_SRV/A_PurchaseOrderItem?$filter=PurchaseOrder eq '{po_number}'"
data = self._make_request(url)
if data and 'd' in data and 'results' in data['d']:
items = data['d']['results']
for item in items:
simplified_item = {
"PurchaseOrder": item.get("PurchaseOrder", ""),
"PurchaseOrderItem": item.get("PurchaseOrderItem", ""),
"Plant": item.get("Plant", ""),
"StorageLocation": item.get("StorageLocation", ""),
"MaterialGroup": item.get("MaterialGroup", ""),
"OrderQuantity": item.get("OrderQuantity", "0"),
"PurchaseOrderQuantityUnit": item.get("PurchaseOrderQuantityUnit", ""),
"DocumentCurrency": item.get("DocumentCurrency", ""),
"NetPriceAmount": item.get("NetPriceAmount", "0"),
"NetPriceQuantity": item.get("NetPriceQuantity", "0")
}
all_items.append(simplified_item)
return all_items
class SAPAgent:
def __init__(self, data_fetcher: SAPDataFetcher, llama_client: LLAMA3Client):
self.data_fetcher = data_fetcher
self.llama_client = llama_client
def categorize_query(self, question: str) -> str:
"""Determine if query is about sales or purchase orders"""
category_prompt = f"""Analyze this question and determine if it's about Sales Orders or Purchase Orders:
Question: "{question}"
Guidelines:
- Sales Orders: customer orders, sales transactions, revenue, sold to party
- Purchase Orders: supplier orders, procurement, purchasing, vendor transactions
Respond with exactly one word: "sales" or "purchase" """
try:
response = self.llama_client.generate_response(category_prompt, max_length=20, temperature=0)
category = response.strip().lower()
return "sales" if "sales" in category else "purchase"
except Exception as e:
logger.error(f"Error in categorization: {e}")
return "purchase" # Default to purchase
def needs_item_details(self, question: str) -> bool:
"""Determine if question requires item-level details"""
detail_prompt = f"""Does this question require detailed item-level information (quantities, prices, materials, line items)?
Question: "{question}"
Answer only "yes" or "no" """
try:
response = self.llama_client.generate_response(detail_prompt, max_length=20, temperature=0)
answer = response.strip().lower()
return "yes" in answer
except Exception as e:
logger.error(f"Error determining detail needs: {e}")
return False
def process_query(self, question: str) -> str:
"""Main function to process user queries"""
logger.info(f"Processing query: {question}")
# Categorize the query
category = self.categorize_query(question)
logger.info(f"Query categorized as: {category}")
# Fetch appropriate data
if category == "sales":
data = self.data_fetcher.fetch_sales_orders()
data_type = "Sales Orders"
context = {"orders": data}
else:
# Fetch purchase order headers
po_headers = self.data_fetcher.fetch_purchase_orders()
context = {"headers": po_headers}
data_type = "Purchase Order Headers"
# Check if item details are needed
if self.needs_item_details(question) and po_headers:
logger.info("Fetching item-level details")
po_numbers = [po["PurchaseOrder"] for po in po_headers[:5] if po["PurchaseOrder"]] # Limit for performance
po_items = self.data_fetcher.fetch_purchase_order_items(po_numbers)
context["items"] = po_items
data_type = "Purchase Orders with Item Details"
# Calculate total value
total_value = 0.0
for item in po_items:
try:
net_price = float(item.get("NetPriceAmount", 0))
quantity = float(item.get("OrderQuantity", 0))
total_value += net_price * quantity
except (ValueError, TypeError):
continue
context["total_value"] = total_value
# Generate response using LLAMA3
return self.generate_response(question, context, data_type)
def generate_response(self, question: str, context: Dict, data_type: str) -> str:
"""Generate response using LLAMA3"""
# Limit context size for API efficiency
context_str = json.dumps(context, indent=2)
if len(context_str) > 2000: # Smaller limit for API
context_str = context_str[:2000] + "... (truncated)"
prompt = f"""Data Type: {data_type}
Available Data:
{context_str}
User Question: {question}
Instructions:
1. Provide a clear, concise answer based on the data
2. Include specific numbers, dates, or values when relevant
3. If the data doesn't contain enough information to answer fully, mention this
4. Format your response in a user-friendly way
5. Keep response under 250 words"""
try:
return self.llama_client.generate_response(prompt, max_length=400, temperature=0.1)
except Exception as e:
logger.error(f"Error generating response: {e}")
return f"I encountered an error while processing your question: {str(e)}"
# Initialize the system
try:
if not HF_TOKEN:
logger.error("HF_TOKEN not found in environment variables")
sap_agent = None
else:
llama_client = LLAMA3Client(HF_TOKEN)
if SAP_API_KEY:
data_fetcher = SAPDataFetcher(SAP_API_KEY)
sap_agent = SAPAgent(data_fetcher, llama_client)
logger.info("SAP Agent initialized successfully")
else:
logger.warning("SAP_API_KEY not found. Demo mode enabled.")
sap_agent = None
except Exception as e:
logger.error(f"Failed to initialize SAP Agent: {e}")
sap_agent = None
# Gradio Interface
def chat_with_sap(message, history):
"""Handle chat interactions"""
if not sap_agent:
return history + [("System", "SAP Agent not initialized. Please check your HF_TOKEN and SAP_API_KEY in Space secrets.")]
if not message.strip():
return history
try:
# Add typing indicator
history = history or []
history.append((message, "π€ Thinking..."))
yield history
# Process the query
response = sap_agent.process_query(message)
history[-1] = (message, response)
yield history
except Exception as e:
error_msg = f"Error processing your request: {str(e)}"
history = history or []
if history and history[-1][1] == "π€ Thinking...":
history[-1] = (message, error_msg)
else:
history.append((message, error_msg))
yield history
def clear_chat():
return []
# Create Gradio interface
with gr.Blocks(title="SAP Order Analytics Agent with LLAMA3") as demo:
gr.Markdown("""
# π SAP Order Analytics Agent (Powered by LLAMA3 via Inference API)
This AI agent uses Meta's LLAMA3 model via Hugging Face Inference API to analyze SAP data. Ask questions like:
- "How many sales orders do we have?"
- "What's the total value of all purchase orders?"
- "Show me recent purchase orders"
- "What are the top suppliers?"
**Setup Required:**
1. Set `HF_TOKEN` in Space secrets (your Hugging Face token)
2. Set `SAP_API_KEY` in Space secrets (your SAP API key)
3. Ensure you have access to LLAMA3 model on Hugging Face
""")
chatbot = gr.Chatbot(
height=500,
placeholder="Ask me anything about your SAP orders...",
show_copy_button=True
)
with gr.Row():
msg = gr.Textbox(
label="Your Question",
placeholder="Type your question here...",
scale=4
)
submit_btn = gr.Button("Send", scale=1, variant="primary")
clear_btn = gr.Button("Clear", scale=1)
# Event handlers
submit_btn.click(chat_with_sap, [msg, chatbot], [chatbot])
msg.submit(chat_with_sap, [msg, chatbot], [chatbot])
clear_btn.click(clear_chat, outputs=[chatbot])
# Clear input after submission
submit_btn.click(lambda: "", outputs=[msg])
msg.submit(lambda: "", outputs=[msg])
# Launch the interface
if __name__ == "__main__":
demo.launch(share=True) |