Spaces:
Sleeping
Sleeping
File size: 3,769 Bytes
af8184f 5bbd57a 4d1d67b af8184f d98ee88 af8184f 209a37d af8184f 4d1d67b 1af19a0 4d1d67b 1af19a0 4d1d67b c357e42 af8184f acc19c4 af8184f acc19c4 af8184f acc19c4 af8184f acc19c4 af8184f acc19c4 af8184f acc19c4 1af19a0 acc19c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import gradio as gr
from PIL import Image
from io import BytesIO
import base64
import traceback
from backend import process_image
def inference(image: Image.Image, gemini_api_key: str):
"""
פונקציה שמבצעת זיהוי וטשטוש נשים בתמונה,
ומעדכנת את סרגל ההתקדמות בהתאם.
"""
if not gemini_api_key.strip():
raise gr.Error("אנא הכנס מפתח API של Gemini על מנת להמשיך")
progress = gr.Progress() # אובייקט לעדכון ההתקדמות
def progress_callback(fraction, description=""):
"""
פונקציה פנימית שתיקרא מ-backend בכל שלב.
fraction - ערך בין 0 ל-1 (לדוגמה 0.3 = 30%)
description - מלל להסבר השלב
"""
progress(fraction, desc=description)
try:
# כעת נקרא ל-process_image עם אפשרות לעדכן התקדמות
encoded_image = process_image(image, gemini_api_key, progress_callback=progress_callback)
decoded_image = Image.open(BytesIO(base64.b64decode(encoded_image)))
return decoded_image, gr.update(value="", visible=False) # החזרת תמונה ו-Textbox מוסתר
except Exception as e:
# טיפול בשגיאה והחזרת הודעה ב-Textbox
error_message = f"שגיאה: {type(e).__name__}\n"
error_message += f"הודעה: {e}\n\n"
error_message += "Traceback:\n"
error_message += traceback.format_exc()
return None, gr.update(value=error_message, visible=True) # החזרת Textbox גלוי עם שגיאה
title_str = "🤖 זיהוי וטשטוש נשים בתמונה"
description_str = """
<div style='text-align: center; direction: rtl'>
<p>
ברוכים הבאים לכלי לזיהוי וטשטוש נשים בתמונה!
<br>
העלו תמונה, הזינו את מפתח ה־API של Gemini,
ולחצו על "הרץ" כדי לנתח את התמונה ולטשטש אוטומטית נשים.
</p>
<p>
שימו לב: נדרש מפתח API תקין של Gemini כדי להשתמש בכלי זה.
<br>
הכלי משתמש בטכנולוגיות מתקדמות כמו YOLO, SAM2 ו-Gemini.
</p>
</div>
"""
# נתיבים לתמונות דוגמה
EXAMPLE_IMAGES = ["example_images/example.jpg", "example_images/example2.jpg", "example_images/example3.jpg"]
with gr.Blocks(
title=title_str,
css="style.css" # קישור לקובץ CSS
) as demo:
gr.Markdown(f"<h1 style='text-align: center;'>{title_str}</h1>")
gr.Markdown(description_str)
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="🖼️ בחרו תמונה לניתוח")
api_key_input = gr.Textbox(
label="🔑 מפתח API של Gemini",
placeholder="הכניסו את מפתח ה-API כאן",
type="password"
)
submit_button = gr.Button("🚀 הרץ", variant="primary")
gr.Examples(
examples=EXAMPLE_IMAGES,
inputs=image_input,
label="👇 דוגמאות",
# cache_examples=True # caching examples speeds up start time, but uses more memory
)
with gr.Column():
image_output = gr.Image(type="pil", label="🖼️ תוצאה לאחר טשטוש")
error_output = gr.Textbox(label="📜 שגיאות", visible=False, lines=5)
submit_button.click(
fn=inference,
inputs=[image_input, api_key_input],
outputs=[image_output, error_output]
)
if __name__ == "__main__":
demo.launch() |