|
import gradio as gr |
|
from transformers import AutoProcessor, AutoModelForImageClassification |
|
from PIL import Image |
|
import torch |
|
|
|
|
|
model_id = "dima806/ai_vs_human_generated_image_detection" |
|
processor = AutoProcessor.from_pretrained(model_id) |
|
model = AutoModelForImageClassification.from_pretrained(model_id) |
|
|
|
def detect_image(image): |
|
inputs = processor(images=image, return_tensors="pt") |
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
probs = torch.nn.functional.softmax(logits, dim=1) |
|
confidences = probs[0].tolist() |
|
labels = model.config.id2label |
|
|
|
|
|
result = sorted(zip(labels.values(), confidences), key=lambda x: x[1], reverse=True) |
|
|
|
top_label, top_conf = result[0] |
|
explanation = "์ด ์ด๋ฏธ์ง๋ '{0}'๋ก ๋ถ๋ฅ๋์์ต๋๋ค.\n\n์ ๋ขฐ๋: {1:.2f}%\n\n".format(top_label, top_conf * 100) |
|
|
|
|
|
if "AI" in top_label: |
|
explanation += "๋ฐฐ๊ฒฝ ํ๋ฆผ, ๋นํ์ค์ ์ธ ๋ํ
์ผ, ๋ฐ๋ณต๋๋ ํจํด ๋ฑ AI ์ด๋ฏธ์ง์ ์ ํ์ ํน์ง์ด ๊ฐ์ง๋์์ต๋๋ค." |
|
else: |
|
explanation += "์กฐ๋ช
, ์ง๊ฐ, ์๊ณก ์๋ ๋ํ
์ผ ๋ฑ ์ค์ ์ดฌ์ ์ด๋ฏธ์ง์ ํน์ง์ด ํ์ธ๋์์ต๋๋ค." |
|
|
|
return f"{top_label} ({top_conf*100:.2f}%)", explanation |
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Base(primary_hue="gray")) as demo: |
|
gr.Markdown( |
|
"# ๐จ AI vs Human ์ด๋ฏธ์ง ํ๋ณ๊ธฐ", |
|
elem_id="title" |
|
) |
|
|
|
with gr.Row(equal_height=False): |
|
with gr.Column(scale=1): |
|
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋", elem_id="image-box") |
|
|
|
with gr.Column(scale=2): |
|
label_output = gr.Textbox(label="ํ๋ณ ๊ฒฐ๊ณผ (๊ฐ์ฅ ๋์ ์ ๋ขฐ๋)", elem_id="result-label", max_lines=1, interactive=False) |
|
text_output = gr.Textbox(label="ํ๋ณ ์ด์ ", lines=10, elem_id="result-text", interactive=False) |
|
|
|
image_input.change(fn=detect_image, inputs=image_input, outputs=[label_output, text_output]) |
|
|
|
|
|
demo.css = """ |
|
body { |
|
background-color: #111; |
|
color: #eee; |
|
font-family: 'Helvetica Neue', sans-serif; |
|
} |
|
#title { |
|
text-align: center; |
|
font-size: 32px; |
|
color: white; |
|
margin-bottom: 20px; |
|
} |
|
#image-box { |
|
background-color: #222; |
|
border: 1px solid #444; |
|
padding: 10px; |
|
} |
|
#result-label textarea { |
|
font-size: 24px; |
|
color: #00ffcc; |
|
background-color: #1a1a1a; |
|
} |
|
#result-text textarea { |
|
font-size: 16px; |
|
background-color: #1a1a1a; |
|
color: #ccc; |
|
} |
|
""" |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|