File size: 2,046 Bytes
e444e3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
import os
os.system("pip install gradio --upgrade")

import gradio as gr
from pyjosa.josa import Josa
import random
import json

with open('pokemon.json', 'r') as f:
    pokemons = json.load(f)

QUESTION_TEMPLATE = {"question": "๋‹ค์Œ ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์€ ๋ญ˜๊นŒ์š”?![]({img_url})", "answer": "{name}"}
with gr.Blocks() as demo:
    quiz_start = gr.State(value=False)
    answer = gr.State(value="")
    chatbot = gr.Chatbot(bubble_full_width=False)
    msg = gr.Textbox()
    clear = gr.ClearButton([msg, chatbot])


    def respond(message, chat_history):
        if not quiz_start.value:
            chosen = random.choice(pokemons)
            name = chosen['name']
            image_path = chosen['image_path']
            answer.value = QUESTION_TEMPLATE['answer'].format(name=name)
            img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}"
            if "ํ€ด์ฆˆ ์‹œ์ž‘" == message:
                bot_message = "ํ€ด์ฆˆ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.\n" + QUESTION_TEMPLATE["question"].format(img_url=img_url)
                quiz_start.value = True
            else:
                bot_message = "ํ€ด์ฆˆ๋ฅผ ์‹œ์ž‘ํ•˜๊ณ  ์‹ถ์œผ์‹œ๋ฉด, **ํ€ด์ฆˆ ์‹œ์ž‘**์ด๋ผ๊ณ  ๋ง์”€ํ•ด์ฃผ์„ธ์š”."
        else:
            trial = Josa.get_full_string(message, "๋Š”")
            if answer.value == message:
                chosen = random.choice(pokemons)
                name = chosen['name']
                image_path = chosen['image_path']
                answer.value = QUESTION_TEMPLATE['answer'].format(name=name)
                img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}"
                bot_message = "์ •๋‹ต์ž…๋‹ˆ๋‹ค! ๋‹ค์Œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค." + QUESTION_TEMPLATE["question"].format(img_url=img_url)
            else:
                bot_message = f"{trial} ์ •๋‹ต์ผ๊นŒ์š”? ๋‹ค์‹œ ํ•œ๋ฒˆ ์ƒ๊ฐํ•ด๋ณด์„ธ์š”."

        chat_history.append((message, bot_message))
        return "", chat_history

    msg.submit(respond, [msg, chatbot], [msg, chatbot])

demo.launch()