File size: 7,796 Bytes
7069580 f0a2ae7 7069580 6137fab ca4687f 5beb563 a9faf47 1c9bb19 fbd18b4 53875ea 4c6a57b 1c9bb19 7069580 f0a2ae7 0be70f1 f0a2ae7 0be70f1 f0a2ae7 45c0950 ae925ca f0a2ae7 ae925ca 45c0950 f0a2ae7 53875ea ae925ca 356d685 f0a2ae7 45c0950 ae925ca 21c4293 f0a2ae7 f022b1f e2d52d2 f0a2ae7 45c0950 53875ea f0a2ae7 356d685 21c4293 f022b1f 21c4293 45c0950 21c4293 356d685 21c4293 45c0950 a289b02 53875ea 36de388 50a6a89 58599b4 f0a2ae7 53875ea 356d685 f0a2ae7 7069580 3e72827 ae925ca 3e72827 7069580 7b013a9 f688296 415e439 8c6ba15 09fb6e2 edcfe5b 415e439 bd85afe ae925ca 415e439 ae925ca b3cfc9f 415e439 7069580 792a55f 7069580 53875ea 7069580 0be70f1 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
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")
#client = InferenceClient("T3lli/test")
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, chatbot):
self.points = 0
self.target_word = ""
self.attempts = 3
self.chatbot = chatbot
self.generate_task()
def generate_task(self):
self.attempts = 3
self.target_word = random.choice(words)
#print(f"New target word: {self.target_word}")
return self.target_word
def check_input_for_word(self, string):
if self.target_word in string.lower():
#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 True
else:
#print(f"The player did not input the target word, so that's good.")
return False
def check_output_for_word(self, string):
#print(f"Checking for '{self.target_word}' in '{string}'.")
if self.target_word in string.lower():
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=""):
#gr.ChatInterface.update(title=f'The Game - Current score: {self.points}, remaining attempts: {self.attempts}, target word: "{self.target_word}" {message}')
self.chatbot.update(title=f'The Game - Current score: {self.points}, remaining attempts: {self.attempts}, target word: "{self.target_word}" {message}')
return f'Current score: {self.points}, remaining attempts: {self.attempts}, target word: "{self.target_word}" {message}'
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)
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)
game.check_output_for_word(response)
print(game.update_status())
'''
if game.check_input_for_word(str(message)):
response = response + " \n\n---\n\n You input the word and lost one point!"
else:
output_check_result = game.check_output_for_word(response)
response = response + " \n\n---\n\n" + output_check_result
response = response + "\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=f"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)",
),
],
)
game = WordGame(demo)
if __name__ == "__main__":
demo.launch()
|