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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -50
app.py CHANGED
@@ -1,65 +1,47 @@
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}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import gradio as gr
 
3
  from gradio_client import Client, handle_file
4
 
5
+ # Fetch Token from Space Secrets (Name it HF_TOKEN in Settings)
6
+ HF_TOKEN = os.getenv("HF_TOKEN")
7
+ TARGET_SPACE = "selfit-camera/omni-image-editor"
 
 
8
 
9
+ def bridge_api(image_path, prompt):
10
+ if not image_path:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  return None
 
 
12
 
13
  try:
14
+ # Direct connection to the target API
15
+ client = Client(TARGET_SPACE, hf_token=HF_TOKEN)
16
+
17
+ # API prediction call
18
+ # Note: Ensure parameter names match target space's client.view_api()
19
  result = client.predict(
20
  image=handle_file(image_path),
21
  prompt=prompt,
22
  api_name="/predict"
23
  )
 
 
 
24
  return result
25
  except Exception as e:
26
+ return f"API Bridge Error: {str(e)}"
 
 
 
 
 
 
27
 
28
+ # UI Layout
29
+ with gr.Blocks(title="Omni Editor Proxy") as demo:
30
+ gr.Markdown("## 🚀 Omni Image Editor (Proxy Mode)")
31
+ gr.Markdown("Processing is handled by the backend API and returned here.")
32
+
33
+ with gr.Row():
34
+ with gr.Column():
35
+ input_img = gr.Image(type="filepath", label="Input Image")
36
+ input_prompt = gr.Textbox(label="Edit Prompt", placeholder="e.g., 'change background to sunset'")
37
+ submit_btn = gr.Button("Process", variant="primary")
38
+ with gr.Column():
39
+ output_img = gr.Image(label="Output")
40
+
41
+ submit_btn.click(
42
+ fn=bridge_api,
43
+ inputs=[input_img, input_prompt],
44
+ outputs=output_img
45
+ )
46
+
47
+ demo.launch()