ai-illust / app.py
Roberte23's picture
AI ์ด๋ฏธ์ง€ ํŒ๋ณ„๊ธฐ ์—…๋กœ๋“œ
c89571f
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
# ๋ธ”๋ž™&ํ™”์ดํŠธ UI + ์™ผ/์˜ค ๋น„์œจ ๋ ˆ์ด์•„์›ƒ
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])
# CSS๋กœ ์Šคํƒ€์ผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•
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()