Download app.py from Alexis92/PlayRock: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/Alexis92/PlayRock/resolve/main/app.py
- Command line
-
hf download hf://spaces/Alexis92/PlayRock/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Alexis92/PlayRock/resolve/main/app.py
1.15 kB
| from fastai.vision.all import * | |
| import gradio as gr | |
| import random | |
| def is_rock(x): | |
| return x.startswith('r') | |
| learn = load_learner('RPS_model2.pkl') | |
| categories = ('paper', 'rock', 'scissors') | |
| def classify_image(img): | |
| pred, idx, probs = learn.predict(img) | |
| return dict(zip(categories, map(float, probs))) | |
| def determine_winner(user_choice, computer_choice): | |
| if user_choice == computer_choice: | |
| return "It's a tie!" | |
| elif (user_choice == 'rock' and computer_choice == 'scissors') or (user_choice == 'paper' and computer_choice == 'rock') or (user_choice == 'scissors' and computer_choice == 'paper'): | |
| return "You won!" | |
| else: | |
| return "Computer won!" | |
| def play_game(img): | |
| user_probs = classify_image(img) | |
| user_choice = max(user_probs, key=user_probs.get) | |
| computer_choice = random.choice(categories) | |
| winner = determine_winner(user_choice, computer_choice) | |
| return f"User's choice: {user_choice}\nComputer's choice: {computer_choice}\n{winner}" | |
| image = gr.Image(shape=(192, 192)) | |
| label = gr.Label() | |
| intf = gr.Interface(fn=play_game, inputs=image, outputs=label) | |
| intf.launch(inline=False) | |