Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import requests | |
| import random | |
| import os | |
| api_key = os.getenv("apikey") | |
| url = "https://talent-interview-prep-conversational-model.multimodal.dev/" | |
| # Predefined questions | |
| questions = { | |
| "Can you start by giving us an overview of your background and how your experience has prepared you specifically for the Product Manager role here at InnovateTech Solutions?", | |
| "What specifically interested you in this Product Manager role at InnovateTech Solutions, and how do you see your skills and experiences aligning with the responsibilities outlined in the job description?", | |
| "Can you tell us about your career aspirations and motivations? How do you see this role helping you achieve your long-term career goals?", | |
| "What do you consider to be your unique professional strengths and competencies that would contribute to your success as a Product Manager at our company? Can you provide an example where one of these strengths played a critical role in a project?", | |
| "Could you walk us through a time when you defined and communicated a product vision and developed a roadmap? How did you ensure alignment with the company's strategic goals and market trends?", | |
| "You've mentioned expertise in agile methodologies. Can you describe a specific project where you led a cross-functional team using Scrum or Kanban? What challenges did you face, and how did you overcome them?", | |
| "Can you provide an example of a situation where you had to manage conflicting priorities among stakeholders? How did you handle it, and what was the outcome?", | |
| "Now that we've discussed the role and responsibilities, do you have any questions about our company or the Product Manager position at InnovateTech Solutions?" | |
| } | |
| # Create a list of full questions (key + value) for the dropdown | |
| full_questions = [f"{key}: {value}" for key, value in questions.items()] | |
| # Initialize the session ID | |
| session_id = random.randint(3000, 20000) | |
| print(f"Session ID initialized: {session_id}") | |
| # Define the API calling function | |
| def api_call(interview_question, user_input): | |
| print(f"Calling API with question: {interview_question}") | |
| print(f"User input: {user_input}") | |
| headers = { | |
| "x-api-key": api_key, | |
| "Content-Type": "application/json" | |
| } | |
| data = { | |
| "session_id": session_id, | |
| "job_title": "Product Manager", | |
| "company_name": "InnovateTech Solutions", | |
| "interview_question": interview_question, | |
| "user_input": user_input | |
| } | |
| response = requests.post(url, headers=headers, json=data) | |
| print(f"API Response status code: {response.status_code}") | |
| try: | |
| json_response = response.json() | |
| print(f"API Response: {json_response['response']}") | |
| return json_response["response"] | |
| except: | |
| print("Error: Invalid JSON response") | |
| return "Error: Invalid JSON response" | |
| # Gradio interface | |
| with gr.Blocks() as demo: | |
| question_dropdown = gr.Dropdown(choices=full_questions, label="Select Question") | |
| chatbot = gr.Chatbot() | |
| msg = gr.Textbox(label="Your Response") | |
| send_btn = gr.Button(value="Send", variant="primary") | |
| def respond(message, history): | |
| print(f"Respond function called with message: {message}") | |
| if not history: | |
| # This is the first interaction, so we use the selected question | |
| question = question_dropdown.value | |
| print(f"First interaction, selected question: {question}") | |
| last_question = question | |
| else: | |
| # Get the last question asked from the local history | |
| last_question = history[-1][0] if history[-1][0] != "" else history[-2][0] | |
| print(f"Using last question: {last_question}") | |
| # Make the API call with the user's response | |
| bot_message = api_call(last_question, message) | |
| print(f"Bot response (now shown in Gradio interface): {bot_message}") | |
| return history + [(message, bot_message)], "" # Return empty string to clear the textbox | |
| def update_chatbot(question): | |
| global session_id | |
| print(f"Updating chatbot with new question: {question}") | |
| # Change session ID only when a new question is selected | |
| session_id = random.randint(3000, 20000) | |
| print(f"New session ID: {session_id}") | |
| return [(question, "")] | |
| # Link the Textbox and Button to trigger the response function | |
| msg.submit(fn=respond, inputs=[msg, chatbot], outputs=[chatbot, msg]) | |
| send_btn.click(fn=respond, inputs=[msg, chatbot], outputs=[chatbot, msg]) | |
| question_dropdown.change(fn=update_chatbot, inputs=[question_dropdown], outputs=[chatbot]) | |
| # Arrange the elements horizontally: Textbox and Button | |
| with gr.Row(): | |
| msg | |
| send_btn | |
| print("Launching Gradio interface...") | |
| demo.launch() |