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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -197
app.py CHANGED
@@ -1,219 +1,61 @@
1
  import gradio as gr
2
  from openai import OpenAI
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
-
94
- # Add the conversation history
95
- messages.extend(history if history else [])
96
-
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()
 
1
  import gradio as gr
2
  from openai import OpenAI
3
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # 简化版本的预测函数
6
  def predict(message, history):
7
+ try:
8
+ # 初始化OpenAI客户端
9
+ api_key = os.environ.get("API_TOKEN")
10
+ if not api_key:
11
+ return "错误:API密钥未设置,请在Hugging Face Space设置中添加名为API_TOKEN的Secret。"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ client = OpenAI(api_key=api_key)
14
+
15
+ # 构建消息
16
+ messages = [
17
+ {"role": "system", "content": "你是一个有用的AI助手。"},
18
+ {"role": "user", "content": message}
19
+ ]
20
+
21
+ # 非流式调用,更简单更可靠
22
+ response = client.chat.completions.create(
23
+ model="gpt-4o",
24
+ messages=messages,
25
+ max_tokens=1000,
26
+ temperature=0.7,
27
+ stream=False # 关闭流式响应
28
+ )
29
+
30
+ return response.choices[0].message.content
31
+ except Exception as e:
32
+ return f"发生错误: {str(e)}"
33
 
34
+ # 创建简化版界面
35
  with gr.Blocks(css="""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  #custom-send {
37
  background-color: #ec4899 !important;
38
  color: white !important;
39
  border-radius: 999px !important;
40
  padding: 10px 24px !important;
41
  font-weight: bold;
 
 
42
  }
43
+
 
 
 
 
 
 
 
 
 
 
 
44
  footer {
45
  display: none !important;
46
  }
47
  """) as demo:
48
+ gr.HTML("""
49
+ <div style='text-align: center; margin-bottom: 10px;'>
50
+ <h1 style='color:white; font-size: 28px; margin-bottom: 6px;'>🎓 AI Career Assistant</h1>
51
+ </div>
52
+ """)
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ chatbot = gr.Chatbot()
55
+ msg = gr.Textbox(placeholder="输入你的问题...", show_label=False)
56
+ send_btn = gr.Button("发送", elem_id="custom-send")
57
+
58
+ send_btn.click(predict, [msg, chatbot], [chatbot])
59
+ msg.submit(predict, [msg, chatbot], [chatbot])
 
 
 
 
 
 
 
 
60
 
61
  demo.launch()