willn9 commited on
Commit
2a6f904
·
verified ·
1 Parent(s): 569af97

Create app_openai.py

Browse files
Files changed (1) hide show
  1. app_openai.py +97 -0
app_openai.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+
5
+ # Initialize OpenAI client
6
+ client = OpenAI(api_key=os.getenv("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,
53
+ top_p=top_p,
54
+ stream=True,
55
+ )
56
+ for chunk in response:
57
+ if chunk.choices and chunk.choices[0].delta.content:
58
+ token = chunk.choices[0].delta.content
59
+ response_text += token
60
+ yield response_text
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 a talented copywriter. You are trying to help a user customize their resume according to 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 and Key Requirements",
75
+ placeholder="Describe the Responsibilities and Key Requirements advertised in the job ad",
76
+ ),
77
+ gr.Textbox(
78
+ label="Your Education, certifications, training, etc.",
79
+ placeholder="Describe your education, training, certifications and professional designations",
80
+ ),
81
+ gr.Textbox(
82
+ label="Your Work Experience",
83
+ placeholder="Describe your 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.Checkbox(label="Ask for Skills Suggestions", value=False),
87
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
88
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
89
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
90
+ ],
91
+ title="Resumize – Customize your CV!",
92
+ description="This app customizes your resume to best suit a specific role, industry, employer and job ad. 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!",
93
+ type="messages", # Required for Gradio ChatInterface streaming
94
+ )
95
+
96
+ if __name__ == "__main__":
97
+ demo.launch()