Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import streamlit as stfrom huggingface_hub import InferenceClientimport random
# Replace with your desired model ID from Hugging Face Hubclient = InferenceClient("unsloth/Llama-3.2-1B-Instruct")
words = [ # ... Add your list of nouns, adjectives, verbs, and adverbs here ...]
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) st.session_state["target_word"] = self.target_word # Store in session state st.title(f'The Game - Current score: {self.points}, remaining attempts: {self.attempts}')
def check_input_for_word(self, user_input): if self.target_word in user_input.lower(): self.generate_task() self.points -= 1 st.write(f"You input the target word yourself, so you lost one point and the game reset.") return True else: return False
def check_output_for_word(self, response): if self.target_word in response.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 respond(self, user_message): messages = [{"role": "system", "content": st.session_state["system_message"]}] for val in st.session_state["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": user_message}) response = "" for message in client.chat_completion( messages, max_tokens=st.session_state["max_tokens"], stream=True, temperature=st.session_state["temperature"], top_p=st.session_state["top_p"] ): token = message.choices[0].delta.content response += token output_check_result = self.check_output_for_word(response) st.write(response + f"\n\n---\n\n{output_check_result}") st.write(f'Current score: {self.points}, remaining attempts: {self.attempts}')
# Update session state with history and system message st.session_state["history"].append((user_message, response)) st.session_state["system_message"] = response
game = WordGame()
# Session state variables to store dynamic informationst.session_state["history"] = [] # List of tuples (user message, response)st.session_state["system_message"] = "You are a friendly Chatbot." # Initial system message
# Streamlit interface elementssystem_message_input = st.text_input("System message", value=st.session_state["system_message"])max_tokens_slider = st.slider("Max new tokens", minimum=1, maximum=2048, value=512, step=1)temperature_slider = st.slider("Temperature", minimum=0.1, maximum=4.0, value=0.7, step=0.1)top_p_slider = st.slider("Top-p (nucleus sampling)", minimum=0.1, maximum=1.0, value=0.95, step=0.05)
# Update session state with user input for system messagest.session_state["system_message"] = system_message_input
user_message = st.text_app.pyAdded+91