File size: 1,590 Bytes
0251dfb
 
4c00ae9
0251dfb
4c00ae9
0251dfb
4c00ae9
 
c511ff9
3bb0699
0251dfb
 
 
97ded66
0251dfb
 
 
4c00ae9
 
 
 
 
 
 
 
 
97ded66
b6b0c44
4c00ae9
 
c7cae0c
e88d808
4c00ae9
b6b0c44
e88d808
 
53d11fc
215797e
e11821d
087ce8b
e88d808
 
49b272e
10fbe99
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
from fastai.vision.all import *
import gradio as gr
import random

__all__ = ['is_rock', 'learn', 'classify_image', 'determine_winner', 'play game' 'categories', 'image', 'label', 'examples', 'intf']

def is_rock(x):
    return x[0].issuper()

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)
    computer_image = get_image_files(f'{computer_choice}.jpg')  
    return f"User's choice: {user_choice}\nComputer's choice: {computer_choice}\n{winner}"#, computer_image


image = gr.inputs.Image(shape=(192, 192))
label = gr.outputs.Label()

examples = ['rock.jpg', 'paper.jpg', 'scissor.jpg']

intf = gr.Interface(fn=play_game, inputs=image, outputs= label, examples = examples)
#intf.blocks[0].block_id = 0  # Unique ID for the image input block
#intf.blocks[1].block_id = 1  # Unique ID for the label output block
intf.launch(inline=False)