lhnjames commited on
Commit
e2bf37e
·
verified ·
1 Parent(s): 4ab3401

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -121
app.py CHANGED
@@ -4,17 +4,23 @@ import gradio as gr
4
  openai.api_key = "sk-proj-J5Kw82RihIm1kxpuMzR7T3BlbkFJ07byobHg6K2A2VQMeUGd"
5
 
6
  def generate_text(messages, max_tokens, temperature, top_p):
 
7
  try:
8
- response = openai.ChatCompletion.create(
 
9
  model="gpt-3.5-turbo",
10
  messages=messages,
11
  max_tokens=max_tokens,
12
  temperature=temperature,
13
- top_p=top_p
 
14
  )
15
- return response.choices[0].message['content']
 
 
16
  except Exception as e:
17
- return f"Error: {str(e)}"
 
18
 
19
  def respond(message, system_message, max_tokens, temperature, top_p, history):
20
  messages = [{"role": "system", "content": system_message}] + \
@@ -30,7 +36,7 @@ def respond(message, system_message, max_tokens, temperature, top_p, history):
30
 
31
  return response1, response2, history
32
 
33
- def vote(choice, history):
34
  response1 = next((r for u, r in history if u == "assistant_1"), "No response")
35
  response2 = next((r for u, r in history if u == "assistant_2"), "No response")
36
  selected_response = response1 if choice == "回答 1" else response2
@@ -40,127 +46,31 @@ def vote(choice, history):
40
  with gr.Blocks() as demo:
41
  gr.Markdown("# 回答选择系统")
42
 
43
- with gr.Column():
44
- system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
45
- max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
46
- temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
47
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
48
 
49
- message = gr.Textbox(label="输入你的问题")
50
- submit_button = gr.Button("发送")
51
 
52
- chat_history = gr.Chatbot(label="聊天历史")
53
- history = gr.State([]) # 用于存储对话历史
54
 
55
- answer1_btn = gr.Button("回答 1", visible=False)
56
- answer2_btn = gr.Button("回答 2", visible=False)
 
 
 
 
57
 
58
- def update_answers(message, system_message, max_tokens, temperature, top_p, history):
59
- response1, response2, updated_history = respond(message, system_message, max_tokens, temperature, top_p, history)
60
- answer1_btn.update(value=response1, visible=True)
61
- answer2_btn.update(value=response2, visible=True)
62
- return updated_history
63
 
64
- submit_button.click(
65
- fn=update_answers,
66
- inputs=[message, system_message, max_tokens, temperature, top_p, history],
67
- outputs=[chat_history, answer1_btn, answer2_btn]
68
- )
69
-
70
- def handle_vote(choice, history):
71
- return vote(choice, history)
72
-
73
- choice = gr.Radio(["回答 1", "回答 2"], label="选择更喜欢的回答")
74
- vote_button = gr.Button("提交选择")
75
-
76
- vote_button.click(
77
- fn=handle_vote,
78
- inputs=[choice, history],
79
- outputs=[chat_history, gr.Textbox(label="选择结果")]
80
- )
81
 
82
  if __name__ == "__main__":
