zwq2018 commited on
Commit
997c9e0
1 Parent(s): f59b8aa

modified: app.py

Browse files

new file: app_with_key.py

Files changed (2) hide show
  1. app.py +25 -71
  2. app_with_key.py +220 -0
app.py CHANGED
@@ -9,7 +9,6 @@ import sys
9
  import time
10
  import os
11
  import pandas as pd
12
- OPENAI_KEY = None
13
  css = """#col-container {max-width: 90%; margin-left: auto; margin-right: auto; display: flex; flex-direction: column;}
14
  #header {text-align: center;}
15
  #col-chatbox {flex: 1; max-height: min(750px, 100%);}
@@ -23,8 +22,8 @@ css = """#col-container {max-width: 90%; margin-left: auto; margin-right: auto;
23
  # plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
24
  # plt.rcParams['axes.unicode_minus'] = False
25
 
26
- plt.rcParams['font.sans-serif'] = ['WenQuanYi Zen Hei', 'Noto Sans CJK']
27
- plt.rcParams['axes.unicode_minus'] = False
28
 
29
 
30
  example_stock =['给我画一下可孚医疗2022年年中到今天的股价','北向资金今年的每日流入和累计流入','看一下近三年宁德时代和贵州茅台的pb变化','画一下五粮液和泸州老窖从2019年年初到2022年年中的收益率走势','成都银行近一年的k线图和kdj指标','比较下沪深300,创业板指,中证1000指数今年的收益率','今年上证50所有成分股的收益率是多少']
@@ -40,28 +39,28 @@ class Client:
40
  self.OPENAI_API_KEY_AZURE = None
41
  self.stop = False # 添加停止标志
42
 
43
- def set_key(self, openai_key, openai_key_azure, api_base_azure, engine_azure):
44
- self.OPENAI_KEY = openai_key
45
- self.OPENAI_API_BASED_AZURE = api_base_azure
46
- self.OPENAI_API_KEY_AZURE = openai_key_azure
47
- self.OPENAI_ENGINE_AZURE = engine_azure
48
- return self.OPENAI_KEY, self.OPENAI_API_KEY_AZURE, self.OPENAI_API_BASED_AZURE, self.OPENAI_ENGINE_AZURE
49
 
50
 
51
  def run(self, messages):
52
- if self.OPENAI_KEY == '' and self.OPENAI_API_KEY_AZURE == '':
53
- yield '', np.zeros((100, 100, 3), dtype=np.uint8), "Please set your OpenAI API key first!!!", pd.DataFrame()
54
- elif len(self.OPENAI_KEY) >= 0 and not self.OPENAI_KEY.startswith('sk') and self.OPENAI_API_KEY_AZURE == '':
55
- yield '', np.zeros((100, 100, 3), dtype=np.uint8), "Your openai key is incorrect!!!", pd.DataFrame()
56
- else:
57
  # self.stop = False
58
- gen = gradio_interface(messages, self.OPENAI_KEY, self.OPENAI_API_KEY_AZURE, self.OPENAI_API_BASED_AZURE, self.OPENAI_ENGINE_AZURE)
59
- while not self.stop: #
60
- try:
61
- yield next(gen)
62
- except StopIteration:
63
- print("StopIteration")
64
- break
 
 
 
 
65
 
66
  # yield from gradio_interface(messages, self.OPENAI_KEY)
67
  #return finally_text, img, output, df
@@ -81,55 +80,14 @@ with gr.Blocks() as demo:
81
  gr.Markdown(
82
  """
83
  # Hello Data-Copilot ! 😀
 
84
  A powerful AI system connects humans and data.
 
85
  ## The current version only supports **Chinese financial data**, in the future we will support for other country data
 
86
  """)
87
 
88
 
