Saravutw commited on
Commit
7f073e6
·
verified ·
1 Parent(s): 18c10a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -31
app.py CHANGED
@@ -1,44 +1,65 @@
1
  import os
2
- import gradio as gr
 
3
  from gradio_client import Client, handle_file
4
 
5
- # ดึง Token จาก Secrets ของ Space (ตั้งชื่อว่า HF_TOKEN) เพื่อสิทธิพิเศษในการเรียก API
6
- hf_token = os.getenv("HF_TOKEN")
 
 
 
7
 
8
- # ื่อมไปยัง Space ต้นทงผ่าน API
9
- # การใช้ Client แบบนี้จะไม่กินทรัพยากรเครื่องของคุณ
10
- client = Client("selfit-camera/omni-image-editor", hf_token=hf_token)
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def api_bridge(input_img, prompt):
13
- if input_img is None:
 
 
 
 
 
 
 
 
14
  return None
 
 
 
15
  try:
16
- # ส่งข้อมูลไปยัง API ต้นทาง
17
- # หมายเหตุ: ตรวจสอบ api_name จาก client.view_api() อีกครั้งหากต้นทางอัปเดต
18
  result = client.predict(
19
- image=handle_file(input_img),
20
  prompt=prompt,
21
  api_name="/predict"
22
  )
 
 
 
23
  return result
24
  except Exception as e:
25
- return f"Error: {str(e)}"
26
-
27
- # สร้างหน้า Interface ของคุณเอง
28
- with gr.Blocks(title="Omni Editor API Bridge") as demo:
29
- gr.Markdown("### Omni Image Editor (via API Bridge)")
30
- with gr.Row():
31
- with gr.Column():
32
- img_input = gr.Image(type="filepath", label="Upload Image")
33
- prompt_input = gr.Textbox(label="Edit Prompt", placeholder="เช่น 'make it look like a sketch'")
34
- submit_btn = gr.Button("Send to API", variant="primary")
35
- with gr.Column():
36
- img_output = gr.Image(label="Result")
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}")