Upload sales_inference.py with huggingface_hub
Browse files- sales_inference.py +91 -0
sales_inference.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# sales_inference.py - Simple Azure OpenAI Only Implementation
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
from openai import AzureOpenAI
|
| 6 |
+
from stable_baselines3 import PPO
|
| 7 |
+
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
from dataclasses import dataclass
|
| 10 |
+
from typing import List, Dict
|
| 11 |
+
|
| 12 |
+
# Azure OpenAI Configuration
|
| 13 |
+
openai_client = AzureOpenAI(
|
| 14 |
+
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
|
| 15 |
+
api_version="2023-05-15",
|
| 16 |
+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 20 |
+
|
| 21 |
+
class CustomCNN(BaseFeaturesExtractor):
|
| 22 |
+
def __init__(self, observation_space, features_dim: int = 64):
|
| 23 |
+
super().__init__(observation_space, features_dim)
|
| 24 |
+
n_input = observation_space.shape[0]
|
| 25 |
+
self.cnn = nn.Sequential(
|
| 26 |
+
nn.Linear(n_input, 512), nn.ReLU(),
|
| 27 |
+
nn.Linear(512, 256), nn.ReLU(),
|
| 28 |
+
nn.Linear(256, features_dim), nn.ReLU(),
|
| 29 |
+
).to(device)
|
| 30 |
+
def forward(self, obs): return self.cnn(obs)
|
| 31 |
+
|
| 32 |
+
@dataclass
|
| 33 |
+
class SalesAgent:
|
| 34 |
+
model_path: str
|
| 35 |
+
|
| 36 |
+
def __init__(self, model_path):
|
| 37 |
+
self.model = PPO.load(model_path, device=device, custom_objects={
|
| 38 |
+
"policy_kwargs": dict(
|
| 39 |
+
activation_fn=nn.ReLU,
|
| 40 |
+
net_arch=[dict(pi=[128, 64], vf=[128, 64])],
|
| 41 |
+
features_extractor_class=CustomCNN,
|
| 42 |
+
features_extractor_kwargs=dict(features_dim=64)
|
| 43 |
+
)
|
| 44 |
+
})
|
| 45 |
+
|
| 46 |
+
def get_embedding(self, text):
|
| 47 |
+
response = openai_client.embeddings.create(
|
| 48 |
+
model=os.getenv("AZURE_EMBEDDING_DEPLOYMENT", "text-embedding-3-large"),
|
| 49 |
+
input=text
|
| 50 |
+
)
|
| 51 |
+
emb = np.array(response.data[0].embedding, dtype=np.float32)
|
| 52 |
+
# Adjust to expected dimension 3072
|
| 53 |
+
return np.concatenate([emb[:3072], np.zeros(max(0, 3072-len(emb)))]) if len(emb) < 3072 else emb[:3072]
|
| 54 |
+
|
| 55 |
+
def predict_conversion(self, conversation_text, turn_number):
|
| 56 |
+
emb = self.get_embedding(conversation_text)
|
| 57 |
+
metrics = np.array([0.5, 0.5, turn_number, 0.5, min(1.0, turn_number/20)], dtype=np.float32)
|
| 58 |
+
state = np.concatenate([emb, metrics, [turn_number], np.zeros(10)])
|
| 59 |
+
with torch.no_grad():
|
| 60 |
+
action, _ = self.model.predict(state, deterministic=True)
|
| 61 |
+
return max(0.0, min(1.0, float(action[0])))
|
| 62 |
+
|
| 63 |
+
def analyze_conversation(self, conversation):
|
| 64 |
+
results = []
|
| 65 |
+
text = ""
|
| 66 |
+
for i, turn in enumerate(conversation):
|
| 67 |
+
text += f"{turn['role']}: {turn['content']}\n"
|
| 68 |
+
if turn['role'] == 'assistant':
|
| 69 |
+
prob = self.predict_conversion(text, i//2+1)
|
| 70 |
+
results.append({'turn_number': i//2+1, 'response': turn['content'], 'probability': prob})
|
| 71 |
+
final_prob = results[-1]['probability'] if results else 0.0
|
| 72 |
+
return {'conversation_results': results, 'final_probability': final_prob,
|
| 73 |
+
'summary': f"Predicted final conversion: {final_prob*100:.1f}%"}
|
| 74 |
+
|
| 75 |
+
# Usage example
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
# Download model from HuggingFace
|
| 78 |
+
from huggingface_hub import hf_hub_download
|
| 79 |
+
model_path = hf_hub_download(repo_id='DeepMostInnovations/sales-conversion-model-reinf-learning', filename="sales_model.zip")
|
| 80 |
+
|
| 81 |
+
# Initialize agent
|
| 82 |
+
agent = SalesAgent(model_path)
|
| 83 |
+
|
| 84 |
+
# Test conversation
|
| 85 |
+
conv = [
|
| 86 |
+
{"role": "user", "content": "I'm interested in your product"},
|
| 87 |
+
{"role": "assistant", "content": "Great! What's your team size?"}
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
result = agent.analyze_conversation(conv)
|
| 91 |
+
print(result['summary'])
|