89
- if not OPENAI_KEY:
90
- with gr.Row().style():
91
- with gr.Column(scale=0.9):
92
- gr.Markdown(
93
- """
94
- You can use gpt35 from openai or from openai-azure ,then press OK.
95
- """)
96
- openai_api_key = gr.Textbox(
97
- show_label=False,
98
- placeholder="Set your OpenAI API key here and press Submit (e.g. sk-xxx)",
99
- lines=1,
100
- type="password"
101
- ).style(container=False)
102
-
103
- with gr.Row():
104
- openai_api_key_azure = gr.Textbox(
105
- show_label=False,
106
- placeholder="Set your Azure-OpenAI key",
107
- lines=1,
108
- type="password"
109
- ).style(container=False)
110
- openai_api_base_azure = gr.Textbox(
111
- show_label=False,
112
- placeholder="Azure-OpenAI api_base (e.g. https://zwq0525.openai.azure.com)",
113
- lines=1,
114
- type="password"
115
- ).style(container=False)
116
- openai_api_engine_azure = gr.Textbox(
117
- show_label=False,
118
- placeholder="Azure-OpenAI engine here (e.g. gpt35)",
119
- lines=1,
120
- type="password"
121
- ).style(container=False)
122
-
123
-
124
- gr.Markdown(
125
- """
126
- It is recommended to use the Openai paid API or Azure-OpenAI service, because the free Openai API will be limited by the access speed and 3 Requests per minute (very slow).
127
- """)
128
-
129
-
130
- with gr.Column(scale=0.1, min_width=0):
131
- btn1 = gr.Button("OK").style(height= '100px')
132
-
133
  with gr.Row():
134
  with gr.Column(scale=0.9):
135
  input_text = gr.inputs.Textbox(lines=1, placeholder='Please input your problem...', label='what do you want to find? Then press Start')
@@ -156,8 +114,7 @@ with gr.Blocks() as demo:
156
 
157
 
158
 
159
- def set_key(state, openai_api_key,openai_api_key_azure, openai_api_base_azure, openai_api_engine_azure):
160
- return state["client"].set_key(openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure)
161
 
162
 
163
  def run(state, chatbot):
@@ -178,7 +135,7 @@ with gr.Blocks() as demo:
178
 
179
  with gr.Row():
180
  with gr.Column(scale=0.3, min_width="500px", max_width="500px", min_height="500px", max_height="500px"):
181
- Res = gr.Textbox(label="Summary and Result: ")
182
  with gr.Column(scale=0.7, min_width="500px", max_width="500px", min_height="500px", max_height="500px"):
183
  solving_step = gr.Textbox(label="Solving Step: ", lines=5)
184
 
@@ -202,9 +159,6 @@ with gr.Blocks() as demo:
202
  example_selector4.change(fn = change_textbox, inputs = example_selector4, outputs = input_text)
203
 
204
 
205
- if not OPENAI_KEY:
206
- openai_api_key.submit(set_key, [state, openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure], [openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure])
207
- btn1.click(set_key, [state, openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure], [openai_api_key,openai_api_key_azure, openai_api_base_azure, openai_api_engine_azure])
208
 
209
  start_btn.click(fn = run, inputs = [state, input_text], outputs=outputs)
210
  # end_btn.click(stop, state)
 
9
  import time
10
  import os
11
  import pandas as pd
 
12
  css = """#col-container {max-width: 90%; margin-left: auto; margin-right: auto; display: flex; flex-direction: column;}
13
  #header {text-align: center;}
14
  #col-chatbox {flex: 1; max-height: min(750px, 100%);}
 
22
  # plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
23
  # plt.rcParams['axes.unicode_minus'] = False
24
 
25
+ # plt.rcParams['font.sans-serif'] = ['WenQuanYi Zen Hei', 'Noto Sans CJK']
26
+ # plt.rcParams['axes.unicode_minus'] = False
27
 
28
 
29
  example_stock =['给我画一下可孚医疗2022年年中到今天的股价','北向资金今年的每日流入和累计流入','看一下近三年宁德时代和贵州茅台的pb变化','画一下五粮液和泸州老窖从2019年年初到2022年年中的收益率走势','成都银行近一年的k线图和kdj指标','比较下沪深300,创业板指,中证1000指数今年的收益率','今年上证50所有成分股的收益率是多少']
 
39
  self.OPENAI_API_KEY_AZURE = None
40
  self.stop = False # 添加停止标志
41
 
42
+ # def set_key(self, openai_key, openai_key_azure, api_base_azure, engine_azure):
43
+ # self.OPENAI_KEY = openai_key
44
+ # self.OPENAI_API_BASED_AZURE = api_base_azure
45
+ # self.OPENAI_API_KEY_AZURE = openai_key_azure
46
+ # self.OPENAI_ENGINE_AZURE = engine_azure
47
+ # return self.OPENAI_KEY, self.OPENAI_API_KEY_AZURE, self.OPENAI_API_BASED_AZURE, self.OPENAI_ENGINE_AZURE
48
 
49
 
50
  def run(self, messages):
51
+
 
 
 
 
52
  # self.stop = False
53
+ self.OPENAI_KEY = ''
54
+ self.OPENAI_API_KEY_AZURE = os.getenv('OPENAI_API_KEY_AZURE')
55
+ self.OPENAI_API_BASED_AZURE = os.getenv('OPENAI_API_BASED_AZURE')
56
+ self.OPENAI_ENGINE_AZURE = os.getenv('OPENAI_ENGINE_AZURE')
57
+ gen = gradio_interface(messages, self.OPENAI_KEY, self.OPENAI_API_KEY_AZURE, self.OPENAI_API_BASED_AZURE, self.OPENAI_ENGINE_AZURE)
58
+ while not self.stop: #
59
+ try:
60
+ yield next(gen)
61
+ except StopIteration:
62
+ print("StopIteration")
63
+ break
64
 
65
  # yield from gradio_interface(messages, self.OPENAI_KEY)
66
  #return finally_text, img, output, df
 
80
  gr.Markdown(
81
  """
