File size: 10,272 Bytes
0661b76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import gradio as gr
from mysite.libs.utilities import chat_with_interpreter, completion, process_file, no_process_file
from interpreter import interpreter
import mysite.interpreter.interpreter_config  # インポートするだけで設定が適用されます
import sqlite3
import os
from datetime import datetime
from typing import List, Tuple, Optional

# データベース設定
DB_PATH = "prompts.db"

def init_db():
    """プロンプトデータベースの初期化"""
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS prompts (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            title TEXT NOT NULL,
            url TEXT,
            content TEXT NOT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )
    ''')
    
    # デフォルトプロンプトの追加(初回のみ)
    cursor.execute('SELECT COUNT(*) FROM prompts')
    if cursor.fetchone()[0] == 0:
        default_prompt = """# gradio で miiboのナレッジに登録する画面の作成
  gradio_interface interfacec name

# fastapi
  gradio apiに接続するAPI
  router で作成

1ファイルで作成
仕様書の作成
plantumlで図にする

#sample fastapi
import requests
import json
import os

from fastapi import APIRouter, HTTPException
from gradio_client import Client

router = APIRouter(prefix="/gradio", tags=["gradio"])
@router.get("/route/gradio")

def get_senario(id,res):
    table = "LOG"
    client = Client("kenken999/fastapi_django_main_live")
    result = client.predict(
            message="Hello!!",
            request=0.95,
            param_3=512,
            api_name="/chat"
    )
    return result
"""
        cursor.execute(
            'INSERT INTO prompts (title, url, content) VALUES (?, ?, ?)',
            ('デフォルト:Gradio + FastAPI作成', 'https://example.com', default_prompt)
        )
    
    conn.commit()
    conn.close()

def save_prompt(title: str, url: str, content: str) -> str:
    """プロンプトを保存"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute(
            'INSERT INTO prompts (title, url, content) VALUES (?, ?, ?)',
            (title, url, content)
        )
        
        conn.commit()
        conn.close()
        return f"✅ プロンプト '{title}' を保存しました!"
    except Exception as e:
        return f"❌ エラー: {str(e)}"

def get_prompts() -> List[Tuple]:
    """全プロンプトを取得"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('SELECT id, title, url, created_at FROM prompts ORDER BY created_at DESC')
        prompts = cursor.fetchall()
        
        conn.close()
        return prompts
    except Exception as e:
        print(f"プロンプト取得エラー: {e}")
        return []

def get_prompt_content(prompt_id: int) -> str:
    """指定IDのプロンプト内容を取得"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('SELECT content FROM prompts WHERE id = ?', (prompt_id,))
        result = cursor.fetchone()
        
        conn.close()
        return result[0] if result else ""
    except Exception as e:
        print(f"プロンプト内容取得エラー: {e}")
        return ""

def delete_prompt(prompt_id: int) -> str:
    """プロンプトを削除"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('DELETE FROM prompts WHERE id = ?', (prompt_id,))
        
        if cursor.rowcount > 0:
            conn.commit()
            conn.close()
            return "✅ プロンプトを削除しました"
        else:
            conn.close()
            return "❌ プロンプトが見つかりません"
    except Exception as e:
        return f"❌ 削除エラー: {str(e)}"

# データベース初期化
init_db()

def load_prompt_from_db(prompt_id):
    """選択されたプロンプトを読み込み"""
    if prompt_id:
        content = get_prompt_content(int(prompt_id))
        return content
    return ""

def refresh_prompt_list():
    """プロンプト一覧を更新"""
    prompts = get_prompts()
    choices = []
    for prompt in prompts:
        id_, title, url, created_at = prompt
        display_text = f"[{id_}] {title} ({created_at[:10]})"
        choices.append((display_text, str(id_)))
    return gr.Dropdown(choices=choices, label="📋 保存済みプロンプト一覧", value=None)

# Gradioインターフェース作成
with gr.Blocks(title="🚀 プロンプト管理 & コード生成") as gradio_interface:
    gr.Markdown("# 🚀 プロンプト管理 & ドキュメントからコード生成")
    
    with gr.Tabs():
        # タブ1: プロンプト管理
        with gr.TabItem("📝 プロンプト管理"):
            gr.Markdown("## プロンプトの保存・管理")
            
            with gr.Row():
                with gr.Column(scale=1):
                    # プロンプト保存フォーム
                    save_title = gr.Textbox(label="📋 タイトル", placeholder="例: FastAPI + Gradio作成プロンプト")
                    save_url = gr.Textbox(label="🔗 参考URL (任意)", placeholder="https://example.com")
                    save_content = gr.Textbox(
                        label="📝 プロンプト内容", 
                        lines=10,
                        placeholder="プロンプトの内容を入力してください..."
                    )
                    save_btn = gr.Button("💾 プロンプトを保存", variant="primary")
                    save_status = gr.Textbox(label="保存結果", interactive=False)
                
                with gr.Column(scale=1):
                    # プロンプト一覧
                    prompt_dropdown = gr.Dropdown(
                        choices=[], 
                        label="📋 保存済みプロンプト一覧",
                        interactive=True
                    )
                    refresh_btn = gr.Button("🔄 一覧を更新")
                    load_btn = gr.Button("📥 選択したプロンプトを読み込み", variant="secondary")
                    delete_btn = gr.Button("🗑️ 選択したプロンプトを削除", variant="stop")
                    delete_status = gr.Textbox(label="削除結果", interactive=False)
        
        # タブ2: コード生成
        with gr.TabItem("⚡ コード生成"):
            gr.Markdown("## ドキュメントからコード生成")
            
            with gr.Row():
                with gr.Column():
                    # ファイルアップロード
                    input_file = gr.File(label="📄 ドキュメントファイル")
                    
                    # プロンプト表示・編集エリア
                    current_prompt = gr.Textbox(
                        label="📝 現在のプロンプト", 
                        lines=15,
                        value="",
                        placeholder="上のタブでプロンプトを選択するか、直接入力してください..."
                    )
                    
                with gr.Column():
                    # 生成設定
                    folder_name = gr.Textbox(label="📁 出力フォルダ名", value="generated_code")
                    github_token = gr.Textbox(label="🔑 GitHub Token (任意)", type="password", value="")
                    
                    # 生成ボタン
                    generate_btn = gr.Button("🚀 コード生成実行", variant="primary", size="lg")
                    
                    # 結果表示
                    result_output = gr.Textbox(label="📤 生成結果", lines=10, interactive=False)
    
    # イベントハンドラー
    def handle_save_prompt(title, url, content):
        if not title.strip() or not content.strip():
            return "❌ タイトルとプロンプト内容は必須です"
        return save_prompt(title, url, content)
    
    def handle_refresh_list():
        prompts = get_prompts()
        choices = []
        for prompt in prompts:
            id_, title, url, created_at = prompt
            display_text = f"[{id_}] {title} ({created_at[:10]})"
            choices.append((display_text, str(id_)))
        return gr.Dropdown(choices=choices, value=None)
    
    def handle_load_prompt(selected_prompt):
        if selected_prompt:
            prompt_id = selected_prompt.split(']')[0][1:]  # [1] から ] までを取得してIDを抽出
            content = get_prompt_content(int(prompt_id))
            return content
        return ""
    
    def handle_delete_prompt(selected_prompt):
        if selected_prompt:
            prompt_id = selected_prompt.split(']')[0][1:]  # IDを抽出
            return delete_prompt(int(prompt_id))
        return "❌ プロンプトが選択されていません"
    
    def handle_generate_code(file, prompt, folder, token):
        if not prompt.strip():
            return "❌ プロンプトが入力されていません"
        return process_file(file, prompt, folder, token)
    
    # イベント接続
    save_btn.click(
        handle_save_prompt,
        inputs=[save_title, save_url, save_content],
        outputs=[save_status]
    )
    
    refresh_btn.click(
        handle_refresh_list,
        outputs=[prompt_dropdown]
    )
    
    load_btn.click(
        handle_load_prompt,
        inputs=[prompt_dropdown],
        outputs=[current_prompt]
    )
    
    delete_btn.click(
        handle_delete_prompt,
        inputs=[prompt_dropdown],
        outputs=[delete_status]
    )
    
    generate_btn.click(
        handle_generate_code,
        inputs=[input_file, current_prompt, folder_name, github_token],
        outputs=[result_output]
    )
    
    # ページ読み込み時にプロンプト一覧を初期化
    gradio_interface.load(
        handle_refresh_list,
        outputs=[prompt_dropdown]
    )