File size: 5,324 Bytes
3d2aa58
024edca
b8c8e0a
1afbeb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3af7f80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1afbeb8
3af7f80
 
 
 
1afbeb8
024edca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1afbeb8
024edca
 
 
 
 
 
 
 
 
 
 
dde85ab
 
3af7f80
 
024edca
1afbeb8
 
 
 
 
 
024edca
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import shutil
import gradio as gr
from mysite.utilities import chat_with_interpreter, completion, process_file
from interpreter import interpreter
import mysite.interpreter_config  # インポートするだけで設定が適用されます
import importlib
import os
import pkgutil
from routers.chat.chat import demo44 as demo4

def include_gradio_interfaces():
    package_dir = "/home/user/app/routers"
    gradio_interfaces = []
    gradio_names = set()

    for module_info in pkgutil.walk_packages([package_dir], "routers."):
        sub_module_name = module_info.name
        try:
            print(f"Trying to import {sub_module_name}")
            module = importlib.import_module(sub_module_name)
            if hasattr(module, "gradio_interface"):
                print(f"Found gradio_interface in {sub_module_name}")
                interface_name = module_info.name.split(".")[-1]
                if interface_name not in gradio_names:
                    gradio_interfaces.append(module.gradio_interface)
                    gradio_names.add(interface_name)
                else:
                    unique_name = f"{interface_name}_{len(gradio_names)}"
                    gradio_interfaces.append(module.gradio_interface)
                    gradio_names.add(unique_name)
        except ModuleNotFoundError:
            print(f"ModuleNotFoundError: {sub_module_name}")
            pass
        except Exception as e:
            print(f"Failed to import {sub_module_name}: {e}")

    print(f"Collected Gradio Interfaces: {gradio_names}")
    return gradio_interfaces, list(gradio_names)

import os
import gradio as gr

def list_files_in_directory(directory):
    tree = []
    for root, dirs, files in os.walk(directory):
        path = root.split(os.sep)
        for dir_name in dirs:
            tree.append((os.path.join(root, dir_name), '/'.join(path + [dir_name])))
        for file_name in files:
            tree.append((os.path.join(root, file_name), '/'.join(path + [file_name])))
    return tree

def read_file(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            return file.read()
    except UnicodeDecodeError:
        with open(file_path, 'rb') as file:
            content = file.read()
        try:
            return content.decode('utf-8')
        except UnicodeDecodeError:
            try:
                return content.decode('latin-1')
            except UnicodeDecodeError:
                return "Cannot decode file content with utf-8 or latin-1 encoding."

def save_file(file_path, content):
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(content)
    return "File saved successfully"

def on_file_select(selected_file):
    if os.path.isfile(selected_file):
        return read_file(selected_file)
    return ""

def build_interface(base_directory):
    file_list = list_files_in_directory(base_directory)
    file_display = [f[1] for f in file_list]
    file_paths = {f[1]: f[0] for f in file_list}

    with gr.Blocks() as demo:
        gr.Markdown("## File Explorer and Editor")

        file_dropdown = gr.Dropdown(label="Select a file or folder", choices=file_display)
        file_editor = gr.Textbox(label="File Editor", lines=20)
        save_button = gr.Button("Save File")

        def update_editor(selected_display):
            selected_file = file_paths.get(selected_display, "")
            return on_file_select(selected_file)

        def on_edit_button_click(selected_display, new_content):
            selected_file = file_paths.get(selected_display, "")
            if os.path.isfile(selected_file):
                return save_file(selected_file, new_content)
            return "File not found"

        file_dropdown.change(fn=update_editor, inputs=file_dropdown, outputs=file_editor)
        save_button.click(fn=on_edit_button_click, inputs=[file_dropdown, file_editor], outputs=None)

    return demo



#if __name__ == "__main__":
base_directory = "/home/user/app/routers"  # Here you can specify any directory you want to explore
demo = build_interface(base_directory)
#demo.launch()


def setup_gradio_interfaces():
    chat_interface = gr.ChatInterface(
        fn=chat_with_interpreter,
        examples=["サンプルHTMLの作成", "google spreadの読み込み作成", "merhaba"],
        title="Auto Program",
        css=".chat-container { height: 1500px; }",
    )

    chat_interface2 = gr.ChatInterface(
        fn=chat_with_interpreter,
        examples=["こんにちは", "どうしたの?"],
        title="Auto Program 2",
    )
    chat_interface2.queue()



    democs = gr.Interface(
        fn=process_file,
        inputs=[
            "file",
            gr.Textbox(label="Additional Notes", lines=10),
            gr.Textbox(label="Folder Name"),
        ],
        outputs="text",
    )

    from routers.postg.gradio_app import crud_interface

    default_interfaces = [demo4,democs,crud_interface()]#,demo]
    default_names = ["OpenInterpreter","仕様書から作成","Database",]#"demo"]

    gradio_interfaces, gradio_names = include_gradio_interfaces()

    all_interfaces = default_interfaces + gradio_interfaces
    all_names = default_names + gradio_names

    tabs = gr.TabbedInterface(all_interfaces, all_names)
    tabs.queue()
    return tabs