82
  # Hello Data-Copilot ! 😀
83
+
84
  A powerful AI system connects humans and data.
85
+
86
  ## The current version only supports **Chinese financial data**, in the future we will support for other country data
87
+
88
  """)
89
 
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  with gr.Row():
92
  with gr.Column(scale=0.9):
93
  input_text = gr.inputs.Textbox(lines=1, placeholder='Please input your problem...', label='what do you want to find? Then press Start')
 
114
 
115
 
116
 
117
+
 
118
 
119
 
120
  def run(state, chatbot):
 
135
 
136
  with gr.Row():
137
  with gr.Column(scale=0.3, min_width="500px", max_width="500px", min_height="500px", max_height="500px"):
138
+ Res = gr.Textbox(label="Summary and Result: ")
139
  with gr.Column(scale=0.7, min_width="500px", max_width="500px", min_height="500px", max_height="500px"):
140
  solving_step = gr.Textbox(label="Solving Step: ", lines=5)
141
 
 
159
  example_selector4.change(fn = change_textbox, inputs = example_selector4, outputs = input_text)
160
 
161
 
 
 
 
162
 
163
  start_btn.click(fn = run, inputs = [state, input_text], outputs=outputs)
164
  # end_btn.click(stop, state)
app_with_key.py ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
+ from PIL import Image
5
+ from io import BytesIO
6
+ from main import run, add_to_queue,gradio_interface
7
+ import io
8
+ import sys
9
+ import time
10
+ import os
11
+ import pandas as pd
12
+ OPENAI_KEY = None
13
+ css = """#col-container {max-width: 90%; margin-left: auto; margin-right: auto; display: flex; flex-direction: column;}
14
+ #header {text-align: center;}
15
+ #col-chatbox {flex: 1; max-height: min(750px, 100%);}
16
+ #label {font-size: 4em; padding: 0.5em; margin: 0;}
17
+ .scroll-hide {overflow-y: scroll; max-height: 100px;}
18
+ .wrap {max-height: 680px;}
19
+ .message {font-size: 3em;}
20
+ .message-wrap {max-height: min(700px, 100vh);}
21
+ """
22
+
23
+ # plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
24
+ # plt.rcParams['axes.unicode_minus'] = False
25
+
26
+ plt.rcParams['font.sans-serif'] = ['WenQuanYi Zen Hei', 'Noto Sans CJK']
27
+ plt.rcParams['axes.unicode_minus'] = False
28
+
29
+
30
+ example_stock =['给我画一下可孚医疗2022年年中到今天的股价','北向资金今年的每日流入和累计流入','看一下近三年宁德时代和贵州茅台的pb变化','画一下五粮液和泸州老窖从2019年年初到2022年年中的收益率走势','成都银行近一年的k线图和kdj指标','比较下沪深300,创业板指,中证1000指数今年的收益率','今年上证50所有成分股的收益率是多少']
31
+ example_economic =['中国过去十年的cpi走势是什么','过去五年中国的货币供应量走势,并且打印保存','我想看看现在的新闻或者最新的消息','我想看看中国近十年gdp的走势','预测中国未来12个季度的GDP增速']
32
+ example_fund =['易方达的张坤管理了几个基金','基金经理周海栋名下的所有基金今年的收益率情况','我想看看周海栋管理的华商优势行业的近三年来的的净值曲线','比较下华商优势行业和易方达蓝筹精选这两只基金的近三年的收益率']
33
+ example_company =['介绍下贵州茅台,这公司是干什么的,主营业务是什么','我想比较下工商银行和贵州茅台近十年的净资产回报率','今年一季度上证50的成分股的归母净利润同比增速分别是']
34
+
35
+ class Client:
36
+ def __init__(self) -> None:
37
+ self.OPENAI_KEY = OPENAI_KEY
38
+ self.OPENAI_API_BASED_AZURE = None
39
+ self.OPENAI_ENGINE_AZURE = None
40
+ self.OPENAI_API_KEY_AZURE = None
41
+ self.stop = False # 添加停止标志
42
+
43
+ def set_key(self, openai_key, openai_key_azure, api_base_azure, engine_azure):
44
+ self.OPENAI_KEY = openai_key
45
+ self.OPENAI_API_BASED_AZURE = api_base_azure
46
+ self.OPENAI_API_KEY_AZURE = openai_key_azure
47
+ self.OPENAI_ENGINE_AZURE = engine_azure
48
+ return self.OPENAI_KEY, self.OPENAI_API_KEY_AZURE, self.OPENAI_API_BASED_AZURE, self.OPENAI_ENGINE_AZURE
49
+
50
+
51
+ def run(self, messages):
52
+ if self.OPENAI_KEY == '' and self.OPENAI_API_KEY_AZURE == '':
53
+ yield '', np.zeros((100, 100, 3), dtype=np.uint8), "Please set your OpenAI API key first!!!", pd.DataFrame()
54
+ elif len(self.OPENAI_KEY) >= 0 and not self.OPENAI_KEY.startswith('sk') and self.OPENAI_API_KEY_AZURE == '':
55
+ yield '', np.zeros((100, 100, 3), dtype=np.uint8), "Your openai key is incorrect!!!", pd.DataFrame()
56
+ else:
57
+ # self.stop = False
58
+ gen = gradio_interface(messages, self.OPENAI_KEY, self.OPENAI_API_KEY_AZURE, self.OPENAI_API_BASED_AZURE, self.OPENAI_ENGINE_AZURE)
59
+ while not self.stop: #
60
+ try:
61
+ yield next(gen)
62
+ except StopIteration:
63
+ print("StopIteration")
64
+ break
65
+
66
+ # yield from gradio_interface(messages, self.OPENAI_KEY)
67
+ #return finally_text, img, output, df
68
+
69
+
70
+
71
+
72
+
73
+ with gr.Blocks() as demo:
74
+ state = gr.State(value={"client": Client()})
75
+ def change_textbox(query):
76
+ # 根据不同输入对输出控件进行更新
77
+ return gr.update(lines=2, visible=True, value=query)
78
+ # 图片框显示
79
+
80
+ with gr.Row():
81
+ gr.Markdown(
82
+ """
83
+ # Hello Data-Copilot ! 😀
84
+
85
+ A powerful AI system connects humans and data.
86
+
87
+ ## The current version only supports **Chinese financial data**, in the future we will support for other country data
88
+
89
+ """)
90
+
91
+
92
+ if not OPENAI_KEY:
93
+ with gr.Row().style():
94
+ with gr.Column(scale=0.9):
95
+ gr.Markdown(
96
+ """
97
+ You can use gpt35 from openai or from openai-azure ,then press OK.
98
+ """)
99
+ openai_api_key = gr.Textbox(
100
+ show_label=False,
101
+ placeholder="Set your OpenAI API key here and press Submit (e.g. sk-xxx)",
102
+ lines=1,
103
+ type="password"
104
+ ).style(container=False)
105
+
106
+ with gr.Row():
107
+ openai_api_key_azure = gr.Textbox(
108
+ show_label=False,
109
+ placeholder="Set your Azure-OpenAI key",
110
+ lines=1,
111
+ type="password"
112
+ ).style(container=False)
113
+ openai_api_base_azure = gr.Textbox(
114
+ show_label=False,
115
+ placeholder="Azure-OpenAI api_base (e.g. https://zwq0525.openai.azure.com)",
116
+ lines=1,
117
+ type="password"
118
+ ).style(container=False)
119
+ openai_api_engine_azure = gr.Textbox(
120
+ show_label=False,
121
+ placeholder="Azure-OpenAI engine here (e.g. gpt35)",
122
+ lines=1,
123
+ type="password"
124
+ ).style(container=False)
125
+
126
+
127
+ gr.Markdown(
128
+ """
129
+ It is recommended to use the Openai paid API or Azure-OpenAI service, because the free Openai API will be limited by the access speed and 3 Requests per minute (very slow).
130
+ """)
131
+
132
+
133
+ with gr.Column(scale=0.1, min_width=0):
134
+ btn1 = gr.Button("OK").style(height= '100px')
135
+
136
+ with gr.Row():
137
+ with gr.Column(scale=0.9):
138
+ input_text = gr.inputs.Textbox(lines=1, placeholder='Please input your problem...', label='what do you want to find? Then press Start')
139
+
140
+ with gr.Column(scale=0.1, min_width=0):
141
+ start_btn = gr.Button("Start").style(full_height=True)
142
+ # end_btn = gr.Button("Stop").style(full_height=True)
143
+
144
+
145
+ gr.Markdown(
146
+ """
147
+ # Try these examples ➡️➡️
148
+ """)
149
+ with gr.Row():
150
+
151
+ example_selector1 = gr.Dropdown(choices=example_stock, interactive=True,
152
+ label="查股票 Query stock:", show_label=True)
153
+ example_selector2 = gr.Dropdown(choices=example_economic, interactive=True,
154
+ label="查经济 Query Economy:", show_label=True)
155
+ example_selector3 = gr.Dropdown(choices=example_company, interactive=True,
156
+ label="查公司 Query Company:", show_label=True)
157
+ example_selector4 = gr.Dropdown(choices=example_fund, interactive=True,
158
+ label="查基金 Query Fund:", show_label=True)
159
+
160
+
161
+
162
+ def set_key(state, openai_api_key,openai_api_key_azure, openai_api_base_azure, openai_api_engine_azure):
163
+ return state["client"].set_key(openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure)
164
+
165
+
166
+ def run(state, chatbot):
167
+ generator = state["client"].run(chatbot)
168
+ for solving_step, img, res, df in generator:
169
+ # if state["client"].stop:
170
+ # print('Stopping generation')
171
+ # break
172
+ yield solving_step, img, res, df
173
+
174
+
175
+ # def stop(state):
176
+ # print('Stop signal received!')
177
+ # state["client"].stop = True
178
+
179
+
180
+
181
+
182
+ with gr.Row():
183
+ with gr.Column(scale=0.3, min_width="500px", max_width="500px", min_height="500px", max_height="500px"):
184
+ Res = gr.Textbox(label="Summary and Result: ")
185
+ with gr.Column(scale=0.7, min_width="500px", max_width="500px", min_height="500px", max_height="500px"):
186
+ solving_step = gr.Textbox(label="Solving Step: ", lines=5)
187
+
188
+
189
+ img = gr.outputs.Image(type='numpy')
190
+ df = gr.outputs.Dataframe(type='pandas')
191
+ with gr.Row():
192
+ gr.Markdown(
193
+ """
194
+ [Tushare](https://tushare.pro/) provides financial data support for our Data-Copilot.
195
+
196
+ [OpenAI](https://openai.com/) provides the powerful Chatgpt model for our Data-Copilot.
197
+ """)
198
+
199
+
200
+ outputs = [solving_step ,img, Res, df]
201
+ #设置change事件
202
+ example_selector1.change(fn = change_textbox, inputs = example_selector1, outputs = input_text)
203
+ example_selector2.change(fn = change_textbox, inputs = example_selector2, outputs = input_text)
204
+ example_selector3.change(fn = change_textbox, inputs = example_selector3, outputs = input_text)
205
+ example_selector4.change(fn = change_textbox, inputs = example_selector4, outputs = input_text)
206
+
207
+
208
+ if not OPENAI_KEY:
209
+ openai_api_key.submit(set_key, [state, openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure], [openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure])
210
+ btn1.click(set_key, [state, openai_api_key, openai_api_key_azure,openai_api_base_azure, openai_api_engine_azure], [openai_api_key,openai_api_key_azure, openai_api_base_azure, openai_api_engine_azure])
211
+
212
+ start_btn.click(fn = run, inputs = [state, input_text], outputs=outputs)
213
+ # end_btn.click(stop, state)
214
+
215
+
216
+
217
+ demo.queue()
218
+ demo.launch()
219
+
220
+