Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,44 +5,48 @@ from openai import OpenAI
|
|
| 5 |
# Initialize the OpenAI client
|
| 6 |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
-
#
|
| 9 |
def respond(
|
| 10 |
message,
|
| 11 |
-
history: list[
|
| 12 |
system_message,
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
max_tokens,
|
| 19 |
temperature,
|
| 20 |
top_p,
|
| 21 |
):
|
| 22 |
-
#
|
| 23 |
enhanced_system_message = (
|
| 24 |
f"{system_message}\n\n"
|
| 25 |
-
f"
|
| 26 |
-
f"
|
| 27 |
-
f"
|
| 28 |
-
f"
|
|
|
|
| 29 |
)
|
| 30 |
-
if ask_for_topic_suggestions:
|
| 31 |
-
enhanced_system_message += "\nThe user is also asking for topic suggestions to address their customer's needs."
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
messages = [{"role": "system", "content": enhanced_system_message}]
|
| 35 |
-
for
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
messages.append({"role": "user", "content": message})
|
| 40 |
|
| 41 |
-
#
|
| 42 |
response_text = ""
|
| 43 |
try:
|
| 44 |
response = client.chat.completions.create(
|
| 45 |
-
model="gpt-4o
|
| 46 |
messages=messages,
|
| 47 |
max_tokens=max_tokens,
|
| 48 |
temperature=temperature,
|
|
@@ -57,38 +61,37 @@ def respond(
|
|
| 57 |
except Exception as e:
|
| 58 |
yield f"❌ An error occurred: {str(e)}"
|
| 59 |
|
| 60 |
-
# Gradio interface
|
| 61 |
demo = gr.ChatInterface(
|
| 62 |
fn=respond,
|
| 63 |
additional_inputs=[
|
| 64 |
gr.Textbox(
|
| 65 |
-
value="You are a friendly Chatbot, a
|
| 66 |
label="Instructions to Bot",
|
| 67 |
),
|
| 68 |
-
gr.Textbox(label="
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
gr.Textbox(
|
| 70 |
-
label="
|
| 71 |
-
placeholder="Describe your
|
| 72 |
),
|
| 73 |
gr.Textbox(
|
| 74 |
-
label="
|
| 75 |
-
placeholder="Describe your
|
| 76 |
),
|
| 77 |
-
gr.Textbox(label="
|
| 78 |
-
gr.Checkbox(label="Ask for Topic Suggestions", value=False),
|
| 79 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 80 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 81 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 82 |
],
|
| 83 |
-
title="
|
| 84 |
-
description=
|
| 85 |
-
|
| 86 |
-
"Based on your input. Powered by OpenAI GPT-4o-mini and domain expertise. "
|
| 87 |
-
"Expand Additional Inputs by clicking on the arrow, input more details about your customers, then a message describing what you need the assistant to do for you. "
|
| 88 |
-
"Developed by wn. Disclaimer: AI can make mistakes. Use with caution and at your own risk!"
|
| 89 |
-
),
|
| 90 |
-
type="messages", # ensures compatibility with Gradio streaming ChatInterface
|
| 91 |
)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
demo.launch()
|
|
|
|
|
|
| 5 |
# Initialize the OpenAI client
|
| 6 |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
+
# Streaming response function
|
| 9 |
def respond(
|
| 10 |
message,
|
| 11 |
+
history: list[tuple[str, str]],
|
| 12 |
system_message,
|
| 13 |
+
role,
|
| 14 |
+
ad,
|
| 15 |
+
education,
|
| 16 |
+
experience,
|
| 17 |
+
skills,
|
| 18 |
+
ask_for_skills_suggestions,
|
| 19 |
max_tokens,
|
| 20 |
temperature,
|
| 21 |
top_p,
|
| 22 |
):
|
| 23 |
+
# Construct the system message
|
| 24 |
enhanced_system_message = (
|
| 25 |
f"{system_message}\n\n"
|
| 26 |
+
f"Role, Industry and Type of Organization: {role}\n"
|
| 27 |
+
f"Job Ad Responsibilities and Key Requirements: {ad}\n"
|
| 28 |
+
f"Education, Training and Certifications: {education}\n"
|
| 29 |
+
f"Work Experience: {experience}\n"
|
| 30 |
+
f"Skills: {skills}\n"
|
| 31 |
)
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
if ask_for_skills_suggestions:
|
| 34 |
+
enhanced_system_message += " The user is also asking for suggestions of skills related to this role."
|
| 35 |
+
|
| 36 |
+
# Compose the conversation
|
| 37 |
messages = [{"role": "system", "content": enhanced_system_message}]
|
| 38 |
+
for user_msg, assistant_msg in history:
|
| 39 |
+
if user_msg:
|
| 40 |
+
messages.append({"role": "user", "content": user_msg})
|
| 41 |
+
if assistant_msg:
|
| 42 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 43 |
messages.append({"role": "user", "content": message})
|
| 44 |
|
| 45 |
+
# Generate and stream response
|
| 46 |
response_text = ""
|
| 47 |
try:
|
| 48 |
response = client.chat.completions.create(
|
| 49 |
+
model="gpt-4o", # use "gpt-4o-mini" when officially available
|
| 50 |
messages=messages,
|
| 51 |
max_tokens=max_tokens,
|
| 52 |
temperature=temperature,
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
yield f"❌ An error occurred: {str(e)}"
|
| 63 |
|
| 64 |
+
# Gradio interface
|
| 65 |
demo = gr.ChatInterface(
|
| 66 |
fn=respond,
|
| 67 |
additional_inputs=[
|
| 68 |
gr.Textbox(
|
| 69 |
+
value="You are a friendly Chatbot, a career coach and with experience as a recruiter. You are trying to help a user practice STAR-style interview questions customized for a specific role, employer organization and job Ad - based on user input. Include tips if some items are missing.",
|
| 70 |
label="Instructions to Bot",
|
| 71 |
),
|
| 72 |
+
gr.Textbox(label="Role, Industry and Employer", placeholder="Describe the role, industry and employer you are applying to."),
|
| 73 |
+
gr.Textbox(
|
| 74 |
+
label="Job Ad Responsibilities",
|
| 75 |
+
placeholder="Describe the Responsibilities and Key Requirements advertised in the job ad",
|
| 76 |
+
),
|
| 77 |
gr.Textbox(
|
| 78 |
+
label=" Job Key Requirements ",
|
| 79 |
+
placeholder="Describe your education, training, certifications and professional designations",
|
| 80 |
),
|
| 81 |
gr.Textbox(
|
| 82 |
+
label="A Summary about your Education and Work Experience",
|
| 83 |
+
placeholder="Describe your education, certifications, skills, work experience, previous responsibilities and key career achievements",
|
| 84 |
),
|
| 85 |
+
gr.Textbox(label="Skills", placeholder="List your key skills that match this job or ask for suggestions"),
|
|
|
|
| 86 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 87 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 88 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 89 |
],
|
| 90 |
+
title="STAR-Style Interview Questions!",
|
| 91 |
+
description="This app generates STAR-Style Job Interview Questions customized to the specific role, industry and employer of the job you are applying to. Based on your input. Powered by OpenAI GPT-4o, design thinking, and domain expertise. Developed by wn. Disclaimer: AI can make mistakes. Use with caution and at your own risk!",
|
| 92 |
+
type="messages", # Required for Gradio ChatInterface streaming
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
demo.launch()
|
| 97 |
+
|