Spaces:
Runtime error
Runtime error
VenkateshRoshan
commited on
Commit
·
aff35cb
1
Parent(s):
b6ec07e
app updated
Browse files- app.py +115 -72
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,92 +1,135 @@
|
|
1 |
-
|
2 |
import torch
|
|
|
3 |
import gradio as gr
|
|
|
|
|
4 |
|
5 |
class CustomerSupportBot:
|
6 |
def __init__(self, model_path="models/customer_support_gpt"):
|
7 |
-
|
8 |
-
Initialize the customer support bot with the fine-tuned model.
|
9 |
-
|
10 |
-
Args:
|
11 |
-
model_path (str): Path to the saved model and tokenizer
|
12 |
-
"""
|
13 |
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
14 |
self.model = AutoModelForCausalLM.from_pretrained(model_path)
|
15 |
-
|
16 |
-
# Move model to GPU if available
|
17 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
self.model = self.model.to(self.device)
|
19 |
-
|
20 |
-
def generate_response(self,
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
inputs = self.tokenizer(input_text, return_tensors="pt")
|
37 |
-
inputs = inputs.to(self.device)
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
**inputs,
|
43 |
-
max_length=50,
|
44 |
-
temperature=temperature,
|
45 |
-
num_return_sequences=1,
|
46 |
-
pad_token_id=self.tokenizer.pad_token_id,
|
47 |
-
eos_token_id=self.tokenizer.eos_token_id,
|
48 |
-
do_sample=True,
|
49 |
-
top_p=0.95,
|
50 |
-
top_k=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 |
-
iface = gr.ChatInterface(
|
79 |
-
fn=chatbot_response,
|
80 |
-
title="Customer Support Chatbot",
|
81 |
-
description="Ask your questions to the customer support bot!",
|
82 |
-
examples=["How do I reset my password?",
|
83 |
-
"What are your shipping policies?",
|
84 |
-
"I want to return a product."],
|
85 |
-
# retry_btn=None,
|
86 |
-
# undo_btn="Remove Last",
|
87 |
-
# clear_btn="Clear",
|
88 |
-
)
|
89 |
|
90 |
-
# Launch the interface
|
91 |
if __name__ == "__main__":
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import psutil
|
2 |
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
import gradio as gr
|
5 |
+
import os
|
6 |
+
from typing import List, Tuple
|
7 |
|
8 |
class CustomerSupportBot:
|
9 |
def __init__(self, model_path="models/customer_support_gpt"):
|
10 |
+
self.process = psutil.Process(os.getpid())
|
|
|
|
|
|
|
|
|
|
|
11 |
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
12 |
self.model = AutoModelForCausalLM.from_pretrained(model_path)
|
|
|
|
|
13 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
self.model = self.model.to(self.device)
|
15 |
+
|
16 |
+
def generate_response(self, message: str) -> str:
|
17 |
+
try:
|
18 |
+
input_text = f"Instruction: {message}\nResponse:"
|
19 |
+
inputs = self.tokenizer(input_text, return_tensors="pt").to(self.device)
|
20 |
+
|
21 |
+
with torch.no_grad():
|
22 |
+
outputs = self.model.generate(
|
23 |
+
**inputs,
|
24 |
+
max_length=50,
|
25 |
+
temperature=0.7,
|
26 |
+
num_return_sequences=1,
|
27 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
28 |
+
eos_token_id=self.tokenizer.eos_token_id,
|
29 |
+
do_sample=True,
|
30 |
+
top_p=0.95,
|
31 |
+
top_k=50
|
32 |
+
)
|
33 |
|
34 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
35 |
+
return response.split("Response:")[-1].strip()
|
36 |
+
except Exception as e:
|
37 |
+
return f"An error occurred: {str(e)}"
|
38 |
+
|
39 |
+
def monitor_resources(self) -> dict:
|
40 |
+
usage = {
|
41 |
+
"CPU (%)": self.process.cpu_percent(interval=1),
|
42 |
+
"RAM (GB)": self.process.memory_info().rss / (1024 ** 3)
|
43 |
+
}
|
44 |
+
if torch.cuda.is_available():
|
45 |
+
usage["GPU (GB)"] = torch.cuda.memory_allocated(0) / (1024 ** 3)
|
46 |
+
return usage
|
47 |
+
|
48 |
+
def create_chat_interface():
|
49 |
+
bot = CustomerSupportBot()
|
50 |
+
|
51 |
+
def predict(message: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
|
52 |
+
if not message:
|
53 |
+
return "", history
|
54 |
|
55 |
+
bot_response = bot.generate_response(message)
|
|
|
|
|
56 |
|
57 |
+
# Log resource usage
|
58 |
+
usage = bot.monitor_resources()
|
59 |
+
print("Resource Usage:", usage)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
history.append((message, bot_response))
|
62 |
+
return "", history
|
63 |
+
|
64 |
+
# Create the Gradio interface with custom CSS
|
65 |
+
with gr.Blocks(css="""
|
66 |
+
.message-box {
|
67 |
+
margin-bottom: 10px;
|
68 |
+
}
|
69 |
+
.button-row {
|
70 |
+
display: flex;
|
71 |
+
gap: 10px;
|
72 |
+
margin-top: 10px;
|
73 |
+
}
|
74 |
+
""") as interface:
|
75 |
+
gr.Markdown("# Customer Support Chatbot")
|
76 |
+
gr.Markdown("Welcome! How can I assist you today?")
|
77 |
|
78 |
+
chatbot = gr.Chatbot(
|
79 |
+
label="Chat History",
|
80 |
+
height=400,
|
81 |
+
elem_classes="message-box"
|
82 |
+
)
|
83 |
+
|
84 |
+
with gr.Row():
|
85 |
+
msg = gr.Textbox(
|
86 |
+
label="Your Message",
|
87 |
+
placeholder="Type your message here...",
|
88 |
+
lines=2,
|
89 |
+
elem_classes="message-box"
|
90 |
+
)
|
91 |
|
92 |
+
with gr.Row(elem_classes="button-row"):
|
93 |
+
submit = gr.Button("Send Message", variant="primary")
|
94 |
+
clear = gr.ClearButton([msg, chatbot], value="Clear Chat")
|
95 |
|
96 |
+
# Add example queries in a separate row
|
97 |
+
with gr.Row():
|
98 |
+
gr.Examples(
|
99 |
+
examples=[
|
100 |
+
"How do I reset my password?",
|
101 |
+
"What are your shipping policies?",
|
102 |
+
"I want to return a product.",
|
103 |
+
"How can I track my order?",
|
104 |
+
"What payment methods do you accept?"
|
105 |
+
],
|
106 |
+
inputs=msg,
|
107 |
+
label="Example Questions"
|
108 |
+
)
|
109 |
|
110 |
+
# Set up event handlers
|
111 |
+
submit_click = submit.click(
|
112 |
+
predict,
|
113 |
+
inputs=[msg, chatbot],
|
114 |
+
outputs=[msg, chatbot]
|
115 |
+
)
|
116 |
+
|
117 |
+
msg.submit(
|
118 |
+
predict,
|
119 |
+
inputs=[msg, chatbot],
|
120 |
+
outputs=[msg, chatbot]
|
121 |
+
)
|
122 |
+
|
123 |
+
# Add keyboard shortcut for submit
|
124 |
+
msg.change(lambda x: gr.update(interactive=bool(x.strip())), inputs=[msg], outputs=[submit])
|
125 |
|
126 |
+
return interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
|
|
128 |
if __name__ == "__main__":
|
129 |
+
demo = create_chat_interface()
|
130 |
+
demo.launch(
|
131 |
+
share=False,
|
132 |
+
server_name="0.0.0.0", # Makes the server accessible from other machines
|
133 |
+
server_port=7860, # Specify the port
|
134 |
+
debug=True
|
135 |
+
)
|
requirements.txt
CHANGED
@@ -7,3 +7,4 @@ boto3
|
|
7 |
pytest
|
8 |
pydantic
|
9 |
datasets
|
|
|
|
7 |
pytest
|
8 |
pydantic
|
9 |
datasets
|
10 |
+
psutil
|