|
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("unsloth/Llama-3.2-1B-Instruct") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
words = [ |
|
|
|
"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", |
|
|
|
|
|
|
|
"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", |
|
|
|
|
|
|
|
"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", |
|
|
|
|
|
|
|
"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", |
|
|
|
] |
|
|
|
|
|
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) |
|
|
|
return self.target_word |
|
|
|
def check_input_for_word(self, string): |
|
if self.target_word in string.lower(): |
|
|
|
self.generate_task() |
|
self.points -= 1 |
|
|
|
return True |
|
|
|
else: |
|
|
|
return False |
|
|
|
|
|
def check_output_for_word(self, string): |
|
|
|
if self.target_word in string.lower(): |
|
self.points += self.attempts |
|
|
|
score_gained = self.attempts |
|
self.generate_task() |
|
return f"Success! You earned {score_gained} points!" |
|
|
|
else: |
|
|
|
self.attempts -= 1 |
|
|
|
if self.attempts <= 0: |
|
self.generate_task() |
|
|
|
return f"You did not win in three attempts. Generating new target word." |
|
|
|
else: |
|
|
|
return "That didn't quite hit the mark. Try again!" |
|
|
|
def update_status(self, 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() |
|
|