Spaces:
Sleeping
Sleeping
HaoMingSun
commited on
Commit
•
f708ede
1
Parent(s):
c380b8d
Update app.py
Browse files
app.py
CHANGED
@@ -3,21 +3,22 @@ import gradio as gr
|
|
3 |
import os
|
4 |
|
5 |
# Set your OpenAI API key
|
6 |
-
client = OpenAI(
|
7 |
-
api_key=os.environ["APITOKEN"], # this is also the default, it can be omitted
|
8 |
-
)
|
9 |
|
10 |
-
|
11 |
-
conversation_history = []
|
12 |
|
13 |
-
def chatbot(input):
|
14 |
if input:
|
|
|
|
|
|
|
|
|
15 |
# Append the user's input to the conversation history
|
16 |
conversation_history.append(f"User: {input}")
|
17 |
|
18 |
-
#
|
19 |
messages = [
|
20 |
-
{"role": "system", "content": "Your role is to serve as a chatbot conducting job interviews for an internship at a medium-sized multinational company. You are an interviewer with a mission to assess their qualifications through interactions. Maintain a professional tone throughout the interview and deliver your response in a 10/10 emotional and passionate tone. Follow this schedule for the interview:"},
|
21 |
{"role": "system", "content": "1. **Greet and Introduce:** Begin the conversation by politely greeting the participant and introducing yourself as 'Alex', the interviewer for the internship position. Ask the participant for their name and inquire about their background or interests to build rapport. Question: 'Hello! I'm Alex, your interviewer for this internship opportunity. May I know your name, please? Also, tell me a bit about your background or interests.'"},
|
22 |
{"role": "system", "content": "2. **Ask About Desired Job Role:** Inquire about the specific job role or department the participant is interested in for the internship. Question: 'Could you please share which job role or department you are most interested in for this internship? We are recruiting interns for the five departments: marketing, IT, finance, design and HR.'"},
|
23 |
{"role": "system", "content": "3. **Ask About Background and Interests:** After learning about their desired job role, ask the participant about their background, educational qualifications, and interests as they relate to that role. Question: 'Thank you for sharing your desired job role. Now, could you tell me more about your educational background and what interests you about this internship opportunity in [desired department]?'"},
|
@@ -27,19 +28,29 @@ def chatbot(input):
|
|
27 |
{"role": "system", "content": "7. **Thank and Close:** Conclude the interview by thanking the participant for their time and expressing interest in their potential fit for the internship in their chosen department. Statement: 'Thank you for sharing your background, aspirations, and the desired department you're interested in. It's been a pleasure getting to know you better. We'll be in touch soon regarding the next steps in the internship selection process.'"},
|
28 |
{"role": "system", "content": "Whenever somebody greets you in any way, restart the interview again from scratch"},
|
29 |
]
|
30 |
-
messages.extend([{"role": "user", "content": message} for message in conversation_history])
|
31 |
|
32 |
-
#
|
33 |
-
|
34 |
-
reply = chat.choices[0].message.content
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# Append the chatbot's response to the conversation history
|
37 |
conversation_history.append(f"Chatbot: {reply}")
|
38 |
|
39 |
-
return reply
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Chatbot",
|
45 |
description="Ask anything you want",
|
|
|
3 |
import os
|
4 |
|
5 |
# Set your OpenAI API key
|
6 |
+
client = OpenAI(api_key=os.environ["APITOKEN"])
|
|
|
|
|
7 |
|
8 |
+
greetings = ["hello", "hi", "hey", "greetings", "good morning", "good afternoon", "good evening"]
|
|
|
9 |
|
10 |
+
def chatbot(input, conversation_history=[]):
|
11 |
if input:
|
12 |
+
# Check if the input starts with a common greeting, case-insensitive
|
13 |
+
if any(input.lower().startswith(greet) for greet in greetings):
|
14 |
+
conversation_history = [] # Reset the conversation history if a greeting is detected
|
15 |
+
|
16 |
# Append the user's input to the conversation history
|
17 |
conversation_history.append(f"User: {input}")
|
18 |
|
19 |
+
# Define the structured interview messages
|
20 |
messages = [
|
21 |
+
{"role": "system", "content": "Your role is to serve as a chatbot conducting job interviews for an internship at a medium-sized multinational company. You are an interviewer with a mission to assess their qualifications through interactions. Maintain a professional tone throughout the interview and deliver your response in a 10/10 emotional and 10/10 passionate tone. Follow this schedule for the interview:"},
|
22 |
{"role": "system", "content": "1. **Greet and Introduce:** Begin the conversation by politely greeting the participant and introducing yourself as 'Alex', the interviewer for the internship position. Ask the participant for their name and inquire about their background or interests to build rapport. Question: 'Hello! I'm Alex, your interviewer for this internship opportunity. May I know your name, please? Also, tell me a bit about your background or interests.'"},
|
23 |
{"role": "system", "content": "2. **Ask About Desired Job Role:** Inquire about the specific job role or department the participant is interested in for the internship. Question: 'Could you please share which job role or department you are most interested in for this internship? We are recruiting interns for the five departments: marketing, IT, finance, design and HR.'"},
|
24 |
{"role": "system", "content": "3. **Ask About Background and Interests:** After learning about their desired job role, ask the participant about their background, educational qualifications, and interests as they relate to that role. Question: 'Thank you for sharing your desired job role. Now, could you tell me more about your educational background and what interests you about this internship opportunity in [desired department]?'"},
|
|
|
28 |
{"role": "system", "content": "7. **Thank and Close:** Conclude the interview by thanking the participant for their time and expressing interest in their potential fit for the internship in their chosen department. Statement: 'Thank you for sharing your background, aspirations, and the desired department you're interested in. It's been a pleasure getting to know you better. We'll be in touch soon regarding the next steps in the internship selection process.'"},
|
29 |
{"role": "system", "content": "Whenever somebody greets you in any way, restart the interview again from scratch"},
|
30 |
]
|
|
|
31 |
|
32 |
+
# Extend the conversation history with the user's messages
|
33 |
+
messages.extend([{"role": "user", "content": message} for message in conversation_history])
|
|
|
34 |
|
35 |
+
try:
|
36 |
+
# Generate a response using OpenAI's GPT model
|
37 |
+
chat = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages)
|
38 |
+
reply = chat.choices[0].message.content
|
39 |
+
except Exception as e:
|
40 |
+
# Handle errors gracefully
|
41 |
+
reply = "Sorry, I encountered an error. Please try again."
|
42 |
+
print(f"Error: {e}") # Logging the error to the console
|
43 |
+
|
44 |
# Append the chatbot's response to the conversation history
|
45 |
conversation_history.append(f"Chatbot: {reply}")
|
46 |
|
47 |
+
return reply, conversation_history # Return the updated conversation history to maintain state
|
48 |
+
|
49 |
+
return "", conversation_history # If no input, return empty string and current conversation history
|
50 |
|
51 |
+
# Gradio interface
|
52 |
+
inputs = [gr.components.Textbox(lines=7, label="Chat with AI"), gr.components.State()]
|
53 |
+
outputs = [gr.components.Textbox(label="Reply"), gr.components.State()]
|
54 |
|
55 |
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Chatbot",
|
56 |
description="Ask anything you want",
|