File size: 2,418 Bytes
e444e3b
039d478
e444e3b
 
 
 
 
 
039d478
e444e3b
 
 
 
 
039d478
e444e3b
039d478
 
e444e3b
d9c3ab3
 
 
e444e3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
039d478
e444e3b
039d478
 
 
 
 
 
e444e3b
 
039d478
e444e3b
 
039d478
55c3782
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
53
54
55
import gradio as gr
import pyjosa
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(value="ν€΄μ¦ˆ μ‹œμž‘")
    clear = gr.ClearButton([msg, chatbot])
    
    
    def respond(message, chat_history):
        if "ν€΄μ¦ˆ μ‹œμž‘" == message:
            bot_message = "ν€΄μ¦ˆλ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€.\n" + QUESTION_TEMPLATE["question"].format(img_url=img_url)
            quiz_start.value = True
        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:
            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:
                try:
                    trial = Josa.get_full_string(message, "λŠ”")
                except pyjosa.exceptions.NotHangleException:
                    trial = f"{message}!?"
                bot_message = f"***{trial}*** μ •λ‹΅μΌκΉŒμš”? 🧐 λ‹€μ‹œ ν•œλ²ˆ μƒκ°ν•΄λ³΄μ„Έμš”."
        
        chat_history.append((message, bot_message))
        return "", chat_history
    
    msg.submit(respond, [msg, chatbot], [msg, chatbot])

demo.queue(concurrency_count=3)
demo.launch()