|
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) |
|
|
|
|
|
with gr.Blocks(title="๐ ใใญใณใใ็ฎก็ & ใณใผใ็ๆ") as gradio_interface: |
|
gr.Markdown("# ๐ ใใญใณใใ็ฎก็ & ใใญใฅใกใณใใใใณใผใ็ๆ") |
|
|
|
with gr.Tabs(): |
|
|
|
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) |
|
|
|
|
|
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:] |
|
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:] |
|
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] |
|
) |