Spaces:
Running
Running
Create chat_agent.py
Browse files- chat_agent.py +105 -0
chat_agent.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import json
|
4 |
+
import time
|
5 |
+
import random
|
6 |
+
|
7 |
+
class ChatAgent:
|
8 |
+
def __init__(self):
|
9 |
+
self.client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
10 |
+
self.load_content()
|
11 |
+
self.engagement_prompts = [
|
12 |
+
"π Hi there! Looking for an AI solution? We have options from $100 for students to enterprise-grade systems.",
|
13 |
+
"π Are you a student? Check out our Student Study Assistant - lifetime access for just $100!",
|
14 |
+
"π Ready to transform your workflow with AI? Our RAG Assistant Pro might be perfect for you.",
|
15 |
+
"π’ Need an enterprise AI solution? Let's discuss your custom requirements.",
|
16 |
+
]
|
17 |
+
|
18 |
+
def load_content(self):
|
19 |
+
with open("data/site_content.json", "r") as f:
|
20 |
+
self.content = json.load(f)
|
21 |
+
|
22 |
+
def get_product_info(self, product_name=None):
|
23 |
+
products = self.content.get('products', [])
|
24 |
+
if product_name:
|
25 |
+
for product in products:
|
26 |
+
if product['name'].lower() == product_name.lower():
|
27 |
+
return product
|
28 |
+
return products[0] # Return first product if none specified
|
29 |
+
|
30 |
+
def generate_initial_greeting(self):
|
31 |
+
return random.choice(self.engagement_prompts)
|
32 |
+
|
33 |
+
def get_response(self, message, history):
|
34 |
+
# Get relevant product based on message content
|
35 |
+
context = ""
|
36 |
+
if "student" in message.lower():
|
37 |
+
product = self.get_product_info("Student Study Assistant")
|
38 |
+
context = f"Focusing on Student Study Assistant: {product['description']} Price: {product['price']}"
|
39 |
+
elif "rag" in message.lower() or "professional" in message.lower():
|
40 |
+
product = self.get_product_info("Personalized RAG Assistant Pro")
|
41 |
+
context = f"Focusing on RAG Assistant Pro: {product['description']} Price: {product['price']}"
|
42 |
+
elif "enterprise" in message.lower():
|
43 |
+
product = self.get_product_info("Enterprise AI Suite")
|
44 |
+
context = f"Focusing on Enterprise AI Suite: {product['description']}"
|
45 |
+
elif "custom" in message.lower() or "llm" in message.lower():
|
46 |
+
product = self.get_product_info("Custom LLM Platform")
|
47 |
+
context = f"Focusing on Custom LLM Platform: {product['description']}"
|
48 |
+
|
49 |
+
system_message = f"""You are a helpful sales assistant for Sletcher Systems.
|
50 |
+
Current product information: {context}
|
51 |
+
Style: Be friendly, professional, and helpful. Focus on understanding the customer's needs.
|
52 |
+
Goals: Help customers find the right AI solution and encourage them to schedule a consultation.
|
53 |
+
"""
|
54 |
+
|
55 |
+
messages = [{"role": "system", "content": system_message}]
|
56 |
+
for msg in history:
|
57 |
+
messages.extend([
|
58 |
+
{"role": "user", "content": msg[0]},
|
59 |
+
{"role": "assistant", "content": msg[1]}
|
60 |
+
])
|
61 |
+
messages.append({"role": "user", "content": message})
|
62 |
+
|
63 |
+
response = ""
|
64 |
+
for msg in self.client.chat_completion(
|
65 |
+
messages,
|
66 |
+
max_tokens=512,
|
67 |
+
stream=True,
|
68 |
+
temperature=0.7,
|
69 |
+
):
|
70 |
+
token = msg.choices[0].delta.content
|
71 |
+
response += token
|
72 |
+
yield response
|
73 |
+
|
74 |
+
def create_chat_interface():
|
75 |
+
agent = ChatAgent()
|
76 |
+
|
77 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
78 |
+
chatbot = gr.Chatbot(
|
79 |
+
label="SletcherSystems Sales Assistant",
|
80 |
+
height=400
|
81 |
+
)
|
82 |
+
msg = gr.Textbox(label="Type your message here...")
|
83 |
+
clear = gr.Button("Clear")
|
84 |
+
|
85 |
+
# Add initial greeting
|
86 |
+
def show_greeting():
|
87 |
+
return [[None, agent.generate_initial_greeting()]]
|
88 |
+
|
89 |
+
def respond(message, chat_history):
|
90 |
+
bot_message = ""
|
91 |
+
for chunk in agent.get_response(message, chat_history):
|
92 |
+
bot_message = chunk
|
93 |
+
yield chat_history + [[message, bot_message]]
|
94 |
+
|
95 |
+
msg.submit(respond, [msg, chatbot], [chatbot])
|
96 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
97 |
+
|
98 |
+
# Show initial greeting
|
99 |
+
demo.load(show_greeting, None, chatbot)
|
100 |
+
|
101 |
+
return demo
|
102 |
+
|
103 |
+
if __name__ == "__main__":
|
104 |
+
demo = create_chat_interface()
|
105 |
+
demo.launch()
|