LuckyHappyFish commited on
Commit
5bd35a5
·
1 Parent(s): 52f4bdb
Files changed (1) hide show
  1. app.py +117 -72
app.py CHANGED
@@ -5,6 +5,7 @@ import traceback
5
  import os
6
 
7
  # Initialize the Inference Client with your model
 
8
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=os.getenv("HUGGINGFACE_API_TOKEN"))
9
 
10
  def respond(
@@ -16,13 +17,15 @@ def respond(
16
  current_salary
17
  ):
18
  """
19
- Respond function to handle the conversation and negotiate salary.
20
  """
21
  # Define the system message internally (hidden from the user)
22
  system_message = (
23
  "You are a hiring manager negotiating a job offer. "
24
- "Your initial offer is $60,000. Only increase the salary if the candidate provides compelling reasons. "
25
- "Communicate any salary offers explicitly in your responses, and only mention the salary when you are making an offer."
 
 
26
  )
27
 
28
  # Initialize the messages with the system prompt
@@ -38,9 +41,9 @@ def respond(
38
  # Append the latest user message
39
  messages.append({"role": "user", "content": message})
40
 
41
- # Generate the AI response
42
  try:
43
- response = client.chat_completion(
44
  messages,
45
  max_tokens=max_tokens,
46
  temperature=temperature,
@@ -48,35 +51,69 @@ def respond(
48
  stop=None
49
  ).get("choices")[0].get("message").get("content")
50
  except Exception as e:
51
- response = f"An error occurred while communicating with the AI: {e}"
52
  traceback.print_exc()
53
 
54
  # Append AI response to history
55
- history.append((message, response))
56
-
57
- # Extract salary offers from AI's response
58
- # Use regex to find salary amounts in the response
59
- salary_pattern = r'\$\s?([\d,]+)'
60
- matches = re.findall(salary_pattern, response)
61
- offered_salary = None
62
-
63
- if matches:
64
- # Convert extracted salary strings to integers
65
- for match in matches:
66
- salary_str = match.replace(',', '').strip()
67
- try:
68
- salary_value = int(salary_str)
69
- if salary_value > current_salary:
70
- offered_salary = salary_value
71
- current_salary = offered_salary
72
- except ValueError:
73
- continue # Ignore invalid salary strings
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  return history, current_salary
76
 
77
  def reset_game():
78
  """
79
- Function to reset the game to initial state.
80
  """
81
  return [], 60000 # Reset history and salary to $60,000
82
 
@@ -85,57 +122,65 @@ with gr.Blocks() as demo:
85
  gr.Markdown("# 💼 Salary Negotiation Game")
86
  gr.Markdown(
87
  """
88
- **Objective:** Negotiate your salary starting from $60,000.
89
-
90
  **Instructions:**
91
- - Use the chat to negotiate your salary with the hiring manager.
92
  - Provide compelling reasons for a higher salary.
93
  - The hiring manager will consider your arguments and may adjust the offer.
94
  """
95
  )
96
-
97
- # Chat history to keep track of the conversation
98
- chat_history = gr.State([])
99
-
100
- # Chat Interface
101
- chatbot = gr.Chatbot()
102
-
103
- # User input
104
- user_input = gr.Textbox(
105
- label="Your Message",
106
- placeholder="Enter your negotiation message here...",
107
- lines=2
108
- )
109
- send_button = gr.Button("Send")
110
-
111
- def handle_message(message, history, max_tokens, temperature, top_p, current_salary):
112
- """
113
- Handles user messages and updates the conversation history and salary.
114
- """
115
- history, current_salary = respond(message, history, max_tokens, temperature, top_p, current_salary)
116
- return history, "", current_salary
117
-
118
- send_button.click(
119
- handle_message,
120
- inputs=[
121
- user_input,
122
- chat_history,
123
- gr.Number(value=512, label="Max New Tokens"),
124
- gr.Number(value=0.7, label="Temperature"),
125
- gr.Number(value=0.95, label="Top-p"),
126
- gr.State(60000) # Initial salary
127
- ],
128
- outputs=[
129
- chatbot,
130
- user_input, # Clear the input textbox
131
- gr.State() # Update the salary internally
132
- ]
133
- )
134
-
135
- # Reset button to restart the game
136
- reset_btn = gr.Button("Reset Game")
137
- reset_btn.click(fn=reset_game, inputs=None, outputs=[chat_history, gr.State(60000)])
138
-
 
 
 
 
 
 
 
 
139
  gr.Markdown(
140
  """
141
  ---
 
5
  import os
6
 
7
  # Initialize the Inference Client with your model
8
+ # Ensure you have set the HUGGINGFACE_API_TOKEN environment variable
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=os.getenv("HUGGINGFACE_API_TOKEN"))
10
 
11
  def respond(
 
17
  current_salary
18
  ):
19
  """
20
+ Handles the negotiation conversation and assesses salary changes.
21
  """
22
  # Define the system message internally (hidden from the user)
23
  system_message = (
24
  "You are a hiring manager negotiating a job offer. "
25
+ "Your initial offer is $60,000. "
26
+ "Only increase the salary if the candidate provides compelling reasons. "
27
+ "Communicate any salary offers explicitly in your responses, "
28
+ "and only mention the salary when you are making an offer."
29
  )
30
 
31
  # Initialize the messages with the system prompt
 
41
  # Append the latest user message
42
  messages.append({"role": "user", "content": message})
43
 
44
+ # Generate the AI response (Negotiation Interaction)
45
  try:
46
+ ai_response = client.chat_completion(
47
  messages,
48
  max_tokens=max_tokens,
49
  temperature=temperature,
 
51
  stop=None
52
  ).get("choices")[0].get("message").get("content")
53
  except Exception as e:
54
+ ai_response = f"An error occurred while communicating with the AI: {e}"
55
  traceback.print_exc()
56
 
57
  # Append AI response to history
58
+ history.append((message, ai_response))
59
+
60
+ # Assess the negotiation outcome (Salary Assessment Interaction)
61
+ # Prepare the assessment prompt
62
+ assessment_prompt = (
63
+ "Based on the following conversation, determine if the candidate's salary should be increased, "
64
+ "decreased, or remain the same compared to $60,000. Respond with one of the following words only: 'increase', 'decrease', or 'same'.\n\n"
65
+ "Conversation:\n"
66
+ )
67
+
68
+ for user_msg, ai_msg in history:
69
+ if user_msg:
70
+ assessment_prompt += f"User: {user_msg}\n"
71
+ if ai_msg:
72
+ assessment_prompt += f"AI: {ai_msg}\n"
73
+
74
+ # Send the assessment prompt to the AI
75
+ try:
76
+ assessment_response = client.chat_completion(
77
+ [{"role": "user", "content": assessment_prompt}],
78
+ max_tokens=10,
79
+ temperature=0,
80
+ top_p=1.0,
81
+ stop=None
82
+ ).get("choices")[0].get("message").get("content").strip().lower()
83
+ except Exception as e:
84
+ assessment_response = f"An error occurred during salary assessment: {e}"
85
+ traceback.print_exc()
86
+
87
+ # Determine salary adjustment based on assessment
88
+ if assessment_response == "increase":
89
+ increment = 5000 # Fixed increment; modify as desired
90
+ current_salary += increment
91
+ negotiation_result = f"🎉 Negotiation successful! Your salary has been increased to ${current_salary:,}."
92
+ # Append the negotiation result to history as an AI message
93
+ history.append(("", negotiation_result))
94
+ elif assessment_response == "decrease":
95
+ decrement = 5000 # Fixed decrement; modify as desired
96
+ current_salary -= decrement
97
+ # Ensure salary doesn't go below $60,000
98
+ if current_salary < 60000:
99
+ current_salary = 60000
100
+ negotiation_result = f"⚠️ Negotiation resulted in no change. Your salary remains at $60,000."
101
+ else:
102
+ negotiation_result = f"⚠️ Negotiation resulted in a salary decrease to ${current_salary:,}."
103
+ history.append(("", negotiation_result))
104
+ elif assessment_response == "same":
105
+ negotiation_result = f"✅ No change to your salary. It remains at ${current_salary:,}."
106
+ history.append(("", negotiation_result))
107
+ else:
108
+ # If the response is unclear, do not change the salary
109
+ negotiation_result = "🤔 I couldn't determine the salary adjustment based on the conversation."
110
+ history.append(("", negotiation_result))
111
 
112
  return history, current_salary
113
 
114
  def reset_game():
115
  """
116
+ Resets the game to its initial state.
117
  """
118
  return [], 60000 # Reset history and salary to $60,000
119
 
 
122
  gr.Markdown("# 💼 Salary Negotiation Game")
123
  gr.Markdown(
124
  """
125
+ **Objective:** Negotiate your salary starting from \$60,000.
126
+
127
  **Instructions:**
128
+ - Use the chat interface to negotiate your salary with the hiring manager.
129
  - Provide compelling reasons for a higher salary.
130
  - The hiring manager will consider your arguments and may adjust the offer.
131
  """
132
  )
133
+
134
+ with gr.Row():
135
+ with gr.Column(scale=1):
136
+ # Reset button to restart the game
137
+ reset_btn = gr.Button("Reset Game")
138
+ reset_btn.click(fn=reset_game, inputs=None, outputs=[gr.State(), gr.State()])
139
+ with gr.Column(scale=3):
140
+ # Chat history to keep track of the conversation
141
+ chat_history = gr.State([])
142
+
143
+ # Chat Interface
144
+ chatbot = gr.Chatbot()
145
+
146
+ # User input
147
+ user_input = gr.Textbox(
148
+ label="Your Message",
149
+ placeholder="Enter your negotiation message here...",
150
+ lines=2
151
+ )
152
+ send_button = gr.Button("Send")
153
+
154
+ def handle_message(message, history, max_tokens, temperature, top_p, current_salary):
155
+ """
156
+ Handles user messages, updates the conversation history, and manages salary adjustments.
157
+ """
158
+ history, current_salary = respond(message, history, max_tokens, temperature, top_p, current_salary)
159
+ return history, current_salary, "" # Clear the input textbox
160
+
161
+ send_button.click(
162
+ handle_message,
163
+ inputs=[
164
+ user_input,
165
+ chat_history,
166
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
167
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
168
+ gr.Slider(
169
+ minimum=0.1,
170
+ maximum=1.0,
171
+ value=0.95,
172
+ step=0.05,
173
+ label="Top-p (nucleus sampling)",
174
+ ),
175
+ gr.State(60000) # Initial salary
176
+ ],
177
+ outputs=[
178
+ chatbot,
179
+ gr.State(), # Update the salary state
180
+ user_input # Clear the input textbox
181
+ ]
182
+ )
183
+
184
  gr.Markdown(
185
  """
186
  ---