Download app.py from kunkun110/tc: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/spaces/kunkun110/tc/resolve/main/app.py
- Command line
-
hf download hf://spaces/kunkun110/tc/app.py
-
curl -L -o app.py https://huggingface.co/spaces/kunkun110/tc/resolve/main/app.py
3.07 kB
| import gradio as gr | |
| import sqlite3 | |
| import uuid | |
| import base64 | |
| from datetime import datetime | |
| # 初始化数据库 | |
| def init_db(): | |
| conn = sqlite3.connect('images.db') | |
| c = conn.cursor() | |
| c.execute('''CREATE TABLE IF NOT EXISTS images | |
| (id TEXT PRIMARY KEY, filename TEXT, data BLOB, created_at TEXT)''') | |
| conn.commit() | |
| conn.close() | |
| init_db() | |
| def upload_image(image): | |
| if image is None: | |
| return "请选择图片", None, None | |
| conn = sqlite3.connect('images.db') | |
| c = conn.cursor() | |
| image_id = str(uuid.uuid4())[:8] | |
| # 读取图片数据 | |
| with open(image, 'rb') as f: | |
| img_data = f.read() | |
| filename = image.split('/')[-1] | |
| created_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
| c.execute("INSERT INTO images VALUES (?, ?, ?, ?)", | |
| (image_id, filename, img_data, created_at)) | |
| conn.commit() | |
| conn.close() | |
| # 生成链接(需要替换为你的 Space URL) | |
| link = f"图片ID: {image_id}" | |
| return f"上传成功!\n{link}", image_id, image | |
| def get_image(image_id): | |
| if not image_id: | |
| return "请输入图片ID", None | |
| conn = sqlite3.connect('images.db') | |
| c = conn.cursor() | |
| c.execute("SELECT data, filename FROM images WHERE id=?", (image_id,)) | |
| result = c.fetchone() | |
| conn.close() | |
| if result: | |
| img_data, filename = result | |
| temp_path = f"/tmp/{filename}" | |
| with open(temp_path, 'wb') as f: | |
| f.write(img_data) | |
| return f"找到图片: {filename}", temp_path | |
| return "图片不存在", None | |
| def list_images(): | |
| conn = sqlite3.connect('images.db') | |
| c = conn.cursor() | |
| c.execute("SELECT id, filename, created_at FROM images ORDER BY created_at DESC LIMIT 20") | |
| results = c.fetchall() | |
| conn.close() | |
| if not results: | |
| return "暂无图片" | |
| output = "最近上传的图片:\n\n" | |
| for r in results: | |
| output += f"ID: {r[0]} | 文件名: {r[1]} | 时间: {r[2]}\n" | |
| return output | |
| # Gradio 界面 | |
| with gr.Blocks(title="简易图床") as demo: | |
| gr.Markdown("# 🖼️ 简易图床") | |
| with gr.Tab("上传图片"): | |
| upload_input = gr.Image(type="filepath", label="选择图片") | |
| upload_btn = gr.Button("上传") | |
| upload_output = gr.Textbox(label="结果") | |
| upload_id = gr.Textbox(label="图片ID") | |
| upload_preview = gr.Image(label="预览") | |
| upload_btn.click(upload_image, upload_input, [upload_output, upload_id, upload_preview]) | |
| with gr.Tab("获取图片"): | |
| get_input = gr.Textbox(label="输入图片ID") | |
| get_btn = gr.Button("获取") | |
| get_output = gr.Textbox(label="结果") | |
| get_image_output = gr.Image(label="图片") | |
| get_btn.click(get_image, get_input, [get_output, get_image_output]) | |
| with gr.Tab("图片列表"): | |
| list_btn = gr.Button("刷新列表") | |
| list_output = gr.Textbox(label="图片列表", lines=15) | |
| list_btn.click(list_images, None, list_output) | |
| demo.launch(share=True) | |