Stone164 commited on
Commit
e1b3ed6
·
verified ·
1 Parent(s): d50a675

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +175 -30
app.py CHANGED
@@ -3,13 +3,91 @@ from openai import OpenAI
3
  import os
4
  import time
5
 
6
- def predict(message, history, system_prompt, model, max_tokens, temperature, top_p):
 
 
 
 
 
 
 
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Initialize the OpenAI client
9
  client = OpenAI(
10
  api_key=os.environ.get("API_TOKEN"),
11
  )
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Start with the system prompt
14
  messages = [{"role": "system", "content": system_prompt}]
15
 
@@ -19,56 +97,123 @@ def predict(message, history, system_prompt, model, max_tokens, temperature, top
19
  # Add the current user message
20
  messages.append({"role": "user", "content": message})
21
 
22
- # Record the start time
23
- start_time = time.time()
24
-
25
- # Streaming response
26
  response = client.chat.completions.create(
27
  model=model,
28
  messages=messages,
29
  max_tokens=max_tokens,
30
  temperature=temperature,
31
  top_p=top_p,
32
- stop=None,
33
  stream=True
34
  )
35
 
36
  full_message = ""
37
- first_chunk_time = None
38
  last_yield_time = None
39
 
40
  for chunk in response:
41
  if chunk.choices and chunk.choices[0].delta.content:
42
- if first_chunk_time is None:
43
- first_chunk_time = time.time() - start_time # Record time for the first chunk
44
-
45
  full_message += chunk.choices[0].delta.content
46
  current_time = time.time()
47
- chunk_time = current_time - start_time # calculate the time delay of the chunk
48
- print(f"Message received {chunk_time:.2f} seconds after request: {chunk.choices[0].delta.content}")
49
-
50
  if last_yield_time is None or (current_time - last_yield_time >= 0.25):
51
  yield full_message
52
  last_yield_time = current_time
53
 
54
  # Ensure to yield any remaining message that didn't meet the time threshold
55
  if full_message:
56
- total_time = time.time() - start_time
57
- # Append timing information to the response message
58
- full_message += f" (First Chunk: {first_chunk_time:.2f}s, Total: {total_time:.2f}s)"
59
  yield full_message
60
 
61
- gr.ChatInterface(
62
- fn=predict,
63
- type="messages",
64
- #save_history=True,
65
- #editable=True,
66
- additional_inputs=[
67
- gr.Textbox("You are a helpful AI assistant.", label="System Prompt"),
68
- gr.Dropdown(["gpt-4o", "gpt-4o-mini"], label="Model"),
69
- gr.Slider(800, 4000, value=2000, label="Max Token"),
70
- gr.Slider(0, 1, value=0.7, label="Temperature"),
71
- gr.Slider(0, 1, value=0.95, label="Top P"),
72
- ],
73
- css="footer{display:none !important}"
74
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import os
4
  import time
5
 
6
+ # Store user responses
7
+ user_profile = {
8
+ "mode": None,
9
+ "age": None,
10
+ "degree": None,
11
+ "interests": None,
12
+ "mbti": None
13
+ }
14
 
15
+ questions = [
16
+ ("mode", "Do you already have a target career, or are you still exploring? (Reply with 'Forward' or 'Backward')"),
17
+ ("age", "What's your age range? (e.g., 'under 18', '18-21', '21-25', '25-29')"),
18
+ ("degree", "What is your current degree? (e.g., Bachelor's, Master's, PhD)"),
19
+ ("interests", "List a few of your interests, separated by commas. (e.g., Programming, Psychology, Design)"),
20
+ ("mbti", "What's your MBTI personality type? (e.g., INFP, INTJ, ENFP, etc.)")
21
+ ]
22
+
23
+ current_q_index = 0
24
+
25
+ # Defaults
26
+ system_prompt_default = "You are a helpful and knowledgeable AI career assistant."
27
+ model_default = "gpt-4o"
28
+ temp_default = 0.7
29
+ top_p_default = 0.95
30
+ token_default = 2000
31
+
32
+ # Placeholder to store setting values
33
+ system_prompt_box = gr.Textbox(system_prompt_default)
34
+ model_dropdown = gr.Dropdown(["gpt-4o", "gpt-4o-mini"], value=model_default)
35
+ token_slider = gr.Slider(800, 4000, value=token_default)
36
+ temp_slider = gr.Slider(0, 1, value=temp_default)
37
+ top_p_slider = gr.Slider(0, 1, value=top_p_default)
38
+
39
+ def predict(message, history):
40
+ global current_q_index
41
+
42
+ # Get values from global widgets
43
+ system_prompt = system_prompt_box.value
44
+ model = model_dropdown.value
45
+ max_tokens = token_slider.value
46
+ temperature = temp_slider.value
47
+ top_p = top_p_slider.value
48
+
49
  # Initialize the OpenAI client
50
  client = OpenAI(
51
  api_key=os.environ.get("API_TOKEN"),
52
  )
53
 
54
+ if current_q_index > 0 and current_q_index <= len(questions):
55
+ key = questions[current_q_index - 1][0]
56
+ user_profile[key] = message.strip()
57
+
58
+ if current_q_index < len(questions):
59
+ question = questions[current_q_index][1]
60
+ current_q_index += 1
61
+ return question
62
+
63
+ # Profile-based response
64
+ mode = user_profile["mode"] or "Forward"
65
+ age = user_profile["age"]
66
+ degree = user_profile["degree"]
67
+ interests = user_profile["interests"]
68
+ mbti = user_profile["mbti"]
69
+
70
+ if "Forward" in mode:
71
+ system_prompt = system_prompt or f"""
72
+ You are a career planning AI assistant. The student wants to explore suitable career options.
73
+ Student profile:
74
+ - Age: {age}
75
+ - Degree: {degree}
76
+ - Interests: {interests}
77
+ - MBTI Type: {mbti}
78
+ Please recommend several career paths based on this background, and describe entry requirements and preparation steps (courses, certifications, skills, etc.).
79
+ """
80
+ else:
81
+ system_prompt = system_prompt or f"""
82
+ You are a career planning AI assistant. The student already has a target career.
83
+ Student profile:
84
+ - Age: {age}
85
+ - Degree: {degree}
86
+ - Interests: {interests}
87
+ - MBTI Type: {mbti}
88
+ Please reverse-design the career path based on this background, including required courses, skill preparation, school resources, and internship advice.
89
+ """
90
+
91
  # Start with the system prompt
92
  messages = [{"role": "system", "content": system_prompt}]
93
 
 
97
  # Add the current user message
98
  messages.append({"role": "user", "content": message})
99
 
100
+ # Create streaming response
 
 
 
101
  response = client.chat.completions.create(
102
  model=model,
103
  messages=messages,
104
  max_tokens=max_tokens,
105
  temperature=temperature,
106
  top_p=top_p,
 
107
  stream=True
108
  )
109
 
110
  full_message = ""
 
111
  last_yield_time = None
112
 
113
  for chunk in response:
114
  if chunk.choices and chunk.choices[0].delta.content:
 
 
 
115
  full_message += chunk.choices[0].delta.content
116
  current_time = time.time()
117
+
 
 
118
  if last_yield_time is None or (current_time - last_yield_time >= 0.25):
119
  yield full_message
120
  last_yield_time = current_time
121
 
122
  # Ensure to yield any remaining message that didn't meet the time threshold
123
  if full_message:
 
 
 
124
  yield full_message
125
 
126
+
127
+ # Create the interface with custom styling
128
+ with gr.Blocks(css="""
129
+ body {
130
+ background-color: #1e1e1e;
131
+ color: #ffffff;
132
+ }
133
+
134
+ .gradio-container {
135
+ font-family: 'Segoe UI', sans-serif;
136
+ }
137
+
138
+ .gr-chatbot {
139
+ background-color: transparent !important;
140
+ }
141
+
142
+ .message.user {
143
+ background-color: #cce6ff !important; /* Much lighter blue */
144
+ color: #000000 !important;
145
+ border-radius: 10px !important;
146
+ padding: 10px;
147
+ margin: 6px;
148
+ }
149
+
150
+ .message.bot {
151
+ background-color: #99ccff !important; /* Lighter blue */
152
+ color: #000000 !important;
153
+ border-radius: 10px !important;
154
+ padding: 10px;
155
+ margin: 6px;
156
+ }
157
+
158
+ .gr-button {
159
+ border-radius: 8px;
160
+ }
161
+
162
+ #custom-send {
163
+ background-color: #ec4899 !important;
164
+ color: white !important;
165
+ border-radius: 999px !important;
166
+ padding: 10px 24px !important;
167
+ font-weight: bold;
168
+ box-shadow: 0 0 10px #ec4899;
169
+ transition: all 0.3s ease-in-out;
170
+ }
171
+
172
+ #custom-send:hover {
173
+ background-color: #d63384 !important;
174
+ box-shadow: 0 0 12px #ec4899;
175
+ }
176
+
177
+ textarea, input {
178
+ background-color: #ffe4f1 !important;
179
+ color: #5e2c49 !important;
180
+ border: 1px solid #ec4899 !important;
181
+ }
182
+
183
+ footer {
184
+ display: none !important;
185
+ }
186
+ """) as demo:
187
+
188
+ with gr.Row():
189
+ gr.HTML("""
190
+ <div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
191
+ <!-- 左侧动图 -->
192
+ <img src='https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmFucGwxbmNsd3J5NXV0Y282NXNtMzNsZW5jMm4wNWh6c2dqbXIwdiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l41m18LjqpzxUr2WA/giphy.gif' width='200' style='border-radius: 12px; box-shadow: 0 0 10px #ec4899;'>
193
+
194
+ <!-- 右侧标题 -->
195
+ <div style='text-align: left;'>
196
+ <h1 style='color:white; font-size: 36px; margin-bottom: 6px;'>🎓 AI Career Exploration Assistant</h1>
197
+ <p style='font-size: 18px; font-weight:bold; color:#ec4899; margin-top: 0;margin-left: 150px'>Hi! Lets Make the Dream Comes True 💖</p>
198
+ </div>
199
+ </div>
200
+ """)
201
+
202
+ gr.Markdown("**Let's chat! 💬** Ask me anything you want")
203
+
204
+ gr.ChatInterface(
205
+ fn=predict,
206
+ chatbot=gr.Chatbot(height=500),
207
+ textbox=gr.Textbox(placeholder="Type your reply here..."),
208
+ submit_btn=gr.Button("🚀 Send", elem_id="custom-send")
209
+ )
210
+
211
+ with gr.Accordion("⚙️ Advanced Settings (Click to Show/Hide)", open=False):
212
+ gr.Markdown("#### Prompt & Model Settings")
213
+ system_prompt_box.render()
214
+ model_dropdown.render()
215
+ token_slider.render()
216
+ temp_slider.render()
217
+ top_p_slider.render()
218
+
219
+ demo.launch()