LuckyHappyFish commited on
Commit
c397a05
·
1 Parent(s): 57c54bb

Salary Negotiation

Browse files
Files changed (1) hide show
  1. app.py +163 -41
app.py CHANGED
@@ -1,64 +1,186 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
9
 
10
  def respond(
11
  message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
  max_tokens,
15
  temperature,
16
  top_p,
 
17
  ):
 
 
 
 
 
 
 
 
 
 
 
18
  messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
 
 
26
  messages.append({"role": "user", "content": message})
27
-
28
  response = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
 
 
 
 
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import re
4
+ import traceback
5
 
6
+ # Initialize the Inference Client with your model
 
 
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
+ # Regular expression to validate YouTube URLs (modify if needed)
10
+ YOUTUBE_URL_PATTERN = re.compile(
11
+ r'(https?://)?(www\.)?(youtube\.com|youtu\.?be)/.+'
12
+ )
13
 
14
  def respond(
15
  message,
16
+ history,
 
17
  max_tokens,
18
  temperature,
19
  top_p,
20
+ current_salary
21
  ):
22
+ """
23
+ Respond function to handle the conversation and negotiate salary.
24
+ """
25
+ # Define the system message internally (hidden from the user)
26
+ system_message = (
27
+ "You are a hiring manager negotiating a job offer. "
28
+ "Your goal is to offer a fair salary based on the candidate's negotiation skills. "
29
+ "Start the negotiation with an initial offer of $60,000."
30
+ )
31
+
32
+ # Initialize the messages with the system prompt
33
  messages = [{"role": "system", "content": system_message}]
34
+
35
+ # Append the conversation history to messages
36
+ for user_msg, ai_msg in history:
37
+ if user_msg:
38
+ messages.append({"role": "user", "content": user_msg})
39
+ if ai_msg:
40
+ messages.append({"role": "assistant", "content": ai_msg})
41
+
42
+ # Append the latest user message
43
  messages.append({"role": "user", "content": message})
44
+
45
  response = ""
46
+ success = False # Flag to determine if negotiation was successful
47
+
48
+ try:
49
+ # Generate the AI response
50
+ for msg in client.chat_completion(
51
+ messages,
52
+ max_tokens=max_tokens,
53
+ stream=True,
54
+ temperature=temperature,
55
+ top_p=top_p,
56
+ ):
57
+ token = msg.choices[0].delta.content
58
+ response += token
59
+ yield response
60
+
61
+ # Simple logic to determine negotiation success based on AI's response
62
+ success_keywords = ["agreed", "accepted", "increase", "raised", "enhanced", "approved", "offer"]
63
+
64
+ # Check if any success keywords are in the AI's response (case-insensitive)
65
+ if any(keyword.lower() in response.lower() for keyword in success_keywords):
66
+ # Increment the salary by a fixed amount
67
+ increment = 5000 # Fixed increment; modify as desired
68
+ current_salary += increment
69
+ success = True
70
+ yield f"\n\n🎉 Negotiation successful! Your salary has been increased to ${current_salary:,}."
71
+
72
+ except Exception as e:
73
+ response = f"An error occurred while communicating with the AI: {e}"
74
+ yield response
75
+ traceback.print_exc()
76
+
77
+ # Update the salary display if negotiation was successful
78
+ if success:
79
+ yield gr.update(value=current_salary)
80
 
81
+ def reset_game():
82
+ """
83
+ Function to reset the game to initial state.
84
+ """
85
+ return [], 60000 # Reset history and salary to 60k
 
 
 
86
 
87
+ # Define the Gradio Blocks layout
88
+ with gr.Blocks() as demo:
89
+ gr.Markdown("# 💼 Salary Negotiation Game")
90
+ gr.Markdown(
91
+ """
92
+ **Objective:** Negotiate with the employer to increase your salary from \$60,000.
93
+
94
+ **Instructions:**
95
+ - Use the chat interface to negotiate.
96
+ - If you negotiate successfully, your salary will increase.
97
+ - Aim to reach the highest salary possible through effective negotiation.
98
+ """
99
+ )
100
 
101
+ with gr.Row():
102
+ with gr.Column(scale=1):
103
+ # Display the current salary
104
+ salary_display = gr.Number(
105
+ value=60000,
106
+ label="Current Salary (\$)",
107
+ interactive=False,
108
+ precision=0
109
+ )
110
+ # Reset button to restart the game
111
+ reset_btn = gr.Button("Reset Game")
112
+ reset_btn.click(fn=reset_game, inputs=None, outputs=[gr.State(), salary_display])
113
 
114
+ with gr.Column(scale=3):
115
+ # Chat history to keep track of the conversation
116
+ chat_history = gr.State([])
117
+
118
+ # Chat Interface
119
+ chatbot = gr.Chatbot()
120
+
121
+ # User input
122
+ user_input = gr.Textbox(
123
+ label="Your Message",
124
+ placeholder="Enter your negotiation message here...",
125
+ lines=2
126
+ )
127
+ send_button = gr.Button("Send")
128
+
129
+ def handle_message(message, history, max_tokens, temperature, top_p, current_salary):
130
+ """
131
+ Handles user messages and updates the conversation history and salary.
132
+ """
133
+ generator = respond(message, history, max_tokens, temperature, top_p, current_salary)
134
+ try:
135
+ # Get the initial AI response
136
+ response = next(generator)
137
+ # Update history
138
+ history.append((message, response))
139
+ # Process the rest of the generator
140
+ try:
141
+ while True:
142
+ additional_response = next(generator)
143
+ response += additional_response
144
+ # Check for negotiation success message
145
+ if "Negotiation successful" in additional_response:
146
+ current_salary = int(re.search(r'\$(\d{1,3}(?:,\d{3})*)', additional_response).group(1).replace(',', ''))
147
+ yield (history, current_salary)
148
+ except StopIteration:
149
+ pass
150
+ except StopIteration:
151
+ pass
152
+ except Exception as e:
153
+ history.append((message, f"An error occurred: {e}"))
154
+ yield (history, current_salary)
155
+
156
+ send_button.click(
157
+ handle_message,
158
+ inputs=[
159
+ user_input,
160
+ chat_history,
161
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
162
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
163
+ gr.Slider(
164
+ minimum=0.1,
165
+ maximum=1.0,
166
+ value=0.95,
167
+ step=0.05,
168
+ label="Top-p (nucleus sampling)",
169
+ ),
170
+ salary_display
171
+ ],
172
+ outputs=[
173
+ chatbot,
174
+ salary_display
175
+ ]
176
+ )
177
 
178
+ gr.Markdown(
179
+ """
180
+ ---
181
+ *Developed with ❤️ using Gradio and Hugging Face.*
182
+ """
183
+ )
184
 
185
  if __name__ == "__main__":
186
  demo.launch()