GitHub Action
๐Ÿš€ Auto-deploy from GitHub Actions
0661b76
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]
)