Roxyzhang commited on
Commit
14cf3da
·
verified ·
1 Parent(s): ed5083b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -3,19 +3,18 @@ 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
- # Initialize a list to store the conversation history
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
- # Create a list of messages based on the conversation history
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 unemotional and 10/10 dispassionate 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.'"},
@@ -26,21 +25,26 @@ def chatbot(input):
26
  {"role": "system", "content": "6. **Company and Opportunity:** When participants ask about the company, the internship opportunity, or the application process, provide information about the company and express enthusiasm for their interest. - Response: 'I'd be happy to provide more information! Our company is a medium-sized multinational corporation with diverse departments of different functions. As for the internship opportunity...'"},
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
- # Use OpenAI's GPT-3.5 Turbo model to generate a response
33
  chat = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages)
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
- inputs = gr.components.Textbox(lines=7, label="Chat with AI")
42
- outputs = gr.components.Textbox(label="Reply")
 
43
 
44
  gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Chatbot",
45
- description="Ask anything you want",
46
- theme="Default").launch()
 
3
  import os
4
 
5
  # Set your OpenAI API key
6
+ client = OpenAI(api_key=os.environ["APITOKEN"])
 
 
7
 
8
+ def chatbot(input, conversation_history=[]):
 
 
 
9
  if input:
10
+ # Restart the conversation if greeted
11
+ if "hello" in input.lower() or "hi" in input.lower():
12
+ conversation_history = [] # Reset the conversation history
13
+
14
  # Append the user's input to the conversation history
15
  conversation_history.append(f"User: {input}")
16
 
17
+ # Define the structured interview messages
18
  messages = [
19
  {"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 unemotional and 10/10 dispassionate tone. Follow this schedule for the interview:"},
20
  {"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.'"},
 
25
  {"role": "system", "content": "6. **Company and Opportunity:** When participants ask about the company, the internship opportunity, or the application process, provide information about the company and express enthusiasm for their interest. - Response: 'I'd be happy to provide more information! Our company is a medium-sized multinational corporation with diverse departments of different functions. As for the internship opportunity...'"},
26
  {"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.'"},
27
  {"role": "system", "content": "Whenever somebody greets you in any way, restart the interview again from scratch"},
28
+ ]
29
+
30
+ # Extend the conversation history with the user's messages
31
  messages.extend([{"role": "user", "content": message} for message in conversation_history])
32
 
33
+ # Generate a response using OpenAI's GPT model
34
  chat = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages)
35
  reply = chat.choices[0].message.content
36
 
37
  # Append the chatbot's response to the conversation history
38
  conversation_history.append(f"Chatbot: {reply}")
39
 
40
+ return reply, conversation_history # Return the updated conversation history to maintain state
41
+
42
+ return "", conversation_history # If no input, return empty string and current conversation history
43
 
44
+ # Gradio interface
45
+ inputs = [gr.components.Textbox(lines=7, label="Chat with AI"), gr.components.State()]
46
+ outputs = [gr.components.Textbox(label="Reply"), gr.components.State()]
47
 
48
  gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Chatbot",
49
+ description="This chatbot is designed to conduct structured interviews. Please begin by introducing yourself.",
50
+ theme="Default", live=True).launch()