ericwangpq commited on
Commit
c78d3b2
·
1 Parent(s): 3b365b4

4-19 1146

Browse files
Files changed (1) hide show
  1. app.py +41 -12
app.py CHANGED
@@ -1,28 +1,57 @@
 
1
  from openai import OpenAI
2
- import gradio as gr
3
  import os
4
 
5
  API_KEY = os.getenv("PPLX_API_KEY")
6
  client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
7
 
8
-
9
  def predict(message, history):
10
  history_openai_format = []
11
  for human, assistant in history:
12
- history_openai_format.append({"role": "user", "content": human })
13
- history_openai_format.append({"role": "assistant", "content":assistant})
14
  history_openai_format.append({"role": "user", "content": message})
15
 
16
- # response = client.chat.completions.create(model='mistral-7b-instruct',
17
- response = client.chat.completions.create(model='pplx-70b-online',
18
- messages= history_openai_format,
19
- temperature=1.0,
20
- stream=True)
 
 
21
 
22
  partial_message = ""
23
  for chunk in response:
24
  if chunk.choices[0].delta.content is not None:
25
- partial_message = partial_message + chunk.choices[0].delta.content
26
- yield partial_message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- gr.ChatInterface(predict).launch(share=True)
 
1
+
2
  from openai import OpenAI
3
+ import gradio as gr
4
  import os
5
 
6
  API_KEY = os.getenv("PPLX_API_KEY")
7
  client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
8
 
 
9
  def predict(message, history):
10
  history_openai_format = []
11
  for human, assistant in history:
12
+ history_openai_format.append({"role": "user", "content": human})
13
+ history_openai_format.append({"role": "assistant", "content": assistant})
14
  history_openai_format.append({"role": "user", "content": message})
15
 
16
+ # 使用 Perplexity AI 的 70B 模型进行响应
17
+ response = client.chat.completions.create(
18
+ model='pplx-70b-online',
19
+ messages=history_openai_format,
20
+ temperature=1.0,
21
+ stream=True
22
+ )
23
 
24
  partial_message = ""
25
  for chunk in response:
26
  if chunk.choices[0].delta.content is not None:
27
+ partial_message += chunk.choices[0].delta.content
28
+ yield partial_message
29
+
30
+ def update_text(show_text1, show_text2):
31
+ text1 = "text 1" if show_text1 else ""
32
+ text2 = "text 2" if show_text2 else ""
33
+ return f"{text1}\n{text2}"
34
+
35
+ def change_textbox(choice):
36
+ #根据不同输入对输出控件进行更新
37
+ if choice == "short":
38
+ return gr.update(lines=2, visible=True, value="Short story: ")
39
+ elif choice == "long":
40
+ return gr.update(lines=8, visible=True, value="Long story...")
41
+ else:
42
+ return gr.update(visible=False)
43
+
44
+ with gr.Blocks(fill_height=True) as demo:
45
+ radio = gr.Radio(
46
+ ["short", "long", "none"], label="Essay Length to Write?"
47
+ )
48
+ text = gr.Textbox(lines=2, interactive=True)
49
+ radio.change(fn=change_textbox, inputs=radio, outputs=text)
50
+ chat_interface = gr.ChatInterface(predict)
51
+
52
+
53
+ # chat_interface = gr.ChatInterface(predict)
54
+ # 启动应用
55
+ demo.launch(share=True)
56
+
57