Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,65 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
from gradio_client import Client, handle_file
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return None
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
# หมายเหตุ: ตรวจสอบ api_name จาก client.view_api() อีกครั้งหากต้นทางอัปเดต
|
| 18 |
result = client.predict(
|
| 19 |
-
image=handle_file(
|
| 20 |
prompt=prompt,
|
| 21 |
api_name="/predict"
|
| 22 |
)
|
|
|
|
|
|
|
|
|
|
| 23 |
return result
|
| 24 |
except Exception as e:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
submit_btn.click(
|
| 39 |
-
fn=api_bridge,
|
| 40 |
-
inputs=[img_input, prompt_input],
|
| 41 |
-
outputs=img_output
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
from datetime import date
|
| 4 |
from gradio_client import Client, handle_file
|
| 5 |
|
| 6 |
+
# --- CONFIGURATION ---
|
| 7 |
+
HF_TOKEN = "ใส่_TOKEN_ของคุณที่นี่" # เพื่อความเสถียรของ API
|
| 8 |
+
SPACE_SOURCE = "selfit-camera/omni-image-editor"
|
| 9 |
+
DAILY_QUOTA = 3 # จำกัดวันละ 3 ภาพ
|
| 10 |
+
QUOTA_FILE = "usage_quota.json"
|
| 11 |
|
| 12 |
+
# 1. ฟังก์ชันตรวจสอบโควต้า
|
| 13 |
+
def check_quota():
|
| 14 |
+
today = str(date.today())
|
| 15 |
+
if not os.path.exists(QUOTA_FILE):
|
| 16 |
+
usage_data = {"date": today, "count": 0}
|
| 17 |
+
else:
|
| 18 |
+
with open(QUOTA_FILE, "r") as f:
|
| 19 |
+
usage_data = json.load(f)
|
| 20 |
+
|
| 21 |
+
if usage_data["date"] != today:
|
| 22 |
+
usage_data = {"date": today, "count": 0}
|
| 23 |
+
|
| 24 |
+
if usage_data["count"] >= DAILY_QUOTA:
|
| 25 |
+
return False, usage_data["count"]
|
| 26 |
+
return True, usage_data["count"]
|
| 27 |
|
| 28 |
+
def update_quota(current_count):
|
| 29 |
+
with open(QUOTA_FILE, "w") as f:
|
| 30 |
+
json.dump({"date": str(date.today()), "count": current_count + 1}, f)
|
| 31 |
+
|
| 32 |
+
# 2. ฟังก์ชันเรียก API
|
| 33 |
+
def run_editor_api(image_path, prompt):
|
| 34 |
+
can_run, current_count = check_quota()
|
| 35 |
+
|
| 36 |
+
if not can_run:
|
| 37 |
+
print(f"❌ ขออภัย: คุณใช้โควต้าครบ {DAILY_QUOTA} ภาพสำหรับวันนี้แล้ว")
|
| 38 |
return None
|
| 39 |
+
|
| 40 |
+
print(f"🔄 กำลังประมวลผล (ภาพที่ {current_count + 1}/{DAILY_QUOTA})...")
|
| 41 |
+
|
| 42 |
try:
|
| 43 |
+
client = Client(SPACE_SOURCE, hf_token=HF_TOKEN)
|
|
|
|
| 44 |
result = client.predict(
|
| 45 |
+
image=handle_file(image_path),
|
| 46 |
prompt=prompt,
|
| 47 |
api_name="/predict"
|
| 48 |
)
|
| 49 |
+
|
| 50 |
+
update_quota(current_count)
|
| 51 |
+
print("✅ ประมวลผลสำเร็จ!")
|
| 52 |
return result
|
| 53 |
except Exception as e:
|
| 54 |
+
print(f"❌ เกิดข้อผิดพลาดจาก API: {e}")
|
| 55 |
+
return None
|
| 56 |
+
|
| 57 |
+
# --- ส่วนการใช้งาน ---
|
| 58 |
+
# ใส่พาธรูปภาพ และ Prompt ที่ต้องการแก้ไข
|
| 59 |
+
my_image = "input.jpg"
|
| 60 |
+
my_prompt = "change the background to a beach"
|
| 61 |
+
|
| 62 |
+
output_path = run_editor_api(my_image, my_prompt)
|
| 63 |
+
|
| 64 |
+
if output_path:
|
| 65 |
+
print(f"ดาวน์โหลดผลลัพธ์ได้ที่: {output_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|