83
  demo.launch()
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
- # import openai
92
- # import gradio as gr
93
-
94
- # openai.api_key = "sk-proj-J5Kw82RihIm1kxpuMzR7T3BlbkFJ07byobHg6K2A2VQMeUGd"
95
-
96
- # def generate_text(messages, max_tokens, temperature, top_p):
97
- # response_text = ""
98
- # try:
99
- # # Create a streaming response from OpenAI
100
- # stream = openai.ChatCompletion.create(
101
- # model="gpt-3.5-turbo",
102
- # messages=messages,
103
- # max_tokens=max_tokens,
104
- # temperature=temperature,
105
- # top_p=top_p,
106
- # stream=True
107
- # )
108
- # for chunk in stream:
109
- # if 'content' in chunk.choices[0].delta:
110
- # response_text += chunk.choices[0].delta['content']
111
- # except Exception as e:
112
- # response_text = f"Error: {str(e)}"
113
- # return response_text
114
-
115
- # def respond(message, system_message, max_tokens, temperature, top_p, history):
116
- # messages = [{"role": "system", "content": system_message}] + \
117
- # [{"role": "user", "content": msg[0]} if i % 2 == 0 else {"role": "assistant", "content": msg[1]} for i, msg in enumerate(history)] + \
118
- # [{"role": "user", "content": message}]
119
-
120
- # response1 = generate_text(messages, max_tokens, temperature, top_p)
121
- # response2 = generate_text(messages, max_tokens, temperature, top_p)
122
-
123
- # history.append(("user", message))
124
- # history.append(("assistant_1", response1))
125
- # history.append(("assistant_2", response2))
126
-
127
- # return response1, response2, history
128
-
129
- # def vote(history, choice):
130
- # response1 = next((r for u, r in history if u == "assistant_1"), "No response")
131
- # response2 = next((r for u, r in history if u == "assistant_2"), "No response")
132
- # selected_response = response1 if choice == "回答 1" else response2
133
- # history.append(("user_selected", selected_response))
134
- # return history, f"你选择了:{choice}"
135
-
136
- # with gr.Blocks() as demo:
137
- # gr.Markdown("# 回答选择系统")
138
-
139
- # system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
140
- # max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
141
- # temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
142
- # top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
143
-
144
- # chat_history = gr.Chatbot(label="聊天历史")
145
- # message = gr.Textbox(label="输入你的问题")
146
-
147
- # history = gr.State([]) # 用于存储对话历史
148
-
149
- # submit_button = gr.Button("发送")
150
- # submit_button.click(
151
- # fn=respond,
152
- # inputs=[message, system_message, max_tokens, temperature, top_p, history],
153
- # outputs=[gr.Textbox(label="回答 1"), gr.Textbox(label="回答 2"), history]
154
- # )
155
-
156
- # choice = gr.Radio(["回答 1", "回答 2"], label="选择更喜欢的回答")
157
- # vote_button = gr.Button("提交选择")
158
-
159
- # vote_button.click(
160
- # fn=vote,
161
- # inputs=[history, choice],
162
- # outputs=[chat_history, gr.Textbox(label="选择结果")]
163
- # )
164
-
165
- # if __name__ == "__main__":
166
- # demo.launch()
 
4
  openai.api_key = "sk-proj-J5Kw82RihIm1kxpuMzR7T3BlbkFJ07byobHg6K2A2VQMeUGd"
5
 
6
  def generate_text(messages, max_tokens, temperature, top_p):
7
+ response_text = ""
8
  try:
9
+ # Create a streaming response from OpenAI
10
+ stream = openai.ChatCompletion.create(
11
  model="gpt-3.5-turbo",
12
  messages=messages,
13
  max_tokens=max_tokens,
14
  temperature=temperature,
15
+ top_p=top_p,
16
+ stream=True
17
  )
18
+ for chunk in stream:
19
+ if 'content' in chunk.choices[0].delta:
20
+ response_text += chunk.choices[0].delta['content']
21
  except Exception as e:
22
+ response_text = f"Error: {str(e)}"
23
+ return response_text
24
 
25
  def respond(message, system_message, max_tokens, temperature, top_p, history):
26
  messages = [{"role": "system", "content": system_message}] + \
 
36
 
37
  return response1, response2, history
38
 
39
+ def vote(history, choice):
40
  response1 = next((r for u, r in history if u == "assistant_1"), "No response")
41
  response2 = next((r for u, r in history if u == "assistant_2"), "No response")
42
  selected_response = response1 if choice == "回答 1" else response2
 
46
  with gr.Blocks() as demo:
47
  gr.Markdown("# 回答选择系统")
48
 
49
+ system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
50
+ max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
51
+ temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
52
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
 
53
 
54
+ chat_history = gr.Chatbot(label="聊天历史")
55
+ message = gr.Textbox(label="输入你的问题")
56
 
57
+ history = gr.State([]) # 用于存储对话历史
 
58
 
59
+ submit_button = gr.Button("发送")
60
+ submit_button.click(
61
+ fn=respond,
62
+ inputs=[message, system_message, max_tokens, temperature, top_p, history],
63
+ outputs=[gr.Textbox(label="回答 1"), gr.Textbox(label="回答 2"), history]
64
+ )
65
 
66
+ choice = gr.Radio(["回答 1", "回答 2"], label="选择更喜欢的回答")
67
+ vote_button = gr.Button("提交选择")
 
 
 
68
 
69
+ vote_button.click(
70
+ fn=vote,
71
+ inputs=[history, choice],
72
+ outputs=[chat_history, gr.Textbox(label="选择结果")]
73
+ )
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  if __name__ == "__main__":
76
  demo.launch()