File size: 7,371 Bytes
7069580
 
f0a2ae7
7069580
 
 
 
6137fab
ca4687f
5beb563
a9faf47
53875ea
fbd18b4
53875ea
4c6a57b
7069580
f0a2ae7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356d685
f0a2ae7
 
 
 
 
 
 
 
21c4293
f0a2ae7
 
 
21c4293
f0a2ae7
53875ea
 
356d685
 
f0a2ae7
21c4293
a289b02
21c4293
f0a2ae7
 
7d36684
f0a2ae7
 
21c4293
53875ea
f0a2ae7
356d685
 
21c4293
 
 
 
 
 
356d685
 
 
21c4293
 
a289b02
53875ea
 
58599b4
356d685
 
 
 
 
 
f0a2ae7
 
53875ea
356d685
 
f0a2ae7
 
 
7069580
 
 
 
 
 
 
 
 
 
3e72827
 
356d685
3e72827
7069580
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b013a9
 
 
b3cfc9f
8c6ba15
09fb6e2
b3cfc9f
58599b4
b3cfc9f
7069580
 
 
 
 
 
 
53d6bc4
7069580
 
 
 
 
 
 
 
 
 
 
 
 
 
53875ea
7069580
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import gradio as gr
from huggingface_hub import InferenceClient
import random

"""
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
"""
#client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
#client = InferenceClient("DrDomedag/LocoLama")
#client = InferenceClient("unsloth/llama-3.2-1b-instruct-bnb-4bit")
#client = InferenceClient("unsloth/llama-3.2-1b-bnb-4bit")
client = InferenceClient("unsloth/Llama-3.2-1B-Instruct") # Works!
#client = InferenceClient("unsloth/Llama-3.2-3B-Instruct") # Works!
#client = InferenceClient("DrDomedag/LocoLamav2")
#client = InferenceClient("T3lli/lora_model")



words = [
    # Nouns (250)
    "apple", "bridge", "cat", "door", "engine", "forest", "giraffe", "horizon", "island", "jungle",
    "kite", "lake", "mountain", "notebook", "ocean", "penguin", "quartz", "rainbow", "snowflake", "tornado",
    "umbrella", "village", "waterfall", "xylophone", "yard", "zebra", "actor", "ball", "camera", "desert",
    "elephant", "firefly", "garden", "hat", "igloo", "jacket", "key", "lantern", "mirror", "necklace",
    "owl", "piano", "quiver", "rocket", "squirrel", "trophy", "unicorn", "vase", "window", "yacht",
    # ... Add more nouns to total 250

    # Adjectives (250)
    "brave", "calm", "delightful", "eager", "fancy", "gentle", "happy", "innocent", "jolly", "kind",
    "lively", "magnificent", "noble", "optimistic", "peaceful", "quick", "radiant", "shy", "tidy", "unique",
    "vivid", "warm", "yellow", "zealous", "adorable", "beautiful", "charming", "diligent", "energetic", "fierce",
    "graceful", "humble", "intelligent", "jovial", "keen", "lovely", "merry", "neat", "outstanding", "pleasant",
    "quirky", "respectful", "silly", "thoughtful", "upbeat", "vibrant", "whimsical", "youthful", "zany",
    # ... Add more adjectives to total 250

    # Verbs (250)
    "accept", "bounce", "climb", "dance", "explore", "fly", "gather", "help", "imagine", "jump",
    "kick", "laugh", "move", "notice", "open", "play", "question", "run", "sing", "talk",
    "understand", "visit", "wait", "yell", "zoom", "answer", "build", "create", "dig", "enjoy",
    "focus", "grow", "hunt", "identify", "juggle", "know", "learn", "measure", "negotiate", "observe",
    "perform", "quiet", "record", "search", "travel", "update", "volunteer", "wander", "write",
    # ... Add more verbs to total 250

    # Adverbs (250)
    "abruptly", "beautifully", "carefully", "diligently", "eagerly", "faithfully", "gracefully", "happily",
    "immediately", "joyfully", "kindly", "loudly", "magically", "neatly", "openly", "politely", "quickly",
    "rarely", "silently", "thoughtfully", "unexpectedly", "vividly", "warmly", "yawningly", "zealously",
    "accidentally", "boldly", "cheerfully", "deliberately", "enthusiastically", "frequently", "gently", "honestly",
    "intensely", "justly", "knowingly", "lightly", "merrily", "nervously", "officially", "partially", "quietly",
    "readily", "safely", "terribly", "urgently", "vaguely", "wildly", "yearly", "zestfully",
    # ... Add more adverbs to total 250
]


class WordGame:

    def __init__(self):
        self.points = 0
        self.target_word = ""
        self.attempts = 3
        self.generate_task()
    
    def generate_task(self):
        self.attempts = 3
        self.target_word = random.choice(words)
        print(f"New target word: {self.target_word}")

    def check_input_for_word(self, string):
        if self.target_word in string:
            print(f"The player input the target word and the task was reset.")
            self.generate_task()
            self.points -= 1
            self.update_status(message="You input the target word yourself, so you lost one point and the game reset.")
            return "You input the target word yourself, so you lost one point and the game reset."
            
        else:
            print(f"The player did not input the target word, so that's good.")
            return ""
            

    def check_output_for_word(self, string):
        print(f"Checking for '{self.target_word}' in '{string}'.")
        if self.target_word in string:
            self.points += self.attempts
            print(f"The player scored {self.attempts} points and has a total of {self.points}.")
            score_gained = self.attempts
            self.generate_task()
            return f"Success! You earned {score_gained} points!"
            #self.update_status(message=f"Success! You earned {score_gained} points!")
        else:
            print("The response did not contain the word.")
            self.attempts -= 1
            print(f"Remaining attempts: {self.attempts}")
            if self.attempts <= 0:
                self.generate_task()
                #print(f"The player ran out of attempts.")
                return f"You did not win in three attempts. Generating new target word."
                #self.update_status(message=f"You did not win in three attempts. Generating new target word.")
            else:
                print(f"The player has attempts left.")
                return "That didn't quite hit the mark. Try again!"

    def update_status(self, message=""):
        return f'Current score: {self.points}, remaining attempts: {self.attempts}, target word: "{self.target_word}" {message}'

    def analyse_output(self, message, history):
        print("---message---")
        print(message)
        print("---message---")
        print(history)
    


game = WordGame()




def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
):
    messages = [{"role": "system", "content": system_message}]

    print("message:")
    print(message)
    input_check_result = game.check_input_for_word(message)

    for val in history:
        if val[0]:
            messages.append({"role": "user", "content": val[0]})
        if val[1]:
            messages.append({"role": "assistant", "content": val[1]})

    messages.append({"role": "user", "content": message})

    response = ""

    for message in client.chat_completion(
        messages,
        max_tokens=max_tokens,
        stream=True,
        temperature=temperature,
        top_p=top_p,
    ):
        token = message.choices[0].delta.content

        response += token

        
        
        #yield response
    print("response:")
    print(response)
    output_check_result = game.check_output_for_word(response)
    response = response + " \n\n---\n\n" + input_check_result + " " + output_check_result + "\n" + game.update_status()
    return response


"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
demo = gr.ChatInterface(
    respond,
    title="The Game",
    additional_inputs=[
        gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
    ],
)



if __name__ == "__main__":
    demo.launch()