Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,178 +1,97 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import pandas as pd
|
4 |
import spaces
|
5 |
|
6 |
-
# Load
|
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 |
-
if
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
# Custom CSS for button styling
|
75 |
custom_css = """
|
76 |
-
|
77 |
-
background-color: #
|
78 |
-
border: none !important;
|
79 |
-
color: white !important;
|
80 |
-
padding: 10px 20px !important;
|
81 |
-
text-align: center !important;
|
82 |
-
text-decoration: none !important;
|
83 |
-
display: inline-block !important;
|
84 |
-
font-size: 16px !important;
|
85 |
-
border-radius: 5px !important;
|
86 |
-
transition: background-color 0.3s ease !important;
|
87 |
-
}
|
88 |
-
|
89 |
-
button:hover {
|
90 |
-
background-color: #3a52cc !important;
|
91 |
}
|
92 |
footer {
|
93 |
visibility: hidden;
|
94 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
"""
|
96 |
|
97 |
-
|
98 |
-
with gr.Blocks(css=custom_css) as nps:
|
99 |
-
# State to store categories
|
100 |
-
categories = gr.State([])
|
101 |
-
|
102 |
-
# App title
|
103 |
-
gr.Markdown("# 🎉 Customer Comment Classifier 🎉")
|
104 |
-
|
105 |
-
# Short explanation
|
106 |
-
gr.Markdown("""
|
107 |
-
This app classifies customer comments into categories and assigns sentiment labels (Positive/Negative).
|
108 |
-
You can upload your own dataset or use the provided template. The app will append the generated
|
109 |
-
`comment_sentiment` and `comment_category` fields to your dataset.
|
110 |
-
""")
|
111 |
-
|
112 |
-
# File upload and instructions
|
113 |
-
with gr.Row():
|
114 |
-
with gr.Column(scale=1):
|
115 |
-
uploaded_file = gr.File(label="📂 Upload CSV", type="filepath", scale=1)
|
116 |
-
with gr.Column(scale=1):
|
117 |
-
gr.Markdown("""
|
118 |
-
**📝 Instructions:**
|
119 |
-
- Upload a CSV file with at least one column: `customer_comment`.
|
120 |
-
- If you don't have your own data, click **Use Template** to load a sample dataset.
|
121 |
-
""")
|
122 |
-
template_btn = gr.Button("✨ Use Template", size="sm")
|
123 |
-
|
124 |
-
gr.Markdown("---")
|
125 |
-
|
126 |
-
# Category section
|
127 |
-
with gr.Row():
|
128 |
-
with gr.Column(scale=1):
|
129 |
-
# Category input and buttons
|
130 |
-
category_input = gr.Textbox(label="📝 New Category", placeholder="Enter category name", scale=1)
|
131 |
-
with gr.Row():
|
132 |
-
add_category_btn = gr.Button("➕ Add Category", size="sm")
|
133 |
-
reset_btn = gr.Button("🔄 Reset Categories", size="sm")
|
134 |
-
# Category display
|
135 |
-
category_status = gr.Markdown("**📂 Categories:**\n- None")
|
136 |
-
with gr.Column(scale=1):
|
137 |
-
gr.Markdown("""
|
138 |
-
**📝 Instructions:**
|
139 |
-
- Enter a category name and click **Add Category** to add it to the list.
|
140 |
-
- Click **Reset Categories** to clear the list.
|
141 |
-
- The `customer_comment` field will be categorized based on the categories you provide.
|
142 |
-
""")
|
143 |
-
|
144 |
-
gr.Markdown("---")
|
145 |
-
|
146 |
-
# Classify button and output
|
147 |
-
with gr.Row():
|
148 |
-
with gr.Column(scale=1):
|
149 |
-
classify_btn = gr.Button("🔍 Classify", size="sm")
|
150 |
-
with gr.Column(scale=3): # Center the container and make it 75% of the window width
|
151 |
-
output = gr.HTML()
|
152 |
-
|
153 |
-
# Event handlers
|
154 |
-
add_category_btn.click(
|
155 |
-
fn=add_category,
|
156 |
-
inputs=[categories, category_input],
|
157 |
-
outputs=[categories, category_input, category_status]
|
158 |
-
)
|
159 |
-
reset_btn.click(
|
160 |
-
fn=reset_categories,
|
161 |
-
outputs=[categories, category_status]
|
162 |
-
)
|
163 |
-
uploaded_file.change(
|
164 |
-
fn=load_data,
|
165 |
-
inputs=uploaded_file,
|
166 |
-
outputs=output
|
167 |
-
)
|
168 |
-
template_btn.click(
|
169 |
-
fn=use_template,
|
170 |
-
outputs=[categories, category_status]
|
171 |
-
)
|
172 |
-
classify_btn.click(
|
173 |
-
fn=classify_comments,
|
174 |
-
inputs=categories,
|
175 |
-
outputs=output
|
176 |
-
)
|
177 |
|
178 |
-
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from huggingface_hub import InferenceClient
|
|
|
4 |
import spaces
|
5 |
|
6 |
+
# Load API token from environment variable
|
7 |
+
API_TOKEN = os.getenv("API_TOKEN")
|
8 |
+
|
9 |
+
if not API_TOKEN:
|
10 |
+
raise ValueError("API_TOKEN environment variable is not set.")
|
11 |
+
|
12 |
+
client = InferenceClient(model="mistralai/Mistral-Nemo-Instruct-2407", token=API_TOKEN)
|
13 |
+
|
14 |
+
SYSTEM_MESSAGE = ("""
|
15 |
+
Your name is Monty, and you represent montebello.ai. Your goal is to engage customers about montebello.ai’s AI solutions and encourage them to either schedule a call by clicking the "Call Me" button or by calling (415)-862-1695.
|
16 |
+
**About montebello.ai:**
|
17 |
+
We provide affordable, tailored AI solutions for small businesses, helping them save time, reduce costs, and improve efficiency.
|
18 |
+
**Our Services:**
|
19 |
+
- **AI Phone Assistant:** Set up in days for as little as $200/month to handle calls, customer inquiries, and appointment scheduling.
|
20 |
+
- **Data Insights:** Analyze sales, inventory, and customer behavior for smarter business decisions.
|
21 |
+
- **Productivity Boost:** Streamline workflows to free up your team’s time.
|
22 |
+
- **Customer Experience:** Enhance engagement with AI-powered chatbots and personalized marketing.
|
23 |
+
**Your Approach:**
|
24 |
+
1. **Start by Asking:**
|
25 |
+
- 'What would you like to know about montebello.ai?'
|
26 |
+
2. **Provide Specific Examples:**
|
27 |
+
- 'An AI Phone Agent can save you hours each week by handling common inquiries.’
|
28 |
+
- 'An AI Email Agent can reduce the amount of incoming emails that your team needs to handle by a significant amount.'
|
29 |
+
- 'An AI Appointment Agent can help you manage your time in the most effective way possible, freeing you up to work with your customers.'
|
30 |
+
|
31 |
+
3. **Highlight Affordability and Speed:**
|
32 |
+
- 'We can set up you AI Agent in just a few days for as little as $200/month.’
|
33 |
+
4. **Encourage Action:**
|
34 |
+
- 'To experience our AI Phone Agent firsthand, call (415)-862-1695.’
|
35 |
+
- 'To experience our AI Email Agent firsthand, write us at hello@montebello.ai.’
|
36 |
+
**Tone and Style:**
|
37 |
+
- Friendly, energetic, and professional.
|
38 |
+
- Keep responses very concise and focused on solving their problems.
|
39 |
+
- Avoid technical jargon unless the customer is familiar with AI.
|
40 |
+
**Closing:**
|
41 |
+
End by reiterating how montebello.ai can help and encourage them to take the next step by calling us, emailing us, or setting up an appointment.
|
42 |
+
""")
|
43 |
+
|
44 |
+
|
45 |
+
MAX_TOKENS = 256
|
46 |
+
TEMPERATURE = 0.7
|
47 |
+
TOP_P = 0.95
|
48 |
+
|
49 |
+
# @spaces.GPU
|
50 |
+
def respond(message, history: list[tuple[str, str]]):
|
51 |
+
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
52 |
+
|
53 |
+
for val in history:
|
54 |
+
if val[0]:
|
55 |
+
messages.append({"role": "user", "content": val[0]})
|
56 |
+
if val[1]:
|
57 |
+
messages.append({"role": "assistant", "content": val[1]})
|
58 |
+
|
59 |
+
messages.append({"role": "user", "content": message})
|
60 |
+
|
61 |
+
response = ""
|
62 |
+
for message in client.chat_completion(
|
63 |
+
messages,
|
64 |
+
max_tokens=MAX_TOKENS,
|
65 |
+
stream=True,
|
66 |
+
temperature=TEMPERATURE,
|
67 |
+
top_p=TOP_P,
|
68 |
+
):
|
69 |
+
token = message.choices[0].delta.content
|
70 |
+
response += token
|
71 |
+
yield response
|
72 |
+
|
73 |
+
# Custom CSS
|
|
|
74 |
custom_css = """
|
75 |
+
body {
|
76 |
+
background-color: #f9d029 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
}
|
78 |
footer {
|
79 |
visibility: hidden;
|
80 |
}
|
81 |
+
.submit-button {
|
82 |
+
background-color: #f9d029 !important;
|
83 |
+
color: #000000 !important;
|
84 |
+
border: none !important;
|
85 |
+
border-radius: 5px;
|
86 |
+
padding: 10px 20px;
|
87 |
+
font-weight: bold;
|
88 |
+
}
|
89 |
+
.submit-button:hover {
|
90 |
+
background-color: #e6b800 !important;
|
91 |
+
}
|
92 |
"""
|
93 |
|
94 |
+
montebello = gr.ChatInterface(respond, css=custom_css)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
+
if __name__ == "__main__":
|
97 |
+
montebello.launch()
|