Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,84 @@
|
|
1 |
-
import
|
2 |
-
from openai import AsyncOpenAI
|
3 |
import os
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
""
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
"content": "あなたは会計士と税理士の資格を持っており、全ての質問にjsonで回答します。"
|
29 |
-
},
|
30 |
-
{
|
31 |
-
"role": "user",
|
32 |
-
"content": prompt
|
33 |
-
|
34 |
-
},
|
35 |
-
],
|
36 |
-
top_p=1,
|
37 |
-
frequency_penalty=0,
|
38 |
-
presence_penalty=0
|
39 |
-
)
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
|
|
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
import os
|
3 |
+
import importlib
|
4 |
+
import re
|
5 |
+
import ast
|
6 |
+
import inspect
|
7 |
+
|
8 |
+
#UIを__doc__から生成する
|
9 |
+
def get_io_types(func):
|
10 |
+
doc = func.__doc__
|
11 |
+
|
12 |
+
input_pattern = r"input\d* \((\w+)\): (.+)?" #inputから始まる行は、型とサンプル
|
13 |
+
input_matches = re.finditer(input_pattern, doc)
|
14 |
+
inputs = []
|
15 |
+
# 関数の引数名を取得
|
16 |
+
sig = inspect.signature(func)
|
17 |
+
param_names = list(sig.parameters.keys())
|
18 |
+
|
19 |
+
for match, param_name in zip(input_matches, param_names):
|
20 |
+
typ = match.group(1)
|
21 |
+
sample = match.group(2).strip() if match.group(2) else "" # サンプルがない場合は
|
22 |
+
inputs.append((param_name, typ, sample))
|
23 |
+
|
24 |
+
output_pattern = r"output\d* \((\w+)\): ([^\n]+)" # outputから始まる行は、型とUIに表示する説明
|
25 |
+
output_matches = re.findall(output_pattern, doc)
|
26 |
+
outputs = [(typ, name.strip()) for typ, name in output_matches]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
return inputs, outputs
|
29 |
+
|
30 |
+
|
31 |
+
def create_component(type,name,sample=None):
|
32 |
+
# 出力タイプに対応するGradioコンポーネント生成関数の辞書を定義
|
33 |
+
component_factory = {
|
34 |
+
"image": lambda: gr.Image(label=name, type='pil'),
|
35 |
+
"image_filepath": lambda: gr.Image(label=name, type='filepath'),
|
36 |
+
"text": lambda: gr.Textbox(label=name, value=sample),
|
37 |
+
"dataframe": lambda: gr.DataFrame(label=name, value=sample),
|
38 |
+
}
|
39 |
|
40 |
+
# 出力タイプに対応するコンポーネント生成関数を辞書から取得し、関数を実行して新しいコンポーネントを生成
|
41 |
+
return component_factory.get(type, lambda: gr.Textbox(label="Output(unknown)"))()
|
42 |
|
43 |
+
def load_apis():
|
44 |
+
api_dir = 'apis'
|
45 |
+
apis = {}
|
46 |
+
for file in os.listdir(api_dir):
|
47 |
+
if file.endswith('.py') and not file.startswith('__'):
|
48 |
+
module_name = file[:-3]
|
49 |
+
module = importlib.import_module(f"apis.{module_name}")
|
50 |
+
func = getattr(module, module_name, None)
|
51 |
+
input_types, output_types = get_io_types(func)
|
52 |
+
apis[module_name] = (func, input_types, output_types)
|
53 |
+
print(apis)
|
54 |
+
return apis
|
55 |
+
|
56 |
+
def create_app(apis):
|
57 |
+
with gr.Blocks() as app:
|
58 |
+
with gr.Tabs():
|
59 |
+
buttons = {}
|
60 |
+
for api_name, (api_func,input_types, output_types) in apis.items():
|
61 |
+
with gr.Tab(api_name):
|
62 |
+
with gr.Column():
|
63 |
+
print("Constructing: ", api_name, input_types, output_types)
|
64 |
+
with gr.Row():
|
65 |
+
inputs = []
|
66 |
+
for param_name, input_type, sample in input_types:
|
67 |
+
with gr.Column():
|
68 |
+
input_component = create_component(input_type, param_name, sample)
|
69 |
+
inputs.append(input_component)
|
70 |
+
with gr.Row():
|
71 |
+
buttons[api_name] = gr.Button("↓Run↓")
|
72 |
+
with gr.Row():
|
73 |
+
outputs = []
|
74 |
+
for output_type, name in output_types:
|
75 |
+
with gr.Column():
|
76 |
+
output_component = create_component(output_type, name)
|
77 |
+
outputs.append(output_component)
|
78 |
+
buttons[api_name].click(api_func, inputs=inputs, outputs=outputs)
|
79 |
+
return app
|
80 |
+
|
81 |
|
82 |
+
apis = load_apis()
|
83 |
+
app = create_app(apis)
|
84 |
+
app.launch(debug=True)
|