GitHub Action
commited on
Commit
ยท
41f215f
1
Parent(s):
5c5ca8d
๐ Auto-deploy from GitHub Actions
Browse files
controllers/gra_03_programfromdocgas/programfromdocAI.py
CHANGED
|
@@ -1,17 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from mysite.libs.utilities import chat_with_interpreter, completion, process_file,no_process_file
|
| 3 |
from interpreter import interpreter
|
| 4 |
import mysite.interpreter.interpreter_config # ใคใณใใผใใใใ ใใง่จญๅฎใ้ฉ็จใใใพใ
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
gradio_interface interfacec name
|
| 16 |
|
| 17 |
# fastapi
|
|
@@ -26,21 +44,15 @@ plantumlใงๅณใซใใ
|
|
| 26 |
import requests
|
| 27 |
import json
|
| 28 |
import os
|
| 29 |
-
# current_user: User = Depends(get_current_active_user)):
|
| 30 |
-
# oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/token")
|
| 31 |
-
# current_user: User = Depends(get_current_active_user)):
|
| 32 |
-
# oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/token")
|
| 33 |
|
| 34 |
from fastapi import APIRouter, HTTPException
|
| 35 |
from gradio_client import Client
|
| 36 |
|
| 37 |
-
#router = APIRouter()
|
| 38 |
router = APIRouter(prefix="/gradio", tags=["gradio"])
|
| 39 |
@router.get("/route/gradio")
|
| 40 |
|
| 41 |
def get_senario(id,res):
|
| 42 |
table = "LOG"
|
| 43 |
-
|
| 44 |
client = Client("kenken999/fastapi_django_main_live")
|
| 45 |
result = client.predict(
|
| 46 |
message="Hello!!",
|
|
@@ -49,19 +61,227 @@ def get_senario(id,res):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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from mysite.libs.utilities import chat_with_interpreter, completion, process_file, no_process_file
|
| 3 |
from interpreter import interpreter
|
| 4 |
import mysite.interpreter.interpreter_config # ใคใณใใผใใใใ ใใง่จญๅฎใ้ฉ็จใใใพใ
|
| 5 |
+
import sqlite3
|
| 6 |
+
import os
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
from typing import List, Tuple, Optional
|
| 9 |
+
|
| 10 |
+
# ใใผใฟใใผใน่จญๅฎ
|
| 11 |
+
DB_PATH = "prompts.db"
|
| 12 |
+
|
| 13 |
+
def init_db():
|
| 14 |
+
"""ใใญใณใใใใผใฟใใผในใฎๅๆๅ"""
|
| 15 |
+
conn = sqlite3.connect(DB_PATH)
|
| 16 |
+
cursor = conn.cursor()
|
| 17 |
+
|
| 18 |
+
cursor.execute('''
|
| 19 |
+
CREATE TABLE IF NOT EXISTS prompts (
|
| 20 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 21 |
+
title TEXT NOT NULL,
|
| 22 |
+
url TEXT,
|
| 23 |
+
content TEXT NOT NULL,
|
| 24 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 25 |
+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 26 |
+
)
|
| 27 |
+
''')
|
| 28 |
+
|
| 29 |
+
# ใใใฉใซใใใญใณใใใฎ่ฟฝๅ ๏ผๅๅใฎใฟ๏ผ
|
| 30 |
+
cursor.execute('SELECT COUNT(*) FROM prompts')
|
| 31 |
+
if cursor.fetchone()[0] == 0:
|
| 32 |
+
default_prompt = """# gradio ใง miiboใฎใใฌใใธใซ็ป้ฒใใ็ป้ขใฎไฝๆ
|
| 33 |
gradio_interface interfacec name
|
| 34 |
|
| 35 |
# fastapi
|
|
|
|
| 44 |
import requests
|
| 45 |
import json
|
| 46 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
from fastapi import APIRouter, HTTPException
|
| 49 |
from gradio_client import Client
|
| 50 |
|
|
|
|
| 51 |
router = APIRouter(prefix="/gradio", tags=["gradio"])
|
| 52 |
@router.get("/route/gradio")
|
| 53 |
|
| 54 |
def get_senario(id,res):
|
| 55 |
table = "LOG"
|
|
|
|
| 56 |
client = Client("kenken999/fastapi_django_main_live")
|
| 57 |
result = client.predict(
|
| 58 |
message="Hello!!",
|
|
|
|
| 61 |
api_name="/chat"
|
| 62 |
)
|
| 63 |
return result
|
| 64 |
+
"""
|
| 65 |
+
cursor.execute(
|
| 66 |
+
'INSERT INTO prompts (title, url, content) VALUES (?, ?, ?)',
|
| 67 |
+
('ใใใฉใซใ๏ผGradio + FastAPIไฝๆ', 'https://example.com', default_prompt)
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
conn.commit()
|
| 71 |
+
conn.close()
|
| 72 |
|
| 73 |
+
def save_prompt(title: str, url: str, content: str) -> str:
|
| 74 |
+
"""ใใญใณใใใไฟๅญ"""
|
| 75 |
+
try:
|
| 76 |
+
conn = sqlite3.connect(DB_PATH)
|
| 77 |
+
cursor = conn.cursor()
|
| 78 |
+
|
| 79 |
+
cursor.execute(
|
| 80 |
+
'INSERT INTO prompts (title, url, content) VALUES (?, ?, ?)',
|
| 81 |
+
(title, url, content)
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
conn.commit()
|
| 85 |
+
conn.close()
|
| 86 |
+
return f"โ
ใใญใณใใ '{title}' ใไฟๅญใใพใใ๏ผ"
|
| 87 |
+
except Exception as e:
|
| 88 |
+
return f"โ ใจใฉใผ: {str(e)}"
|
| 89 |
|
| 90 |
+
def get_prompts() -> List[Tuple]:
|
| 91 |
+
"""ๅ
จใใญใณใใใๅๅพ"""
|
| 92 |
+
try:
|
| 93 |
+
conn = sqlite3.connect(DB_PATH)
|
| 94 |
+
cursor = conn.cursor()
|
| 95 |
+
|
| 96 |
+
cursor.execute('SELECT id, title, url, created_at FROM prompts ORDER BY created_at DESC')
|
| 97 |
+
prompts = cursor.fetchall()
|
| 98 |
+
|
| 99 |
+
conn.close()
|
| 100 |
+
return prompts
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"ใใญใณใใๅๅพใจใฉใผ: {e}")
|
| 103 |
+
return []
|
| 104 |
|
| 105 |
+
def get_prompt_content(prompt_id: int) -> str:
|
| 106 |
+
"""ๆๅฎIDใฎใใญใณใใๅ
ๅฎนใๅๅพ"""
|
| 107 |
+
try:
|
| 108 |
+
conn = sqlite3.connect(DB_PATH)
|
| 109 |
+
cursor = conn.cursor()
|
| 110 |
+
|
| 111 |
+
cursor.execute('SELECT content FROM prompts WHERE id = ?', (prompt_id,))
|
| 112 |
+
result = cursor.fetchone()
|
| 113 |
+
|
| 114 |
+
conn.close()
|
| 115 |
+
return result[0] if result else ""
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print(f"ใใญใณใใๅ
ๅฎนๅๅพใจใฉใผ: {e}")
|
| 118 |
+
return ""
|
| 119 |
+
|
| 120 |
+
def delete_prompt(prompt_id: int) -> str:
|
| 121 |
+
"""ใใญใณใใใๅ้ค"""
|
| 122 |
+
try:
|
| 123 |
+
conn = sqlite3.connect(DB_PATH)
|
| 124 |
+
cursor = conn.cursor()
|
| 125 |
+
|
| 126 |
+
cursor.execute('DELETE FROM prompts WHERE id = ?', (prompt_id,))
|
| 127 |
+
|
| 128 |
+
if cursor.rowcount > 0:
|
| 129 |
+
conn.commit()
|
| 130 |
+
conn.close()
|
| 131 |
+
return "โ
ใใญใณใใใๅ้คใใพใใ"
|
| 132 |
+
else:
|
| 133 |
+
conn.close()
|
| 134 |
+
return "โ ใใญใณใใใ่ฆใคใใใพใใ"
|
| 135 |
+
except Exception as e:
|
| 136 |
+
return f"โ ๅ้คใจใฉใผ: {str(e)}"
|
| 137 |
|
| 138 |
+
# ใใผใฟใใผในๅๆๅ
|
| 139 |
+
init_db()
|
| 140 |
|
| 141 |
+
def load_prompt_from_db(prompt_id):
|
| 142 |
+
"""้ธๆใใใใใญใณใใใ่ชญใฟ่พผใฟ"""
|
| 143 |
+
if prompt_id:
|
| 144 |
+
content = get_prompt_content(int(prompt_id))
|
| 145 |
+
return content
|
| 146 |
+
return ""
|
| 147 |
+
|
| 148 |
+
def refresh_prompt_list():
|
| 149 |
+
"""ใใญใณใใไธ่ฆงใๆดๆฐ"""
|
| 150 |
+
prompts = get_prompts()
|
| 151 |
+
choices = []
|
| 152 |
+
for prompt in prompts:
|
| 153 |
+
id_, title, url, created_at = prompt
|
| 154 |
+
display_text = f"[{id_}] {title} ({created_at[:10]})"
|
| 155 |
+
choices.append((display_text, str(id_)))
|
| 156 |
+
return gr.Dropdown(choices=choices, label="๐ ไฟๅญๆธใฟใใญใณใใไธ่ฆง", value=None)
|
| 157 |
+
|
| 158 |
+
# Gradioใคใณใฟใผใใงใผในไฝๆ
|
| 159 |
+
with gr.Blocks(title="๐ ใใญใณใใ็ฎก็ & ใณใผใ็ๆ") as gradio_interface:
|
| 160 |
+
gr.Markdown("# ๐ ใใญใณใใ็ฎก็ & ใใญใฅใกใณใใใใณใผใ็ๆ")
|
| 161 |
+
|
| 162 |
+
with gr.Tabs():
|
| 163 |
+
# ใฟใ1: ใใญใณใใ็ฎก็
|
| 164 |
+
with gr.TabItem("๐ ใใญใณใใ็ฎก็"):
|
| 165 |
+
gr.Markdown("## ใใญใณใใใฎไฟๅญใป็ฎก็")
|
| 166 |
+
|
| 167 |
+
with gr.Row():
|
| 168 |
+
with gr.Column(scale=1):
|
| 169 |
+
# ใใญใณใใไฟๅญใใฉใผใ
|
| 170 |
+
save_title = gr.Textbox(label="๐ ใฟใคใใซ", placeholder="ไพ: FastAPI + Gradioไฝๆใใญใณใใ")
|
| 171 |
+
save_url = gr.Textbox(label="๐ ๅ่URL (ไปปๆ)", placeholder="https://example.com")
|
| 172 |
+
save_content = gr.Textbox(
|
| 173 |
+
label="๐ ใใญใณใใๅ
ๅฎน",
|
| 174 |
+
lines=10,
|
| 175 |
+
placeholder="ใใญใณใใใฎๅ
ๅฎนใๅ
ฅๅใใฆใใ ใใ..."
|
| 176 |
+
)
|
| 177 |
+
save_btn = gr.Button("๐พ ใใญใณใใใไฟๅญ", variant="primary")
|
| 178 |
+
save_status = gr.Textbox(label="ไฟๅญ็ตๆ", interactive=False)
|
| 179 |
+
|
| 180 |
+
with gr.Column(scale=1):
|
| 181 |
+
# ใใญใณใใไธ่ฆง
|
| 182 |
+
prompt_dropdown = gr.Dropdown(
|
| 183 |
+
choices=[],
|
| 184 |
+
label="๐ ไฟๅญๆธใฟใใญใณใใไธ่ฆง",
|
| 185 |
+
interactive=True
|
| 186 |
+
)
|
| 187 |
+
refresh_btn = gr.Button("๐ ไธ่ฆงใๆดๆฐ")
|
| 188 |
+
load_btn = gr.Button("๐ฅ ้ธๆใใใใญใณใใใ่ชญใฟ่พผใฟ", variant="secondary")
|
| 189 |
+
delete_btn = gr.Button("๐๏ธ ้ธๆใใใใญใณใใใๅ้ค", variant="stop")
|
| 190 |
+
delete_status = gr.Textbox(label="ๅ้ค็ตๆ", interactive=False)
|
| 191 |
+
|
| 192 |
+
# ใฟใ2: ใณใผใ็ๆ
|
| 193 |
+
with gr.TabItem("โก ใณใผใ็ๆ"):
|
| 194 |
+
gr.Markdown("## ใใญใฅใกใณใใใใณใผใ็ๆ")
|
| 195 |
+
|
| 196 |
+
with gr.Row():
|
| 197 |
+
with gr.Column():
|
| 198 |
+
# ใใกใคใซใขใใใญใผใ
|
| 199 |
+
input_file = gr.File(label="๐ ใใญใฅใกใณใใใกใคใซ")
|
| 200 |
+
|
| 201 |
+
# ใใญใณใใ่กจ็คบใป็ทจ้ใจใชใข
|
| 202 |
+
current_prompt = gr.Textbox(
|
| 203 |
+
label="๐ ็พๅจใฎใใญใณใใ",
|
| 204 |
+
lines=15,
|
| 205 |
+
value="",
|
| 206 |
+
placeholder="ไธใฎใฟใใงใใญใณใใใ้ธๆใใใใ็ดๆฅๅ
ฅๅใใฆใใ ใใ..."
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
with gr.Column():
|
| 210 |
+
# ็ๆ่จญๅฎ
|
| 211 |
+
folder_name = gr.Textbox(label="๐ ๅบๅใใฉใซใๅ", value="generated_code")
|
| 212 |
+
github_token = gr.Textbox(label="๐ GitHub Token (ไปปๆ)", type="password", value="")
|
| 213 |
+
|
| 214 |
+
# ็ๆใใฟใณ
|
| 215 |
+
generate_btn = gr.Button("๐ ใณใผใ็ๆๅฎ่ก", variant="primary", size="lg")
|
| 216 |
+
|
| 217 |
+
# ็ตๆ่กจ็คบ
|
| 218 |
+
result_output = gr.Textbox(label="๐ค ็ๆ็ตๆ", lines=10, interactive=False)
|
| 219 |
+
|
| 220 |
+
# ใคใใณใใใณใใฉใผ
|
| 221 |
+
def handle_save_prompt(title, url, content):
|
| 222 |
+
if not title.strip() or not content.strip():
|
| 223 |
+
return "โ ใฟใคใใซใจใใญใณใใๅ
ๅฎนใฏๅฟ
้ ใงใ"
|
| 224 |
+
return save_prompt(title, url, content)
|
| 225 |
+
|
| 226 |
+
def handle_refresh_list():
|
| 227 |
+
prompts = get_prompts()
|
| 228 |
+
choices = []
|
| 229 |
+
for prompt in prompts:
|
| 230 |
+
id_, title, url, created_at = prompt
|
| 231 |
+
display_text = f"[{id_}] {title} ({created_at[:10]})"
|
| 232 |
+
choices.append((display_text, str(id_)))
|
| 233 |
+
return gr.Dropdown(choices=choices, value=None)
|
| 234 |
+
|
| 235 |
+
def handle_load_prompt(selected_prompt):
|
| 236 |
+
if selected_prompt:
|
| 237 |
+
prompt_id = selected_prompt.split(']')[0][1:] # [1] ใใ ] ใพใงใๅๅพใใฆIDใๆฝๅบ
|
| 238 |
+
content = get_prompt_content(int(prompt_id))
|
| 239 |
+
return content
|
| 240 |
+
return ""
|
| 241 |
+
|
| 242 |
+
def handle_delete_prompt(selected_prompt):
|
| 243 |
+
if selected_prompt:
|
| 244 |
+
prompt_id = selected_prompt.split(']')[0][1:] # IDใๆฝๅบ
|
| 245 |
+
return delete_prompt(int(prompt_id))
|
| 246 |
+
return "โ ใใญใณใใใ้ธๆใใใฆใใพใใ"
|
| 247 |
+
|
| 248 |
+
def handle_generate_code(file, prompt, folder, token):
|
| 249 |
+
if not prompt.strip():
|
| 250 |
+
return "โ ใใญใณใใใๅ
ฅๅใใใฆใใพใใ"
|
| 251 |
+
return process_file(file, prompt, folder, token)
|
| 252 |
+
|
| 253 |
+
# ใคใใณใๆฅ็ถ
|
| 254 |
+
save_btn.click(
|
| 255 |
+
handle_save_prompt,
|
| 256 |
+
inputs=[save_title, save_url, save_content],
|
| 257 |
+
outputs=[save_status]
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
refresh_btn.click(
|
| 261 |
+
handle_refresh_list,
|
| 262 |
+
outputs=[prompt_dropdown]
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
load_btn.click(
|
| 266 |
+
handle_load_prompt,
|
| 267 |
+
inputs=[prompt_dropdown],
|
| 268 |
+
outputs=[current_prompt]
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
delete_btn.click(
|
| 272 |
+
handle_delete_prompt,
|
| 273 |
+
inputs=[prompt_dropdown],
|
| 274 |
+
outputs=[delete_status]
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
generate_btn.click(
|
| 278 |
+
handle_generate_code,
|
| 279 |
+
inputs=[input_file, current_prompt, folder_name, github_token],
|
| 280 |
+
outputs=[result_output]
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
# ใใผใธ่ชญใฟ่พผใฟๆใซใใญใณใใไธ่ฆงใๅๆๅ
|
| 284 |
+
gradio_interface.load(
|
| 285 |
+
handle_refresh_list,
|
| 286 |
+
outputs=[prompt_dropdown]
|
| 287 |
+
)
|