import gradio as gr from huggingface_hub import InferenceClient import re import traceback import os # Initialize the Inference Client with your model # Ensure you have set the HUGGINGFACE_API_TOKEN environment variable client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=os.getenv("HUGGINGFACE_API_TOKEN")) def respond( message, history, max_tokens, temperature, top_p, current_salary ): """ Handles the negotiation conversation and assesses salary changes. """ # Define the system message internally (hidden from the user) system_message = ( "You are a hiring manager negotiating a job offer. " "Your initial offer is $60,000. " "Only increase the salary if the candidate provides compelling reasons. " "Communicate any salary offers explicitly in your responses, " "and only mention the salary when you are making an offer." ) # 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 response (Negotiation Interaction) try: ai_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: ai_response = f"An error occurred while communicating with the AI: {e}" traceback.print_exc() # Append AI response to history history.append((message, ai_response)) # Assess the negotiation outcome (Salary Assessment Interaction) # Prepare the assessment prompt assessment_prompt = ( "Based on the following conversation, determine if the candidate's salary should be increased, " "decreased, or remain the same compared to $60,000. Respond with one of the following words only: 'increase', 'decrease', or 'same'.\n\n" "Conversation:\n" ) for user_msg, ai_msg in history: if user_msg: assessment_prompt += f"User: {user_msg}\n" if ai_msg: assessment_prompt += f"AI: {ai_msg}\n" # Send the assessment prompt to the AI try: assessment_response = client.chat_completion( [{"role": "user", "content": assessment_prompt}], max_tokens=10, temperature=0, top_p=1.0, stop=None ).get("choices")[0].get("message").get("content").strip().lower() except Exception as e: assessment_response = f"An error occurred during salary assessment: {e}" traceback.print_exc() # Determine salary adjustment based on assessment if assessment_response == "increase": increment = 5000 # Fixed increment; modify as desired current_salary += increment negotiation_result = f"🎉 Negotiation successful! Your salary has been increased to ${current_salary:,}." # Append the negotiation result to history as an AI message history.append(("", negotiation_result)) elif assessment_response == "decrease": decrement = 5000 # Fixed decrement; modify as desired current_salary -= decrement # Ensure salary doesn't go below $60,000 if current_salary < 60000: current_salary = 60000 negotiation_result = f"⚠️ Negotiation resulted in no change. Your salary remains at $60,000." else: negotiation_result = f"⚠️ Negotiation resulted in a salary decrease to ${current_salary:,}." history.append(("", negotiation_result)) elif assessment_response == "same": negotiation_result = f"✅ No change to your salary. It remains at ${current_salary:,}." history.append(("", negotiation_result)) else: # If the response is unclear, do not change the salary negotiation_result = "🤔 I couldn't determine the salary adjustment based on the conversation." history.append(("", negotiation_result)) return history, current_salary def reset_game(): """ Resets the game to its 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 interface 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. """ ) with gr.Row(): with gr.Column(scale=1): # Reset button to restart the game reset_btn = gr.Button("Reset Game") reset_btn.click(fn=reset_game, inputs=None, outputs=[gr.State(), gr.State()]) with gr.Column(scale=3): # Chat history to keep track of the conversation chat_history = gr.State([]) # 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, updates the conversation history, and manages salary adjustments. """ history, current_salary = respond(message, history, max_tokens, temperature, top_p, current_salary) return history, current_salary, "" # Clear the input textbox send_button.click( handle_message, inputs=[ user_input, chat_history, gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"), gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), gr.Slider( minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)", ), gr.State(60000) # Initial salary ], outputs=[ chatbot, gr.State(), # Update the salary state user_input # Clear the input textbox ] ) gr.Markdown( """ --- *Developed with ❤️ using Gradio and Hugging Face.* """ ) if __name__ == "__main__": demo.launch()