Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
|
4 |
+
|
5 |
+
def get_ans(user_content, chat_data_, chat_log_, api_key):
|
6 |
+
chat_data_.append({"role": "user", "content": user_content})
|
7 |
+
res_ = request_ans(api_key, chat_data_)
|
8 |
+
res = res_.choices[0].message.content
|
9 |
+
while res.startswith("\n") != res.startswith("?"):
|
10 |
+
res = res[1:]
|
11 |
+
chat_data_.append({"role": 'assistant', "content": res})
|
12 |
+
|
13 |
+
chat_log_.append([user_content, res])
|
14 |
+
|
15 |
+
return chat_log_, chat_log_, "", chat_data_
|
16 |
+
|
17 |
+
|
18 |
+
def request_ans(api_key, msg):
|
19 |
+
# openai.proxy = {'http': "http://127.0.0.1:8001", 'https': 'http://127.0.0.1:8001'}
|
20 |
+
openai.api_key = api_key
|
21 |
+
|
22 |
+
response = openai.ChatCompletion.create(
|
23 |
+
model='gpt-3.5-turbo',
|
24 |
+
messages=msg,
|
25 |
+
temperature=0.2
|
26 |
+
)
|
27 |
+
|
28 |
+
return response
|
29 |
+
|
30 |
+
|
31 |
+
def config(api_key, company, job):
|
32 |
+
prompt = f"你是一名熟悉{company}的面试官。用户将成为候选人,您将向用户询问{job}职位的面试问题。希望你只作为面试官回答。不要一次写出所有的问题。希望你只对用户进行面试。问用户问题,等待用户的回答。不要写解释。你需要像面试官一样一个一个问题问用户,等用户回答。 若用户回答不上某个问题,那就继续问下一个问题 "
|
33 |
+
content = [{"role": "system", "content": prompt}, {"role": "user", "content": "面试官你好"}]
|
34 |
+
|
35 |
+
res_ = request_ans(api_key, content)
|
36 |
+
res = res_.choices[0].message.content
|
37 |
+
while res.startswith("\n") != res.startswith("?"):
|
38 |
+
res = res[1:]
|
39 |
+
|
40 |
+
history = [["面试官您好!", res]]
|
41 |
+
|
42 |
+
return history, history, api_key, content
|
43 |
+
|
44 |
+
|
45 |
+
with gr.Blocks(title="AI面试官") as block:
|
46 |
+
chat_log = gr.State()
|
47 |
+
key = gr.State()
|
48 |
+
chat_data = gr.State()
|
49 |
+
|
50 |
+
gr.Markdown("""<h1><center>AI面试官</center></h1>""")
|
51 |
+
with gr.Row():
|
52 |
+
with gr.Column():
|
53 |
+
chatbot = gr.Chatbot()
|
54 |
+
message = gr.Textbox(label="你的回答")
|
55 |
+
|
56 |
+
message.submit(
|
57 |
+
fn=get_ans,
|
58 |
+
inputs=[
|
59 |
+
message,
|
60 |
+
chat_data,
|
61 |
+
chat_log,
|
62 |
+
key
|
63 |
+
],
|
64 |
+
outputs=[chatbot, chat_log, message, chat_data]
|
65 |
+
)
|
66 |
+
|
67 |
+
submit = gr.Button("发送")
|
68 |
+
submit.click(
|
69 |
+
get_ans,
|
70 |
+
inputs=[
|
71 |
+
message,
|
72 |
+
chat_data,
|
73 |
+
chat_log,
|
74 |
+
key
|
75 |
+
],
|
76 |
+
outputs=[chatbot, chat_log, message, chat_data],
|
77 |
+
)
|
78 |
+
|
79 |
+
with gr.Column():
|
80 |
+
# temperature = gr.Slider(label="Temperature", minimum=0, maximum=1, step=0.1, value=0.9)
|
81 |
+
# max_tokens = gr.Slider(label="Max Tokens", minimum=10, maximum=400, step=10, value=150)
|
82 |
+
# top_p = gr.Slider(label="Top P", minimum=0, maximum=1, step=0.1, value=1)
|
83 |
+
# frequency_penalty = gr.Slider(
|
84 |
+
# label="Frequency Penalty",
|
85 |
+
# minimum=0,
|
86 |
+
# maximum=1,
|
87 |
+
# step=0.1,
|
88 |
+
# value=0,
|
89 |
+
# )
|
90 |
+
# presence_penalty = gr.Slider(
|
91 |
+
# label="Presence Penalty",
|
92 |
+
# minimum=0,
|
93 |
+
# maximum=1,
|
94 |
+
# step=0.1,
|
95 |
+
# value=0.6,
|
96 |
+
# )
|
97 |
+
|
98 |
+
openai_token = gr.Textbox(label="OpenAI API Key") # , value=os.getenv("OPENAI_API_KEY")
|
99 |
+
company = gr.Textbox(label="你面试的公司")
|
100 |
+
job = gr.Textbox(label="你面试的岗位")
|
101 |
+
|
102 |
+
start = gr.Button("开始面试")
|
103 |
+
start.click(
|
104 |
+
config,
|
105 |
+
inputs=[
|
106 |
+
openai_token,
|
107 |
+
company,
|
108 |
+
job
|
109 |
+
],
|
110 |
+
outputs=[chatbot, chat_log, key, chat_data]
|
111 |
+
)
|
112 |
+
|
113 |
+
if __name__ == "__main__":
|
114 |
+
block.launch(auth=('alpaca', 'alpaca'), auth_message="Enter your username and password")
|