Spaces:
Sleeping
Sleeping
File size: 5,989 Bytes
430ade8 c397a05 3a8e74d 430ade8 c397a05 3a8e74d 430ade8 c397a05 430ade8 c397a05 430ade8 c397a05 29cff03 c397a05 29cff03 c397a05 29cff03 c397a05 430ade8 c397a05 430ade8 c397a05 29cff03 c397a05 29cff03 c397a05 3a8e74d c397a05 29cff03 c397a05 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 5bd35a5 29cff03 3a8e74d 29cff03 430ade8 c397a05 29cff03 c397a05 52f4bdb 430ade8 c397a05 29cff03 c397a05 29cff03 52f4bdb c397a05 29cff03 c397a05 430ade8 |
1 2 3 4 5 6 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
import gradio as gr
from huggingface_hub import InferenceClient
import re
import traceback
import os
# Initialize the Inference Client with your model
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=os.getenv("HUGGINGFACE_API_TOKEN"))
def respond(
message,
history,
max_tokens,
temperature,
top_p,
current_salary
):
"""
Respond function to handle the conversation and update salary based on AI's assessment.
"""
# Define the system message for the conversation (hidden from the user)
system_message = (
"You are a hiring manager negotiating a job offer with a candidate. "
"Your initial salary offer is $60,000. "
"Engage in a negotiation with the candidate, adjusting your offer based on their arguments."
)
# Initialize the messages with the system prompt
messages = [{"role": "system", "content": system_message}]
# Append the conversation history to messages
for user_msg, ai_msg in history:
if user_msg:
messages.append({"role": "user", "content": user_msg})
if ai_msg:
messages.append({"role": "assistant", "content": ai_msg})
# Append the latest user message
messages.append({"role": "user", "content": message})
# Generate the AI's response to the user's message
try:
response = client.chat_completion(
messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
stop=None
).get("choices")[0].get("message").get("content")
except Exception as e:
response = f"An error occurred while communicating with the AI: {e}"
traceback.print_exc()
# Append the AI's response to the history
history.append((message, response))
# Now, send the conversation to the AI to get the updated salary
# Prepare the salary assessment prompt
salary_assessment_prompt = (
"As the hiring manager, based on the conversation so far, "
"what salary do you now offer to the candidate? "
"Please provide only the salary amount as a number, without any additional text."
)
# Prepare the messages for salary assessment
assessment_messages = [{"role": "system", "content": salary_assessment_prompt}]
# Include the conversation history
for user_msg, ai_msg in history:
if user_msg:
assessment_messages.append({"role": "user", "content": user_msg})
if ai_msg:
assessment_messages.append({"role": "assistant", "content": ai_msg})
# Generate the AI's salary assessment
try:
salary_response = client.chat_completion(
assessment_messages,
max_tokens=10,
temperature=temperature,
top_p=top_p,
stop=None
).get("choices")[0].get("message").get("content")
except Exception as e:
salary_response = f"An error occurred while assessing salary: {e}"
traceback.print_exc()
salary_response = str(current_salary)
# Parse the salary from the AI's response
try:
# Remove any non-digit characters
salary_str = re.sub(r'[^\d]', '', salary_response)
if salary_str:
new_salary = int(salary_str)
if new_salary != current_salary:
# Update current_salary
current_salary = new_salary
else:
# If parsing fails, keep the current salary
pass
except Exception as e:
# If parsing fails, keep the current salary
pass
return history, "", current_salary # Return history, clear input, and update salary internally
def reset_game():
"""
Function to reset the game to initial state.
"""
return [], 60000 # Reset history and salary to $60,000
# Define the Gradio Blocks layout
with gr.Blocks() as demo:
gr.Markdown("# 💼 Salary Negotiation Game")
gr.Markdown(
"""
**Objective:** Negotiate your salary starting from $60,000.
**Instructions:**
- Use the chat to negotiate your salary with the hiring manager.
- Provide compelling reasons for a higher salary.
- The hiring manager will consider your arguments and may adjust the offer.
"""
)
# Chat history to keep track of the conversation
chat_history = gr.State([])
# Current salary (hidden from the user)
current_salary_state = gr.State(60000)
# Chat Interface
chatbot = gr.Chatbot()
# User input
user_input = gr.Textbox(
label="Your Message",
placeholder="Enter your negotiation message here...",
lines=2
)
send_button = gr.Button("Send")
def handle_message(message, history, max_tokens, temperature, top_p, current_salary):
"""
Handles user messages and updates the conversation history and salary.
"""
history, _, current_salary = respond(message, history, max_tokens, temperature, top_p, current_salary)
return history, "", current_salary
send_button.click(
handle_message,
inputs=[
user_input,
chat_history,
gr.Number(value=512, label="Max New Tokens"),
gr.Number(value=0.7, label="Temperature"),
gr.Number(value=0.95, label="Top-p"),
current_salary_state # Pass the current salary
],
outputs=[
chatbot,
user_input, # Clear the input textbox
current_salary_state # Update the salary internally
]
)
# Reset button to restart the game
reset_btn = gr.Button("Reset Game")
reset_btn.click(fn=reset_game, inputs=None, outputs=[chat_history, current_salary_state])
gr.Markdown(
"""
---
*Developed with ❤️ using Gradio and Hugging Face.*
"""
)
if __name__ == "__main__":
demo.launch()
|