Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,27 +2,77 @@ import gradio as gr
|
|
2 |
import random
|
3 |
from PIL import Image
|
4 |
import time
|
5 |
-
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
6 |
import torch
|
7 |
-
from
|
8 |
-
|
9 |
-
# טוענים דאטאסט
|
10 |
-
dataset = load_dataset("imagefolder", data_dir=".", split={"train": "train[:80%]", "test": "train[80%:]"})
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
checkpoint = "facebook/deit-tiny-patch16-224"
|
15 |
processor = AutoImageProcessor.from_pretrained(checkpoint)
|
16 |
-
|
17 |
-
# במקום להטעין מודל סופי → טוענים ואז משנים את הראש
|
18 |
model = AutoModelForImageClassification.from_pretrained(
|
19 |
checkpoint,
|
20 |
num_labels=3,
|
21 |
id2label={0: "rock", 1: "paper", 2: "scissors"},
|
22 |
label2id={"rock": 0, "paper": 1, "scissors": 2},
|
23 |
-
ignore_mismatched_sizes=True
|
24 |
)
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import random
|
3 |
from PIL import Image
|
4 |
import time
|
|
|
5 |
import torch
|
6 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
|
|
|
|
|
|
7 |
|
8 |
+
# הגדרת קטגוריות
|
9 |
+
moves = ["rock", "paper", "scissors"]
|
10 |
+
computer_images = {
|
11 |
+
"rock": "computer_rock.png",
|
12 |
+
"paper": "computer_paper.png",
|
13 |
+
"scissors": "computer_scissors.png"
|
14 |
+
}
|
15 |
|
16 |
+
# טעינת מודל
|
17 |
checkpoint = "facebook/deit-tiny-patch16-224"
|
18 |
processor = AutoImageProcessor.from_pretrained(checkpoint)
|
|
|
|
|
19 |
model = AutoModelForImageClassification.from_pretrained(
|
20 |
checkpoint,
|
21 |
num_labels=3,
|
22 |
id2label={0: "rock", 1: "paper", 2: "scissors"},
|
23 |
label2id={"rock": 0, "paper": 1, "scissors": 2},
|
24 |
+
ignore_mismatched_sizes=True
|
25 |
)
|
26 |
|
27 |
+
# חוקים
|
28 |
+
def game_logic(user_move, computer_move):
|
29 |
+
if user_move == computer_move:
|
30 |
+
return "It's a tie!"
|
31 |
+
if (user_move == "rock" and computer_move == "scissors") or \
|
32 |
+
(user_move == "paper" and computer_move == "rock") or \
|
33 |
+
(user_move == "scissors" and computer_move == "paper"):
|
34 |
+
return "You win!"
|
35 |
+
else:
|
36 |
+
return "You lose!"
|
37 |
+
|
38 |
+
# פונקציית משחק מלאה
|
39 |
+
def play(live_image):
|
40 |
+
prediction = processor(images=live_image, return_tensors="pt")
|
41 |
+
outputs = model(**prediction)
|
42 |
+
logits = outputs.logits
|
43 |
+
predicted_class_idx = logits.argmax(-1).item()
|
44 |
+
user_move = model.config.id2label[predicted_class_idx]
|
45 |
+
|
46 |
+
computer_move = random.choice(moves)
|
47 |
+
computer_img = Image.open(computer_images[computer_move])
|
48 |
+
|
49 |
+
result = game_logic(user_move, computer_move)
|
50 |
+
|
51 |
+
return live_image, computer_img, f"You chose {user_move}, computer chose {computer_move}. {result}"
|
52 |
+
|
53 |
+
# ספירה לאחור + משחק
|
54 |
+
def full_play(live_image):
|
55 |
+
# ספירה לאחור
|
56 |
+
for i in ["3...", "2...", "1...", "GO!"]:
|
57 |
+
print(i)
|
58 |
+
time.sleep(1)
|
59 |
+
return play(live_image)
|
60 |
+
|
61 |
+
# בניית אפליקציה
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
gr.Markdown("# ✂️ 🪨 📄 Rock Paper Scissors - LIVE Game!")
|
64 |
+
|
65 |
+
webcam = gr.Camera(label="Show your move after countdown!")
|
66 |
+
play_button = gr.Button("Start Countdown and Play")
|
67 |
+
|
68 |
+
user_output = gr.Image(label="Your Move")
|
69 |
+
computer_output = gr.Image(label="Computer's Move")
|
70 |
+
result_text = gr.Textbox(label="Result")
|
71 |
+
|
72 |
+
play_button.click(
|
73 |
+
fn=full_play,
|
74 |
+
inputs=[webcam],
|
75 |
+
outputs=[user_output, computer_output, result_text]
|
76 |
+
)
|
77 |
+
|
78 |
+
demo.launch()
|